How to use a simple onscroll listener on a recyclerview that is inside a fragment

独自空忆成欢 提交于 2020-08-10 20:29:25

问题


Ive been looking all over the internet and cant find the answer that works for me.

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.

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

    })`

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".


回答1:


For infinite scrolling I would recommend you to use this library provided by Google.



来源:https://stackoverflow.com/questions/62645123/how-to-use-a-simple-onscroll-listener-on-a-recyclerview-that-is-inside-a-fragmen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!