【问题标题】:Problem with scrolling in Recyclerview. Scroll is not smooth在 Recyclerview 中滚动出现问题。滚动不流畅
【发布时间】:2020-07-19 10:54:48
【问题描述】:

这是我的代码-

<?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"
    tools:context=".MainActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
        android:id="@+id/frame1"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:text="Recruitment Responses"
        android:gravity="center"
        android:textSize="30sp"
        android:textColor="#fff"
        android:background="@drawable/bg_rounded1"
        />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:padding="16dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/frame1"
        android:background="#f9fafc"/>
    </RelativeLayout>
</ScrollView>
</RelativeLayout>

滚动不流畅,很烦人,有什么办法可以改善吗?我希望 textview 在向上滚动时与 Recycler View 一起上升,这就是为什么我将它们都嵌套在 ScrollView 中的原因,也许这就是造成问题的原因!

【问题讨论】:

  • 嵌套在同一方向(水平/垂直)滚动的视图通常不是一个好主意。
  • 尝试使用 CoordinatorLayout、CollapsibleToolbar 来实现该行为。如果您有大量项目,则使用高度设置为 wrap_content 的回收站视图不是一个好主意。

标签: java android xml android-recyclerview android-scrollview


【解决方案1】:

添加 而不是 ScrollView 可以解决所有问题。

<?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"
    tools:context=".MainActivity">

<androidx.core.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    <TextView
        android:id="@+id/frame1"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:text="Recruitment Responses"
        android:gravity="center"
        android:textSize="30sp"
        android:textColor="#fff"
        android:background="@drawable/bg_rounded1"
        />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:padding="16dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/frame1"
        android:background="#f9fafc"/>
    </RelativeLayout>
</androidx.core.widget.NestedScrollView>
</RelativeLayout>

【讨论】:

    【解决方案2】:

    试试这个:

    recyclerView.setScrollView(true);
    

    这可以帮助您使recyclerView滚动视图平滑。

    【讨论】:

    • 这里是documentationandroidx.recyclerview.widget.RecyclerView。你在那里找到任何setScrollView() 方法吗?
    • @Wrichik Basu 这是秘密
    【解决方案3】:

    在你的活动中试试这个:

    recyclerView.setNestedScrollingEnabled(false);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 2014-12-06
      • 2018-01-30
      相关资源
      最近更新 更多