首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嵌套滚动不起作用(HorizontalScrollView中的HorizontalScrollView)

嵌套滚动不起作用(HorizontalScrollView中的HorizontalScrollView)
EN

Stack Overflow用户
提问于 2016-12-23 02:55:38
回答 2查看 3.8K关注 0票数 2

我想充分利用API21中新增的NestedScroll特性。我的布局非常简单:

HorizontalScrollView LinearLayout (明显水平)常规视图HorizontalScrollView TextView

默认情况下,nestedScrollEnabled为false。所以我在xml中为我想要优先于根HorizontalScrollView滚动的子对象(例如内部的HorizontalScrollView)启用了它。因此,它不会做任何事情。只有顶部的滚动视图能够滚动,内部的滚动视图似乎根本看不到任何滚动的触摸事件。

代码语言:javascript
复制
<?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,我不明白...谢谢

EN

回答 2

Stack Overflow用户

发布于 2016-12-23 03:26:27

您将需要使用onTouchListener拦截触摸:

代码语言:javascript
复制
// 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...

代码语言:javascript
复制
  <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>

票数 0
EN

Stack Overflow用户

发布于 2016-12-23 03:18:35

尝试对子LinearLayout使用方向为水平的NestedScrollView。

NestedScrollView就像ScrollView一样,但它支持在新旧版本的安卓上同时充当嵌套滚动父项和子项。默认情况下,嵌套滚动处于启用状态。

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

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41289957

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档