Nested recyclerview endless scroll not working - android

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;
}
}
}
}

Related

Android getchildcount return to total before scrolling

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

SwipeRefreshLayout for RecycleView for both top and bottom?

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
}

How to load first 20 contact to recycler view and use OnScrollListener to load more to recycler view

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);

How to implement pagination in RecyclerView which is inside nestedScrollView

I have included RecyclerView inside NestedScrollView and set
mRecyclerView.setNestedScrollingEnabled(false);
Since the scrolling of RecyclerView is set to false, I will not get the scroll position of RecyclerView due to this I will not be able to put pagination in RecyclerView.
I have also tried this
mRecylerview.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if(dy > 0) {
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
// End has been reached
// Do something
if (mOnLoadMoreListener != null) {
mOnLoadMoreListener.onLoadMore();
isLoading = true;
}
}
}
}
});
But here lastVisibleItem always gives the length of the list to be displayed.
After few digging, I found that when we use RecylerView inside NestedScrollView all the view gets created at the very beginning so that's the reason why lastVisibleItem always gives the size of the list.
Or if I am wrong please explain, me how RecyclerView inside NestedScrollView works?.
Is there any workaround so that I can get RecyclerView scroll position so that I can make my pagination work?.
It will be a great help. Thanks in advance
Here scroll is NestedScrollView
scroll.getViewTreeObserver().addOnScrollChangedListener(() -> {
View view = (View) scroll.getChildAt(scroll.getChildCount() - 1);
Log.d("CategoryNeswList", scroll.getChildCount() + " child");
int diff = (view.getBottom() - (scroll.getHeight() + scroll
.getScrollY()));
if (diff == 0) {
// getPlaylistFromServer("more");
Toast.makeText(mContext, "Load More", Toast.LENGTH_SHORT).show();
}
});

endless scroll Recyclerview reloads page onscroll

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;
}
}
});

Categories

Resources