Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...