如何在片段内的recyclerview上使用简单的onscroll侦听器 [英] How to use a simple onscroll listener on a recyclerview that is inside a fragment
问题描述
我一直在互联网上四处寻找,找不到适合我的答案.
Ive been looking all over the internet and cant find the answer that works for me.
我正在尝试使用无限滚动条创建片段(就像在Twitter,Facebook,Instagram,Tumblr等社交网络应用程序上一样),研究表明,回收站视图是最好的方法.此图像向您展示了我是如何做到的.文件->新->片段->片段(列表)
I am trying to create a fragment with an infinite scroller (just like on social network apps like twitter, facebook, instagram, tumblr etc ) and research suggests that a recycler view is the best way. This image show you how i have done it. File -> new -> Fragment -> Fragment (List)
如果我是正确的,那么下一步就是创建一个滚动侦听器,这样我就知道用户何时滚动到底部,就像我加载更多数据一样.
If I'm correct, the next step now is to create an on scroll listener so that i know when the user has scrolled to the bottom as that would be when i load more data.
如果到目前为止,我还不明白,我认为它位于MyContentRecyclerViewAdapter文件中我必须弄清楚如何将我的下一部分代码放置在scrollListener上
If i have understood things so far, i think its inside the file MyContentRecyclerViewAdapter here that i have to figure out how where to put my next bit of code for the scrollListener
` list.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (dy > 0) {
Toast.makeText(list.context,"Scrolling up", Toast.LENGTH_SHORT).show()
// Scrolling up
} else {
Toast.makeText(list.context,"Scrolling down", Toast.LENGTH_SHORT).show()
// Scrolling down
}
}
})`
回复Lalit Fauazdar.这就是我现在得到的,但是由于某种原因这导致我的应用程序崩溃.即使我的androidx.recyclerview.widget.RecyclerView
具有android:id="@+id/list"
,列表由于某些原因仍显示为空值.
Replying to Lalit Fauazdar. This is what I've got right now however for some reason this causes my app to crash. list appears to have a null value for some reason even though my androidx.recyclerview.widget.RecyclerView
has android:id="@+id/list"
.
推荐答案
对于无限滚动,我建议您使用此由Google提供的库.
For infinite scrolling I would recommend you to use this library provided by Google.
这篇关于如何在片段内的recyclerview上使用简单的onscroll侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!