当回收器视图放置在嵌套滚动视图中时,PagedListAdapter 获取所有项目

Posted

技术标签:

【中文标题】当回收器视图放置在嵌套滚动视图中时,PagedListAdapter 获取所有项目【英文标题】:PagedListAdapter fetching all the items when recycler view is placed inside nestedscrollview 【发布时间】:2019-09-29 20:28:32 【问题描述】:

我使用的是 NestedScrollView,在其中,我有一些编辑文本和按钮,然后是 RecyclerView。当我打开活动时,所有项目都开始使用 PageKeyedDataSource 从服务器获取,并且 RecyclerView 滚动非常缓慢,最后应用程序显示 ANR。

我在 android-architecture-components 的 GitHub 存储库中查看了这个问题,发现 SmartNestedScrollView 可以解决这个问题,但是当我使用 SmartNestedScrollView 时,我的回收站视图只有一小部分会滚动。

https://github.com/googlesamples/android-architecture-components/issues/215

LayoutFile.xml

<?xml version="1.0" encoding="utf-8"?>
<com.magtappofficial.app.utilities.SmartNestedScrollView
    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_
    android:layout_
    android:fillViewport="true"
    android:layout_margin="16dp"
    tools:context=".ui.Home">

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_
        android:layout_>

    <LinearLayout
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_
            android:layout_/>


    <EditText.../>

    <ImageView.../>

    <ProgressBar
            android:layout_
            android:layout_
            android:id="@+id/newsProgress"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toBottomOf="@+id/exclusiveForYou"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"/>

    <androidx.recyclerview.widget.RecyclerView
            android:layout_
            android:layout_
            android:id="@+id/newsRecycler"
            android:layout_marginTop="16dp"
            app:layout_constraintTop_toBottomOf="@+id/exclusiveForYou"
            app:layout_constraintEnd_toEndOf="parent"
            android:layout_marginEnd="8dp"
            android:layout_marginRight="8dp"
            app:layout_constraintStart_toStartOf="parent"
            android:layout_marginLeft="8dp"
            android:layout_marginStart="8dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

</com.magtappofficial.app.utilities.SmartNestedScrollView>

谁能告诉我我还能做什么?

【问题讨论】:

人们只是来把每一个他们不知道答案的合理问题降级,这很好,但这是一些重要问题还没有解决方案的主要原因。 继续努力,伙计们 你找到解决办法了吗? @SaurabhThorat,好吧。 【参考方案1】:

注意:这不是最佳做法,只是一种解决方法,如果有人有更好的答案,请分享。

对于那些仍在寻找答案的人,我有一个简单的解决方法。

我刚刚将要显示的整个视图移动到回收站视图内的回收站视图上方,即将该视图作为回收站视图的标题。

在 Recycler 视图内部:

const val HEADER = 0
const val REAL_ITEM = 1

getItemViewType 方法将视图类型返回给 onCreateViewHolder。

override fun getItemViewType(position: Int): Int 
        return when (position) 
            0 -> HEADER
            else -> REAL_ITEM
        
    

onCreateViewHolder 方法内部:

override fun onCreateViewHolder(p0: ViewGroup, p1: Int): NewsViewHolder 
        val view = when (p1) 
            HEADER -> 
                LayoutInflater.from(ctx).inflate(R.layout.rv_header, p0, false)
            
            else -> LayoutInflater.from(ctx).inflate(R.layout.xyz, p0, false)
        
        return SomeViewHolder(view)
    

在 onBindViewHolder 中,您可以根据视图需要获取视图类型和绑定数据。

override fun onBindViewHolder(p0: NewsViewHolder, p1: Int) 
        val viewType = getItemViewType(p1)

【讨论】:

正确的做法是使用坐标布局。 你有那个样品吗? 不,我必须创建它。 @SunnyAgarwal

以上是关于当回收器视图放置在嵌套滚动视图中时,PagedListAdapter 获取所有项目的主要内容,如果未能解决你的问题,请参考以下文章

在 FireTv 应用程序的嵌套回收器视图中焦点切换到下一行

无法使用回收视图在 ScrollVIew/NestedScrollView 内滚动 ViewPager

Recyclerview 滚动在嵌套滚动视图中的片段中不起作用

当新行添加到 UITextView 实例中时,如何向上滚动视图?

仅在不在视图中时滚动到元素 - jQuery

当节点不在视图中时如何删除它?