A
A
Andrey Goroshko2019-02-22 09:42:24
JavaScript
Andrey Goroshko, 2019-02-22 09:42:24

How to implement an action when the recyclerView reaches the top of the list?

I have a recyclerView in my app and I want to page load data into it. Data is loaded from the server by changing the offset and filling in the list and adapter. I want to make an analogue of the library from the Google pagination library. For a long time I tried to use it in the application, but I could not bind the adapter to it, so I decided to implement this function myself. From the good - when the bottom of the list is reached, everything works correctly and without fail, but when the top level of the list is reached, I can’t think of anything. Here's how the loading is implemented when the bottom of the list is reached. There is an interface:

public interface OnBottomReachedListener {
    void onBottomReached(int position);
}

here is how it is registered in the adapter:
void setOnBottomReachedListener(OnBottomReachedListener onBottomReachedListener) {
        this.onBottomReachedListener = onBottomReachedListener;
    }

in onBindViewHolder:
if (position == getItemCount() - 1 && position != getItemCount()) {
            onBottomReachedListener.onBottomReached(position);
        }

and here's how I load the data in the activity:
((ListAdapter) adapter).setOnBottomReachedListener(new OnBottomReachedListener() {
                            @Override
                            public void onBottomReached(int position) {
                                if (next_url != null) {
                                    getMessages(type, a_token, Integer.parseInt(Uri.parse(next_url).getQueryParameter("offset")));
                                }
                            }
                        });

this code works flawlessly, but when it reaches the top of the list, I couldn't come up with anything working. I thought at first to add a scrollListener, but it works somehow strangely, it works twice for some reason. The logic is this - at first the list is formed and the first position is always 0 after the list is formed, then we scroll to the very bottom of the list, load the data (up to this point everything is fine), then we have a list with new data, but there is already a URL for going to the previous "page" with data, but we do not load anything after the list is formed, then we, for example, scroll to the middle of the list and return to the top of the list again, drag the list further down and at this moment you can send a request. It's like an endless list or something like that. I can't figure out how to implement this, I've been sitting for a week now.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
CyberHost, 2016-09-12
@night_breeze

For example like this

R
Rasul Kakushev, 2019-07-04
@Raserad

You can make an interface with the onTopReached method.
Next, make a link to this interface in the adapter. In the onBindViewHolder method, check the index of the current element (usually the nuposition variable). If position == 0 then you have reached the top of the list and at that moment you can call the onTopReached method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question