Answer the question
In order to leave comments, you need to log in
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);
}
void setOnBottomReachedListener(OnBottomReachedListener onBottomReachedListener) {
this.onBottomReachedListener = onBottomReachedListener;
}
if (position == getItemCount() - 1 && position != getItemCount()) {
onBottomReachedListener.onBottomReached(position);
}
((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")));
}
}
});
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question