Jevon
Jevon

Reputation: 313

How to implement endless list with a RecyclerView inside a fragment

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.

Upvotes: 0

Views: 188

Answers (1)

Biscuit
Biscuit

Reputation: 5257

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

Upvotes: 1

Related Questions