Recyclerview indicates that reaches the last item
Thanks to Khemraj who give the tip for solution
because I have a recyclerview
inside NestedScrollView
and coordinator layout
as parent for them
I have solved my problem like this:
public Disposable observeNestedScroll(LoadMoreListener listener) {
return RxNestedScrollView.scrollChangeEvents(nestedScrollView)
.subscribe(
viewScrollChangeEvent -> {
NestedScrollView nestedScrollView =(NestedScrollView) viewScrollChangeEvent.view();
if(nestedScrollView.getChildAt(nestedScrollView.getChildCount() - 1) != null) {
if ((viewScrollChangeEvent.scrollY() >= (nestedScrollView.getChildAt(nestedScrollView.getChildCount() - 1).getMeasuredHeight() - nestedScrollView.getMeasuredHeight())) &&
viewScrollChangeEvent.scrollY() > viewScrollChangeEvent.oldScrollY()) {
listener.onLoadMore();
}
}
});
}
How can I identify that RecyclerView's last item is visible on screen?
One option would involve editing your LayoutManager
. The idea here is to find the position of the last visible item. If that position is equal to the last item of your dataset, then you should trigger a reload.
@Override
public int scrollVerticallyBy(int dy, RecyclerView.Recycler recycler, RecyclerView.State state) {
final int result = super.scrollVerticallyBy(dy, recycler, state);
if (findLastVisibleItemPosition() == mData.length - 1) {
loadMoreData();
}
return result;
}
@Override
public void onLayoutChildren(RecyclerView.Recycler recycler, RecyclerView.State state) {
super.onLayoutChildren(recycler, state);
if (findLastVisibleItemPosition() == mData.length - 1) {
loadMoreData();
}
}
Alternatively, you could do this via your adapter's onBindViewHolder
method, although this is admittedly a bit of a "hack":
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
if (position == mData.length - 1) {
// load more data here.
}
/// binding logic
}
3rd option would be to add an OnScrollListener to the RecyclerView. @velval's answer on this page explains this well.
Regardless which option you go for, you should also include code to prevent the data load logic from triggering too many times (e.g., before the previous request to fetch more data completes and returns new data).
NestedRecyclerView Problem - Last item of the second RecyclerView is duplicated into the first one after the keyboard appears
In SubAdapter
, remove the static
keyword from your items
field:
private static List<Item> items;
Should be this instead:
private List<Item> items;
You should make the same change to your outer adapter, but since there's only one of them at a time it doesn't wind up causing issues.
How do I detect when it comes to the end in recyclerview?
In your adapter, you could use onBindViewHolder
then check the position.
public void onBindViewHolder(ViewHolder holder, int position) {
if (position == getItemCount() - 1) {
// You are at the end of list
}
}
RecyclerView doesn't display last item when multiple ViewHolders are used
When i add two items and the header, the header and one item are
displayed.
problem is in your getItemCount
method.
override fun getItemCount(): Int {
return if (items.isEmpty()) 1 else items.filter { it.viewType == ITEM }.size
}
If you want to show 1 header and 2 elements that means that there are must be 3 items in recyclerview, so getItemCount
must return 3. But now it looks like getItemCount
will return 2, thats why recycerlview
doesn't even create third element.
Android - Detect when the last item in a RecyclerView is visible
try working with onScrollStateChanged
it will solve your issue
Related Topics
How to Open a New Fragment from Another Fragment
Google Maps, No Option for Starting the Navigation, Only Preview Is There
Open External Links in the Browser With Android Webview
Touchableopacity Not Working Inside an Absolute Positioned View
How to Disable Recyclerview Items from Clicking
Android - How to Make a Button Click Play a Sound File Every Time It Been Pressed
Android: Rotate Image in Imageview by an Angle
Flutter:Renderbox Was Not Laid Out: Renderrepaintboundary#58C65 Relayoutboundary=Up1 Needs-Paint
React-Native: Module Appregistry Is Not a Registered Callable Module
Android - Edittext Not Showing Any Text
Add Text to Image in Android Programmatically
How to Retry Retrofit Call on Http Errors (401) When Using Rxjava
How to Enable/Disable Floatingactionbutton Behavior
Background Image Jumps When Address Bar Hides Ios/Android/Mobile Chrome
How to Handle Notification When App in Background in Firebase