【问题标题】:Nested scroll not working (HorizontalScrollView inside HorizontalScrollView)嵌套滚动不起作用(Horizo​​ntalScrollView 内的 Horizo​​ntalScrollView)
【发布时间】:2016-12-22 18:55:38
【问题描述】:

我想利用 API 21 中添加的新 NestedScroll 功能。 我的布局很简单:

水平滚动视图 LinearLayout(显然是水平的) 常规视图 水平滚动视图 文本视图

默认情况下,nestedScrollEnabled 为 false。所以我在 xml 中为我想要滚动到根 Horizo​​ntalScrollView 的子项(例如内部 Horizo​​ntalScrollView)启用了它。 因此它什么也不做。只有顶部的滚动视图可以滚动,内部的似乎根本看不到任何滚动触摸事件。

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/scrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.majeur.test.MainActivity">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">

        <HorizontalScrollView
            android:id="@+id/scrollView2"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:nestedScrollingEnabled="true">

            <TextView
                android:layout_width="250dp"
                android:layout_height="match_parent"
                android:text="@string/text" />

        </HorizontalScrollView>

        <View
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:background="@color/colorAccent"/>


    </LinearLayout>
</HorizontalScrollView>

这是它应该工作的 wau,我不明白...... 谢谢

【问题讨论】:

  • 你找到解决办法了吗?

标签: android scrollview horizontalscrollview nestedscrollview android-nestedscrollview


【解决方案1】:

您需要使用 onTouchListener 拦截触摸:

// Intercept touch scroll by 
    transparentImageView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    // Disallow ScrollView to intercept touch events.
                    scrollView.requestDisallowInterceptTouchEvent(true);
                    // Disable touch on transparent view
                    return false;

                case MotionEvent.ACTION_UP:
                    // Allow ScrollView to intercept touch events.
                    scrollView.requestDisallowInterceptTouchEvent(false);
                    return true;

                case MotionEvent.ACTION_MOVE:
                    scrollView.requestDisallowInterceptTouchEvent(true);
                    return false;

                default:
                    return true;
            }
        }
    });

还有你的 XML....

  <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.majeur.test.MainActivity">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent">


   <RelativeLayout
            android:id="@+id/mapLayout"
            android:layout_width="match_parent"
            android:layout_height="300dp" >
    <HorizontalScrollView
        android:id="@+id/scrollView2"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:nestedScrollingEnabled="true">

        <TextView
            android:layout_width="250dp"
            android:layout_height="match_parent"
            android:text="@string/text" />

    </HorizontalScrollView>
               <ImageView
                android:id="@+id/transparent_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                />
     </RelativeLayout>

    <View
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:background="@color/colorAccent"/>


</LinearLayout>

【讨论】:

  • 感谢您的帮助,但这不是我想要的!该解决方案的问题在于,当子级无法再滚动时,它不会将滚动返回给父级。这就是嵌套滚动“框架”的全部目的,它允许滚动在层次结构中的不同视图之间同步。
  • 我们这里要做的是不阻止父滚动锁定...我们正在拦截它...这意味着它将取消外部滚动的效果以启用内部当用户滚动内部滚动时滚动......所以它不会以任何方式影响父滚动......至少在我的情况下不会!
【解决方案2】:

尝试使用带有水平方向的子 LinearLayout 的 NestedScrollView。

NestedScrollView 和 ScrollView 一样,但它支持充当 新旧版本中的嵌套滚动父项和子项 安卓的。默认启用嵌套滚动。

https://developer.android.com/reference/android/support/v4/widget/NestedScrollView.html

【讨论】:

  • 在 API 21+ 中 support.v4.widget.NestedScrollView 应该与框架 ScrollView 完全相同。无论如何,我会尝试支持库。嵌套滚动的记录非常糟糕,我无法确定是否支持 NestedScrollView 是一个垂直 ScrollView 或者它是否决定了它应该在哪些轴上滚动。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多