【问题标题】:Scrolling is not working when I set GestureListener to ScrollView当我将 GestureListener 设置为 ScrollView 时,滚动不起作用
【发布时间】:2016-08-15 17:03:48
【问题描述】:

我有一个ScrollView 和一个嵌套的TextView。当我将setOnTouchListener 设置为它时,手势被识别但滚动不起作用。如果我将setOnTouchListener 设置为嵌套TextView,它工作正常。我已经尝试通过谷歌搜索但无法解决问题。 但我需要将setOnTouchListener 设置为ScrollView。

请帮忙。

layout.xml

<ScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#FF0000"
            android:id="@+id/scrollView">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/tv"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textColor="#000000"
                    android:background="#00FF00"
                    android:padding="15dp"
                    android:textSize="18sp" />
            </LinearLayout>
        </ScrollView>

MainActivity.java

mGestureDetector = new GestureDetectorCompat(this,this);
        findViewById(R.id.scrollView).setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, final MotionEvent event) {
                Log.e("Stark", "setOnTouchListener");
                mGestureDetector.onTouchEvent(event);
                return true;
            }
        });

【问题讨论】:

  • 不完全确定,但我相信您是说 onTouch 中的 return true 是它处理了 touchevent。这样 ScrollView 将不再处理触摸事件(滚动)
  • 你震撼了。它通过将 return 设置为 false 来工作。谢谢:)

标签: android scrollview android-gesture


【解决方案1】:

为什么需要设置 onTouch。如果只是上下滚动。试试下面

ScrollView sv;

sv.post(new Runnable() {
                @Override
                public void run() {
                    sv.fullScroll(View.FOCUS_DOWN);
                }
            });

确保你在 scrollview xml 中有这个。希望有帮助。

android:fillViewport="true"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-03
    相关资源
    最近更新 更多