PagedList.Config 使用recycleview在中间加载列表

Posted

技术标签:

【中文标题】PagedList.Config 使用recycleview在中间加载列表【英文标题】:PagedList.Config loads list in the middle with recycleview 【发布时间】:2021-02-12 12:03:40 【问题描述】:

我的 RecyclerView 加载在列表的中间,而不是最前面的项目。该列表应加载最顶部的项目。在这种情况下,它是“最新”的帖子。我已将反向布局设置为 true 并且有效。

我的回收站视图

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    tools:viewBindingIgnore="true">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/postsList"
        android:layout_
        android:layout_
        android:clipToPadding="false"
        android:padding="5dp"
        tools:listitem="@layout/post_item" />

</FrameLayout>

我的帖子列表片段

 // Set up FirebaseRecyclerAdapter with the Query
        val postsQuery = database.child(Globals().POSTS_ROOT_PATH)

        // This configuration comes from the Paging Support Library
        // https://developer.android.com/reference/androidx/paging/PagedList.Config
        val config = PagedList.Config.Builder()
            .setEnablePlaceholders(false)
            .setPrefetchDistance(5)
            .setPageSize(10)
            .build()

        // The options for the adapter combine the paging configuration with query information
        // and application-specific options for lifecycle, etc.
        val options = DatabasePagingOptions.Builder<Post>()
            .setLifecycleOwner(this)
            .setQuery(postsQuery, config, Post::class.java)
            .build()

        adapter = object : FirebaseRecyclerPagingAdapter<Post, PostViewHolder>(options) 

            override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): PostViewHolder 
                val inflater = LayoutInflater.from(viewGroup.context)

                return PostViewHolder(inflater.inflate(R.layout.post_item, viewGroup, false))
            

            override fun onBindViewHolder(viewHolder: PostViewHolder, position: Int, model: Post) 
                val postRef = getRef(position)
                // Set click listener for the whole post view
                val postKey = postRef.key
//                Log.d("PostListFragment", "post key id is: " + postKey)
                viewHolder.itemView.setOnClickListener 
                    // Launch PostDetailActivity
                    val intent = Intent(activity, PostDetailActivity::class.java)
                    intent.putExtra(PostDetailActivity.EXTRA_POST_KEY, postKey)
                    startActivity(intent)
                

  
            

            override fun onLoadingStateChanged(state: LoadingState) 
                when (state) 
                    LoadingState.LOADING_INITIAL -> 
                        Log.d(TAG, "Initial data loading")
                    
                    LoadingState.LOADING_MORE -> 

                    
                    LoadingState.LOADED -> 

                    
                    LoadingState.FINISHED -> 
                        Log.d(TAG, "All data loaded.")
                    
                    LoadingState.ERROR -> 
                        Log.d(TAG, "Error occured while loading data")
                    
                
            

            override fun onError(databaseError: DatabaseError) 
                super.onError(databaseError)
                databaseError.toException().printStackTrace()
            
        

        // Set up Layout Manager, reverse layout
        manager = LinearLayoutManager(activity)
        manager.reverseLayout = true
        manager.stackFromEnd = true
        recyclerList.layoutManager = manager

        recyclerList.adapter = adapter
    

当我将 setPageSize() 设置为一个非常大的数字时,它会加载最顶部的项目。我需要做什么才能始终首先加载最顶部的列表项?

谢谢

编辑: post_item.xml

<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:layout_margin="5dp"
    tools:viewBindingIgnore="true">

    <RelativeLayout
        android:layout_
        android:layout_>

        <include
            android:id="@+id/postTextLayout"
            layout="@layout/post_text_include"
            android:layout_
            android:layout_
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginLeft="2dp"
            android:layout_marginTop="2dp"
            android:layout_marginStart="2dp" />

        <include
            android:id="@+id/postThumbnailImg"
            layout="@layout/post_video_thumbnail_include"
            android:layout_
            android:layout_
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/postTextLayout"
            android:layout_alignParentStart="true" />

        <include
            android:id="@+id/postAuthorLayout"
            layout="@layout/post_author_thumbnail_include"
            android:layout_
            android:layout_
            android:layout_below="@id/postThumbnailImg"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true" />
    </RelativeLayout>

</com.google.android.material.card.MaterialCardView>

子布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    android:orientation="vertical">

    <TextView
        android:id="@+id/postTitle"
        style="@style/TextAppearance.AppCompat.Medium"
        android:layout_
        android:layout_
        android:ellipsize="end"
        android:maxLines="1"
        android:textStyle="bold"
        android:textColor="@color/colorPrimaryDark"
        tools:text="My First Post"
        android:paddingStart="5dp"
        android:paddingEnd="5dp" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    tools:viewBindingIgnore="true">

    <RelativeLayout
        android:layout_
        android:layout_ />

    <ImageView
        android:id="@+id/videoThumbnailImg"
        android:layout_
        android:layout_
        android:src="@drawable/main_background" />
    />

    <RelativeLayout
        android:layout_
        android:layout_>

        <ImageView
            android:id="@+id/playButton"
            android:layout_
            android:layout_
            android:scaleType="centerCrop"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:src="@drawable/ic_baseline_play_arrow_24" />
    </RelativeLayout>
</FrameLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_
    android:layout_
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:gravity="center_vertical"
    android:orientation="horizontal">

    <androidx.cardview.widget.CardView
        android:layout_
        android:layout_
        app:cardCornerRadius="20dp">

        <ImageView
            android:id="@+id/postAuthorPhoto"
            android:layout_
            android:layout_
            android:scaleType="centerCrop"
            android:src="@drawable/ic_person_outline_40" />

    </androidx.cardview.widget.CardView>

    <TextView
        android:id="@+id/postAuthor"
        style="@style/Base.TextAppearance.AppCompat.Small"
        android:layout_
        android:layout_
        android:layout_marginLeft="10dp"
        android:gravity="center_vertical"
        android:textColor="@color/colorPrimaryDark"
        tools:text="someauthor@email.com" />

</LinearLayout>

【问题讨论】:

分享你的项目的xml @PriyankaRajput 添加了 post_item.xml。 也分享子布局 @PriyankaRajput 添加了 【参考方案1】:

您需要为子布局设置高度 wrap_content 而不是 match_parent。

【讨论】:

我加了,结果还是一样。

以上是关于PagedList.Config 使用recycleview在中间加载列表的主要内容,如果未能解决你的问题,请参考以下文章

使用 #recycle() 后应回收类型化数组

Android Honeycomb 中的 Bitmap#recycle() 实际上做了啥?

如何在 Fragment 中使用 Recycler View [重复]

recycler

在Recycler视图中使用数组列表

Oracle Recycle Bin