【问题标题】:RecyclerView does not scroll while searchingRecyclerView 搜索时不滚动
【发布时间】:2018-02-23 12:02:33
【问题描述】:

我有一个 recyclerview 与工具栏中的过滤搜索相关联。当我开始搜索时,recyclerview 显示的项目数远远大于屏幕大小。但是当我尝试通过滑动查看屏幕上的元素来向上滚动时,没有任何响应。我认为像 ListViews 这样的 RecyclerViews 总是支持滚动。任何想法为什么我的 RecyclerView 不滚动?

这是包含在我的主布局中的工具栏、搜索和回收器视图的协调器布局(我为格式化道歉,但插入代码小部件无法正常工作):

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"                                                 
    xmlns:app="http://schemas.android.com/apk/res-auto"                                                 
    xmlns:tools="http://schemas.android.com/tools"                                                 
    android:layout_width="match_parent"                                                 
    android:layout_height="match_parent"                                                 
    android:fitsSystemWindows="true"                                                 
    tools:context="com.lampreynetworks.cpa.CPA_Activity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar_search_id"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorToolbarBackground"
            android:theme="@style/AppThemeDark"
            app:contentInsetEnd="0dp"
            app:contentInsetStart="0dp">
        </android.support.v7.widget.Toolbar>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/card_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

主要布局如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:app="http://schemas.android.com/apk/res-auto"
          xmlns:tools="http://schemas.android.com/tools"
          android:id="@+id/base_layout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true"
          tools:context="com.lampreynetworks.cpa.CPA_Activity"
          android:orientation="vertical">

<include
    android:id="@+id/toolbar_search"
    layout="@layout/toolbar_search_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

<android.support.v4.widget.DrawerLayout 
  xmlns:android="http://schemas.android.com/apk/res/android"                                            
  xmlns:app="http://schemas.android.com/apk/res-auto"                                            
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/drawer_layout"
  android:layout_width="match_parent"                                            
  android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            android:id="@+id/borderLayout"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_margin="4dp"
            android:background="@drawable/text_view_border"
            android:orientation="vertical">

            <TextView
                android:id="@+id/text_connect_status"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="6dp"
                android:text="some text"
                android:textColor="@color/orange"
                android:textStyle="bold"/>
        </RelativeLayout>

        <ListView
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/borderLayout"
            android:layout_marginLeft="4dp"
            android:layout_marginStart="4dp"
            android:choiceMode="none"
            android:stackFromBottom="false">
        </ListView>

    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:menu="@menu/nav_drawer_menu"/>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>

所有其他方面似乎都还可以。该列表以交互方式响应用户输入的文本,并且在recyclerview 项目上的单击操作能够获得搜索到的信息。但是这个列表永远不会滚动。我尝试了许多从相关问题中获取提示的方法(在协调器布局中添加各种类型的滚动布局、在各个位置调用与滚动相关的属性等),但行为保持不变。我一定是在做一些非常愚蠢的事情来扼杀这种基本行为。

【问题讨论】:

    标签: android search android-recyclerview android-coordinatorlayout


    【解决方案1】:

    尝试将RecyclerView 移到AppBarLayout 之外,如下所示:

    <android.support.design.widget.CoordinatorLayout                                           
        android:layout_width="match_parent"                                                 
        android:layout_height="match_parent"                                                 
        android:fitsSystemWindows="true"                                                 
        tools:context="com.lampreynetworks.cpa.CPA_Activity">
    
        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar_search_id"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorToolbarBackground"
                android:theme="@style/AppThemeDark"
                app:contentInsetEnd="0dp"
                app:contentInsetStart="0dp">
            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.AppBarLayout>
    
        <android.support.v7.widget.RecyclerView
            android:id="@+id/card_recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
        </android.support.design.widget.CoordinatorLayout>
    

    【讨论】:

    • 哇!那解决了它。必须在 RecyclerView android:layout_marginBottom="?actionBarSize" 上添加较低的边距,这样最后的项目就不会与主屏幕重叠。现在我对 appbarlayout 中的 recyclerview 做了什么有了更好的理解......
    【解决方案2】:
    change recyclerview height to wrap_content.
    
    <android.support.v7.widget.RecyclerView
                android:id="@+id/card_recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:scrollbars="vertical"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    

    将您的回收站视图从 AppBarLayout 中取出。

    【讨论】:

    • 是的,我试过了,但它仍然不能滚动。当 RecyclerView 被“附加”到搜索时,可能无法滚动?如果您过滤得足够多,您将只有一个条目! (我完全不喜欢无法滚动的想法;滚动一点比在手机上搜索时输入文本要好得多!
    猜你喜欢
    • 2021-08-13
    • 1970-01-01
    • 2020-07-09
    • 2018-08-04
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    相关资源
    最近更新 更多