繁体   English   中英

当回收器视图放在nestedscrollview中时,PagedListAdapter获取所有项目

[英]PagedListAdapter fetching all the items when recycler view is placed inside nestedscrollview

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

我在android-architecture-components的GitHub repo中看到了这个问题,发现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_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:layout_margin="16dp"
    tools:context=".ui.Home">

<androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <LinearLayout
            android:focusable="true"
            android:focusableInTouchMode="true"
            android:layout_width="0px"
            android:layout_height="0px"/>


    <EditText.../>

    <ImageView.../>

    <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            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_width="match_parent"
            android:layout_height="wrap_content"
            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>

任何人都可以告诉我还能做什么?

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

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

我刚刚移动了整个视图,我希望在回收器视图中的回收器视图上方显示,即将该视图作为Recycler View的标题。

布局蓝图

在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)
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM