A
A
Andrey Goroshko2018-12-26 13:34:51
Android
Andrey Goroshko, 2018-12-26 13:34:51

How to check if the recyclerView has reached the top of the list?

In my application I have a list created on the basis of recyclerView which has its own adapter. This adapter is filled with data that it receives from the server, let's take a list of messages for example. Currently, I'm trying to add new items to the list when reaching the bottom or top level of the list (similar to the Gmail message list). I read about the library that is used in Gmail, but I did not understand how it can be used in my situation. Now I managed to add list elements when the bottom border of the list is reached, and everything seems to be working fine. But I won't be able to do the same using the experience I've gained from reaching the bottom of the list. First I created an interface:

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

further in the adapter I create a function:
void setOnBottomReachedListener(OnBottomReachedListener onBottomReachedListener) {
        this.onBottomReachedListener = onBottomReachedListener;
    }

and then I insert a check into onBindViewHolder, which does not always work correctly, but in general everything works:
if (position == getItemCount() - 1) {
            onBottomReachedListener.onBottomReached(position);
        }

In the activity, after creating and populating the adapter, I added a listener:
((ListAdapter) adapter).setOnBottomReachedListener(new OnBottomReachedListener() {
                            @Override
                            public void onBottomReached(int position) {
                                getMessages(type, a_token, offset + 50);
                            }
                        });

i can't figure out exactly how i should reverse the check so that it fires and when i reach the top of the list. I hope for your help and useful advice. Thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
illuzor, 2018-12-26
@iLLuzor

A very dubious decision. Better just hang OnScrollListener on RecyclerView
https://stackoverflow.com/questions/36127734/detec...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question