【问题标题】:RecyclerView not working inside a ScrollView properlyRecyclerView 在 ScrollView 中无法正常工作
【发布时间】:2021-10-28 16:06:17
【问题描述】:

在我将第二个 RecyclerView 添加到片段之前,我的应用程序和 RecyclerView 工作正常。由于我添加了第二个片段,因此我在片段中添加了ScrollView。然后我在recyclerview中遇到了一些滚动问题,它滚动不顺畅。在浏览了包括 StackOverflow 在内的很多在线文章后,我将 ScrollView 更改为 androidx.core.widget.NestedScrollView。这解决了滚动问题,但出现了一些主要问题。 RecyclerView 中的项目加载需要更多时间,当我点击顶部菜单栏上的搜索按钮时,扩展搜索字段需要很长时间,有时应用程序会显示“无响应”消息。有些文章说添加.setNestedScrollingEnabled=false,我在片段的onCreateView() 中添加如下。但我仍然面临这个问题。

我正在使用kotlin

binding.rv_home_items.isNestedScrollingEnabled=false
binding.rv_home_categories.isNestedScrollingEnabled=false

下面是xml

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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:background="@color/colorOffWhite"
        android:orientation="vertical"
        tools:context=".ui.fragments.HomeFragment">
    
        <FrameLayout
            android:id="@+id/framelayout_category"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/rv_home_categories"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="10dp"
                android:orientation="horizontal"
                android:paddingBottom="15dp" />
    
            <ImageButton
                android:id="@+id/ibutton_show_category"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|right"
                android:layout_margin="5dp"
                android:background="@drawable/ic_show_category" />
    
        </FrameLayout>
    
        <androidx.core.widget.NestedScrollView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/framelayout_category">
    
            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <com.denzcoskun.imageslider.ImageSlider
                    android:id="@+id/image_slider"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:visibility="gone"
                    app:iss_auto_cycle="true"
                    app:iss_corner_radius="5"
                    app:iss_delay="0"
                    app:iss_error_image="@color/colorDarkGrey"
                    app:iss_period="2500"
                    app:iss_placeholder="@color/colorDarkGrey"
                    app:iss_selected_dot="@drawable/default_selected_dot"
                    app:iss_unselected_dot="@drawable/default_unselected_dot"
                    tools:visibility="visible" />
    
                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/rv_home_items"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/image_slider"
                    android:layout_marginBottom="50dp" />
            </RelativeLayout>

        </androidx.core.widget.NestedScrollView>
    
    </RelativeLayout>

我完全卡在我的项目上,非常感谢任何帮助

编辑:

当我启动应用程序时,它会在几秒钟内显示应用程序没有响应的消息。

【问题讨论】:

  • bindingView.recyclerView.isNestedScrollingEnabled = false bindingView.recyclerView.isFocusableInTouchMode = true
  • 我为onCreateView() 中的recyclerViews 添加了这个,但它没有解决问题。还有其他建议吗?我应该在onCreateView() 中添加这个吗?
  • 我应该在哪里添加bindingView.recyclerView.isNestedScrollingEnabled = false,我在fragmentonCreateView() 中添加了这个。对吗?
  • 我完全卡在我的项目上,非常感谢任何帮助

标签: android kotlin android-recyclerview android-scrollview android-nestedscrollview


【解决方案1】:

如果您有多个RecyclerView,甚至它们都具有相同的滚动方向,似乎ConcatAdapter 可能是首选解决方案。

ConcatAdapter 可以将多个适配器按顺序组合成一个RecyclerView

【讨论】:

  • RecyclerView 的滚动方向不同。您能否提出任何其他解决方案来解决此问题?
  • 我相信ConcatAdapter 也可以为RecyclerView 使用不同的滚动方向
  • 感谢您的回复,下次更新我的应用程序时我会尝试一下,因为它需要对代码进行大量更改才能实现ConcatAdapter。但现在,我需要一个修复/解决方案来解决我当前布局的问题。