【问题标题】:Android- Sliding conflict between scrollview and horizontal recycler viewAndroid-滚动视图和水平回收器视图之间的滑动冲突
【发布时间】:2018-04-19 07:07:54
【问题描述】:

我遇到了滚动视图和水平回收器视图之间的滚动问题。在片段中,有一个滚动视图包装了 2 个水平回收器视图。有时想要滑动回收器视图但系统也无法检测到,它会触发滚动视图向上或向下滑动。滚动回收站视图对我来说非常困难。 下图

    <ScrollView
        android:descendantFocusability="beforeDescendants"
        android:id="@+id/scrollView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">


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


            <ImageView
                android:layout_width="125dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:src="@drawable/logo" />

            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@color/gray" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="20dp"
                android:layout_weight="1"
                android:fontFamily="@font/roboto_condensed_regular"
                android:text="Journals"
                android:textColor="@color/v2_text_color"
                android:textSize="18sp"
                android:textStyle="bold" />

            <View
                android:layout_width="25dp"
                android:layout_height="1dp"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="5dp"
                android:background="@color/v2_text_color" />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:descendantFocusability="afterDescendants"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:scrollbars="horizontal" />


            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="15dp"
                android:fontFamily="@font/roboto_condensed_regular"
                android:text="Editor's Pick"
                android:textColor="@color/v2_text_color"
                android:textSize="18sp"
                android:textStyle="bold" />

            <View
                android:layout_width="25dp"
                android:layout_height="1dp"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="5dp"
                android:background="@color/v2_text_color" />


            <android.support.v7.widget.RecyclerView
                android:id="@+id/editorRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:descendantFocusability="afterDescendants"
                android:scrollbars="horizontal" />



            <Button
                android:id="@+id/tv_showMore"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="110dp"
                android:layout_height="35dp"
                android:layout_gravity="center"
                android:layout_marginBottom="15dp"
                android:layout_marginTop="30dp"
                android:background="@drawable/white_rounded_button"
                android:text="Show All"
                android:textAllCaps="false"
                android:textColor="#696969" />


        </LinearLayout>


    </ScrollView>

【问题讨论】:

    标签: android scroll android-recyclerview scrollview conflict


    【解决方案1】:

    使用 android.support.v4.widget.NestedScrollView 代替 ScrollView

    并使用 android:nestedScrollingEnabled="false" 到您的 RecyclerView

    像这样改变你的布局

    <?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:fitsSystemWindows="true"
        android:descendantFocusability="beforeDescendants">
    
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
    
    
            <ImageView
                android:layout_width="125dp"
                android:layout_height="50dp"
                android:layout_gravity="center"
                android:layout_marginTop="5dp"
                android:src="@drawable/logo" />
    
            <View
                android:layout_width="match_parent"
                android:layout_height="1dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:background="@color/gray" />
    
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="20dp"
                android:layout_weight="1"
                android:fontFamily="@font/roboto_condensed_regular"
                android:text="Journals"
                android:textColor="@color/v2_text_color"
                android:textSize="18sp"
                android:textStyle="bold" />
    
            <View
                android:layout_width="25dp"
                android:layout_height="1dp"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="5dp"
                android:background="@color/v2_text_color" />
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/recyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:descendantFocusability="afterDescendants"
                android:nestedScrollingEnabled="false"
                android:scrollbars="horizontal" />
    
    
            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="15dp"
                android:fontFamily="@font/roboto_condensed_regular"
                android:text="Editor's Pick"
                android:textColor="@color/v2_text_color"
                android:textSize="18sp"
                android:textStyle="bold" />
    
            <View
                android:layout_width="25dp"
                android:layout_height="1dp"
                android:layout_marginLeft="16dp"
                android:layout_marginTop="5dp"
                android:background="@color/v2_text_color" />
    
    
            <android.support.v7.widget.RecyclerView
                android:id="@+id/editorRecyclerView"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="10dp"
                android:layout_marginTop="10dp"
                android:descendantFocusability="afterDescendants"
                android:nestedScrollingEnabled="false"
                android:scrollbars="horizontal" />
    
    
            <Button
                android:id="@+id/tv_showMore"
                style="?android:attr/borderlessButtonStyle"
                android:layout_width="110dp"
                android:layout_height="35dp"
                android:layout_gravity="center"
                android:layout_marginBottom="15dp"
                android:layout_marginTop="30dp"
                android:background="@drawable/white_rounded_button"
                android:text="Show All"
                android:textAllCaps="false"
                android:textColor="#696969" />
    
    
        </LinearLayout>
    
    
    </android.support.v4.widget.NestedScrollView>
    

    【讨论】:

    • 它的后代对焦会影响吗?
    【解决方案2】:

    在滚动视图/嵌套滚动视图中使用 Recyclerview 时会出现很多问题。视图不会被回收,会影响应用程序的整体性能(如果您有大量数据) 如果您尝试实现多个水平滑动并能够垂直滑动布局,则最佳做法是使用“Nested Recyclerview”

    基本上它的作用是“我们将有一个垂直的recyclerview,垂直recyclerview的每个项目都将是一个水平的recyclerview” 你可以参考这个project

    我遵循这个模式并在多个项目中使用过它

    希望有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多