Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can not scroll the ViewPager when touching TextView(with android:gravity="center") in it

In my ViewPager, there was a ImageButton and TextView hold by LinearLayout, and now I change them to one TextView with coupound drawable.

<?xml version="1.0" encoding="utf-8"?>
<com.iclinux.android.custom_views.NoScrollTextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:iclinux="http://schemas.android.com/apk/res-auto"
    android:clipChildren="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:singleLine="true"
    android:gravity="center"
    android:paddingLeft="8dp"
    android:paddingRight="8dp"
    android:drawableTop="@drawable/icon_bg"
    style="@style/EllipsizedSingleLineTextView"
    android:textSize="@dimen/grid_item_title_size"
    >
</com.iclinux.android.custom_views.NoScrollTextView>

My question is: I can not flip left or right when touching the TextView, but if removed "android:gravity="center", it works... unfortunately, the text is not centered anyway...

public class NoScrollTextView extends TextView {

    public NoScrollTextView(Context context) {
        super(context);
    }

    public NoScrollTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public NoScrollTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE)
            return false;
        return super.onTouchEvent(event);
    }
}

What happened here? thanks a lot.

like image 708
iclinux Avatar asked Jun 04 '13 10:06

iclinux


People also ask

Is ViewPager scrollable?

The ViewPager is populated with fragments taking up space way beyond the screen (vertically). Still, nothing on the screen is scrollable.

How to add ViewPager in activity in android?

You can't use ViewPager to swipe between Activities . You need to convert each of you five Activities into Fragments , then combine everything in one FragmentActivity with the Adapter you use with ViewPager .

What is View pager?

ViewPager is a layout manager that allows the user to flip left and right through pages of data. It is mostly found in apps like Youtube, Snapchat where the user shifts right – left to switch to a screen. Instead of using activities fragments are used.


2 Answers

solved by overriding:

@TargetApi(14)
@Override
public boolean canScrollHorizontally(int direction) {
    return false;
}
like image 50
iclinux Avatar answered Sep 20 '22 05:09

iclinux


android:singleLine="true" forces android:scrollHorizontally to be set to true, and according to my tests only if you change android:gravity (i.e. default gravity seems not to give this problem).

I solved this issue in a TextView by simply replacing android:singleLine="true" with android:maxLines="1"

After this change, I had no more problems with the ViewPager. Now it is scrolling as expected while touching elements with android:gravity="right".

like image 21
agirardello Avatar answered Sep 20 '22 05:09

agirardello