0

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

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.

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

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
                }


            }


        })

----------------------Edit---------------------

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.

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

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.

Jevon
  • 295
  • 2
  • 13
  • I would suggest Paging for this. Here, look at this [Kotlin based article](https://www.raywenderlich.com/6948-paging-library-for-android-with-kotlin-creating-infinite-lists) to implement infinite list. – Lalit Fauzdar Jul 11 '20 at 08:57
  • Ive been looking into paging and found a tutorial with some source code however, i cant seem to figure out how to put it inside a fragment. – Jevon Jul 20 '20 at 03:01

1 Answers1

1

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

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

Biscuit
  • 4,840
  • 4
  • 26
  • 54
  • Paging 3 is in alpha? what does that mean? should i use that version? – Jevon Jul 15 '20 at 01:49
  • You can use Paging 2 in the mean time if you're not confortable with putting an `alpha` in your application [should I use alpha ?](https://www.reddit.com/r/androiddev/comments/bnw7si/should_i_use_alpha_versions_of_androidx_libraries/) – Biscuit Jul 15 '20 at 06:35
  • Ive been looking into paging and found a tutorial with some source code however, i cant seem to figure out how to put it inside a fragment. – Jevon Jul 20 '20 at 03:01