嵌套滚动无效(Horizo​​ntalScrollView内的Horizo​​ntalScrollView)

时间:2016-12-22 18:55:38

标签: android scrollview horizontalscrollview nestedscrollview android-nestedscrollview

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

Horizo​​ntalScrollView     LinearLayout(明显水平)         定期查看         Horizo​​ntalScrollView             TextView的

默认情况下,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>

它应该有效,我不明白...... 感谢

2 个答案:

答案 0 :(得分:0)

您需要使用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>

答案 1 :(得分:-2)

尝试将NestedScrollView与子LinearLayout一起使用,方向为水平。

  

NestedScrollView就像ScrollView,但它支持表现为   新旧版本上的嵌套滚动父级和子级   Android版默认情况下启用嵌套滚动。

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

相关问题