触摸 TextView(带有 android:gravity=“center")时无法滚动 ViewPager [英] can not scroll the ViewPager when touching TextView(with android:gravity="center") in it

查看:20
本文介绍了触摸 TextView(带有 android:gravity=“center")时无法滚动 ViewPager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 ViewPager 中,LinearLayout 保存了一个 ImageButton 和 TextView,现在我将它们更改为一个带有 coupound drawable 的 TextView.

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>

我的问题是:触摸 TextView 时我无法向左或向右翻转,但如果删除android:gravity="center",它可以工作......不幸的是,文本无论如何都没有居中......

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.

推荐答案

android:singleLine="true" 强制 android:scrollHorizo​​ntally 设置为 true,并且根据我的测试,仅当您更改 android:gravity (即默认重力似乎不会出现此问题).

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

我在 TextView 中解决了这个问题,只需将 android:singleLine="true" 替换为 android:maxLines="1"

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

在此更改之后,我对 ViewPager 不再有任何问题.现在它在使用 android:gravity="right" 触摸元素时按预期滚动.

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

这篇关于触摸 TextView(带有 android:gravity=“center")时无法滚动 ViewPager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆