I'm trying to implement Endless Recyclerview in android and able to load the next set of data On scroll of RecyclerView.
But once I receive new set of data , my recycler activity is getting reloaded and items are displaying from initial data . I have even tried with EndlessScrollRecyclListener, but it was same.
Here is the code i'm trying
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = mRecyclerView.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
// End has been reached
new LoadData().execute(0);
loading = true;
}
}
});
Related
I am going to custom a scroll listener for RecyclerView. My expectation is a button "Load more" will display when user scroll to end of list.
But I got a problem when getChildCount onScrolled() method:
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
int totalItemCount = mLayoutManager.getItemCount();
int visibleItemCount = recyclerView.getChildCount();
Log.d(TAG, "onScrolled:visibleItemCount "+visibleItemCount);
if (loading && (totalItemCount > previousTotalItemCount)) {
loading = false;
previousTotalItemCount = totalItemCount;
}
if (!loading && (visibleItemCount + visibleThreshold) > totalItemCount) {
currentPage++;
onLoadMore(currentPage, totalItemCount);
loading = true;
}
}
In the first log, visibleItemCount return the totalItemCount. For example:
onScrolled:visibleItemCount 20
onScrolled:visibleItemCount 4
etc, ....
Note:
20 is total item;
4 is the 4-th item that visible on screen
I have a RecyclerView,I want it to be SwipeRefreshLayout when pull from top,so I can get the latest 20 item and show it at RecyclerView.At the same time,when reach the 20th item at the bottom,then fetch another 20th item from the database.
Therefore,I get the last item like below,so I can fetch the next 20th item.
//for endless scroll
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
// End has been reached
Log.i("Yaeye!", "end called");
fetchPost(feedItems.get(feedItems.size()-1).getId());
//This function is fetch the next 20th item from database
// Do something
loading = true;
}
}
});
}
This is alright when it reach the bottom of the Recycleview,but the problem is when now is the top(1st item in RecyclerView),I pull down,suppose it fetch the new item,but it crashed.This is the error:
java.lang.ArrayIndexOutOfBoundsException: length=27; index=-1
Here is my code: for the SwipeRefreshLayout to fetch latest 20th item in database
// show loader and fetch messages
swipeRefreshLayout.post(
new Runnable() {
#Override
public void run() {
clear();
fetchIntialPost(); // this one,fetch the latest item
}
}
);
So I know that it cause by,when at the 1st item,it also consider the end of the RecyclerView,so it call fetchPost(feedItems.get(feedItems.size()-1).getId()); this function instead of this function fetchIntialPost();
So now my question is,when at the top(1st item),call fetchIntialPost(); when pull the SwipeRefreshLayout down?and the last item,just call fetchPost(feedItems.get(feedItems.size()-1).getId()); .
Thanks
I solved it by checking the value of feedItems.size()-1 before execute this line of code fetchPost(feedItems.get(feedItems.size()-1).getId());
Here is my working code :
//top and bottom also consider end of the list
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
// End has been reached
//top and bottom also consider end of the list
Log.i("Yaeye!", "end called");
if(feedItems.size() - 1 > 0) {
//when pull down SwipeRefreshLayout,feedItems.size = -1
//so if greater than 0,means is bottom of the list
// Do something
fetchPost(feedItems.get(feedItems.size() - 1).getId());
loading = true;
}else{
//here is the top of the list
}
}
}
});
Can you try this?
lastVisibleItem = mLayoutManager.findLastVisibleItemPosition();
if (!loading && lastVisibleItem == (totalItemCount - 1)) {
// End has been reached
}
I have recyclerview which contains horizontal child recycler views and in the end it has one single endless scrolling vertical recycler view child
<---RecyclerView-------->
<Horizonatl recycler view>
<Horizonatl recycler view>
<Vertical recyler view endless scrolling>
<---RecyclerView-------->
I have added wrap_content height to child Vertical recyler view due to which I am not able to add scroll listner to this child recylerview
I have added endless scroll listener to outermost parent recyclerview but scroll is working till page 2 only and then its not working how can I achieve nested infinite scroll. Following is my code for listener applied on parent recyclerview
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = layoutManager.getItemCount();
firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
if (dy > 0) {
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
loading = false;
currentPage++;
}
}
if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
if (Utils.haveNetworkConnection(getActivity()) && currentPage < Constants.MAX_PAGES) {
loadMoreData();
loading = true;
}
}
}
}
Please i am working on an android contact app which i need to load first 20 phone book contact to recycler view and use Onscroll listener to get the remaining contact to the recyclyer view when scrolling.
First add 20 item in Arraylist of Contact.on Scroll end add more item's and call adapter.notifyDataSetChanged() . This is the code to detect scroll end.
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = mRecyclerView.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
firstVisibleItem = mLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
// End has been reached
Log.i("Yaeye!", "end called");
// Add more items into contact Arraylist and call notifyDataSetChanged()
loading = true;
}
}
});
Note : Make sure you are using LinearLayoutManager as layout manager for RecyclerView.
LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
There is any way to implement endless scrolling to load more itens when i use SuperSLiM library?
Normally I'm used to use the LinearLayoutManager.findFirstVisibleItemPosition() method to help me to know when I need load more itens... but now, with linearlayout of SuperSLiM i can't use this.
How can i implement this feature?
In place of LinearLayoutManager,use LayoutManager of superslim. I had implemented it in one of demo project. Row code for that is,
public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
private int previousTotal = 0; // The total number of items in the dataset after the last load
private boolean loading = true; // True if we are still waiting for the last set of data to load.
private int visibleThreshold = 5; // The minimum amount of items to have below your current scroll position before loading more.
int firstVisibleItem, visibleItemCount, totalItemCount;
private int current_page = 1;
private LayoutManager mlayoutManager;
public EndlessRecyclerOnScrollListener(LayoutManager linearLayoutManager) {
this.mlayoutManager = layoutManager;
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mlayoutManager.getItemCount();
firstVisibleItem = mlayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
// End has been reached
// Do something here
current_page++;
loading = true;
}
}
public abstract void onLoadMore(int current_page);
}
I hope this will help you. :)