如何在片段内使用RecyclerView实现无尽列表 [英] How to implement endless list with a RecyclerView inside a fragment

查看:63
本文介绍了如何在片段内使用RecyclerView实现无尽列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一旦用户在底部附近滚动,如何向我的RecyclerView列表中加载更多内容?

How do i load more to my RecyclerView list once the user scrolls near the bottom?

我看过了堆栈溢出页面,但我仍在努力尝试从将Kotlin的答案纳入我的代码.

I have had a look at this stack overflow page but I'm still struggling with trying to implement the code from the Kotlin answer into my code.

要获得像你们一样的更好的上下文ID,请查看我之前的问题,这向您展示了我是如何创建我的android studio中的片段(通过单击片段(列表)并选择一个空片段来完成).

For better context id like you guys to have a look at my previous question, this shows you how i created my fragment in android studio (it was done by clicking on fragment (list) and choosing an empty fragment).

在我的onCreateView里面,我有这段代码

inside of my onCreateView i have this piece of code

view.list.addOnScrollListener(object : RecyclerView.OnScrollListener() {


            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)

                val contentHeight = view.list.height - (view.list.paddingTop + view.list.paddingBottom)

                val distanceToBottom = view.list.computeVerticalScrollRange() - view.list.computeVerticalScrollOffset() - contentHeight

                val metrics = DisplayMetrics()

                activity!!.windowManager.getDefaultDisplay().getMetrics(metrics)
                //val Measuredwidth = metrics.widthPixels
                val Measuredheight = metrics.heightPixels

                // everything looks like its in pixels. It is important that everything is one unit type to ensure accurate calculations
                // Once this is all finished i will ask a question in stack overflow on how to ensure its one unit type


                // using the screen height and timesing it by 2 to know when to load more data
                if (Measuredheight * 2 > distanceToBottom) {

                    Toast.makeText(view.list.context, "Scrolling $distanceToBottom, this is the screen height excluding the nav bar in pixels " + Measuredheight + " more content needs to be loaded", Toast.LENGTH_SHORT).show()

                    // Load more function to be placed here
                }


            }


        })

----------------------编辑---------------------

我一直在研究分页库,并且遇到过本教程,但是我仍然很难将代码放在一个片段中.这是本教程的源代码,因此这是代码我已经在我的项目中使用过.

Ive been looking into the paging library and I've come across this tutorial, however I'm still having trouble putting the code in a fragment. This is the source code for the tutorial, so this is the code i have used in my project.

我认为我应该做的是在我的onCreateView内部,我必须放置这段代码

What I think I'm supposed to do is inside of my onCreateView, i have to place this piece of code

                adapter = UserAdapter()

                val itemViewModel = ViewModelProviders.of(view.list.context)
                    .get(UserViewModel::class.java)

                itemViewModel.userPagedList.observe(view.list.context, Observer {
                    adapter.submitList(it)
                })

                list.adapter = adapter

但是,我遇到了错误,某些代码似乎已被弃用.有人可以告诉我我需要使用的代码,以便在片段中包含RecyclerView,以便我接受他们的回答.

However, i am getting errors and some of the code seems to be deprecated. Can somebody please show me the code that i need to use in order to have a RecyclerView inside of a fragment please so that i can accept their answer.

推荐答案

您正在寻找的是名为 Paging (它是Android体系结构组件的一部分).

What you're looking for is a Library called Paging it is part of Android Architecture Component.

这是 codelab ,请注意,分页3已 https://developer.android.com/topic/libraries/建筑/分页/v3-概述

Here is the codelab and note that the Paging 3 is out https://developer.android.com/topic/libraries/architecture/paging/v3-overview

这篇关于如何在片段内使用RecyclerView实现无尽列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆