I tried implementing addOnScrollListener() but this thing fetch data and loading the recycler view from the beginning every time.i want to load the data after the current position.Here is my sample code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_load, null);
progressBar = (ProgressBar) view.findViewById(R.id.progressBar);
progressBar.setVisibility(View.VISIBLE);
myClickHandler();
business_rv = (RecyclerView) view.findViewById(R.id.business_rv);
business = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL,false);
business_rv.setHasFixedSize(true);
business_rv.setLayoutManager(business);
if (business_rv.getLayoutManager() instanceof LinearLayoutManager) {
business = (LinearLayoutManager) business_rv.getLayoutManager();
business_rv.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
UpdateRecyclerView(url);
}
});
}
return view;
}
what you need is endless recycler view ,which will load the data while scrolling . Here I have attached link for that implementation.
Follow this link
use this as
_recycler.addOnScrollListener(_recyclerViewOnScrollListener);
private RecyclerView.OnScrollListener recyclerViewOnScrollListener = 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) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if (!isLoading && !isLastPage) {
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
&& firstVisibleItemPosition >= 0
&& totalItemCount >= PAGE_SIZE) {
loadMoreItems(++currentPage);
}
}
}
};
Related
I'm trying to implement pagination using code below, and it work well with fragments, but when i implemented it in Nested Fragment it load all data (load first page and all next pages continuously). The problem is that my total items is 12 and visible in time is 3 but layoutManager.getChildCount() give me 12 same as total item (layoutManager.getItemCount()) so it start loading next pages continuously.
Here is oNScroll code :
public PaginationScrollListener(LinearLayoutManager layoutManager) {
this.layoutManager = layoutManager;
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if (!isLoading() && !isLastPage()) {
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
&& firstVisibleItemPosition >= 0
&& totalItemCount >= getTotalPageCount()) {
loadMoreItems();
}
}
}
When scroll and request next pages :
recyclerView.addOnScrollListener(new PaginationScrollListener(layoutManager) {
#Override
protected void loadMoreItems() {
isLoading = true;
currentPage += 1;
// mocking network delay for API call
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
loadNextPage();
}
}, 1000);
}
#Override
public int getTotalPageCount() {
return TOTAL_PAGES;
}
#Override
public boolean isLastPage() {
return isLastPage;
}
#Override
public boolean isLoading() {
return isLoading;
}
});
change your scroll listener like below:
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if(dy>0){ //if is scrolling vertically
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if (!isLoading() && !isLastPage()) {
if (firstVisibleItemPosition + visibleItemCount >= totalItemCount) {
//make your request here
}
}
}});
I am getting this error "Syntax error on tokens, AnnotationName expected instead".Can anyone resolve it.-- Android
mRecyclerView.setLayoutManager(new LinearLayoutManager(products.this){
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
totalItemCount = linearLayoutManager.getItemCount();
lastVisibleItem = linearLayoutManager.findLastVisibleItemPosition();
if (!isLoading && totalItemCount <= (lastVisibleItem + visibleThreshold)) {
if(totalItemCount!=array1.size()-1){
if (mOnLoadMoreListener != null) {
mOnLoadMoreListener.onLoadMore();
}
isLoading = true;
}
}
}
});
}
});
I m getting the error on line 2 i.e.
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
Thanks, in advance.
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()){
// what ever you want to do here is only for LinearLayoutManager methods
});
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
// implement your scrolled code here
}
});
I use the following code to implement the infinite scroll. but the problem is the onScroll() method keeps calling continuously and loading data falls to an endless loop. what am I missing here?
public abstract class EndlessRecyclerOnScrollListener extends RecyclerView.OnScrollListener {
public int previousTotal = 0; // The total number of items in the dataSet after the last load
public boolean loading = true; // True if we are still waiting for the last set of data to load.
public int visibleThreshold = 0; // The minimum amount of items to have below your current scroll position before loading more.
int firstVisibleItem, visibleItemCount, totalItemCount;
public int current_page = 1;
private LinearLayoutManager mLinearLayoutManager;
public EndlessRecyclerOnScrollListener(LinearLayoutManager linearLayoutManager) {
this.mLinearLayoutManager = linearLayoutManager;
}
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLinearLayoutManager.getItemCount();
firstVisibleItem = mLinearLayoutManager.findFirstVisibleItemPosition();
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
}
}
if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem + visibleThreshold)) {
current_page++;
onLoadMore(current_page);
loading = true;
}
}
public abstract void onLoadMore(int current_page);
#Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
}
and in my activity:
endlessRecyclerOnScrollListener = new EndlessRecyclerOnScrollListener(mLayoutManager) {
#Override
public void onLoadMore(int current_page) {
loadData()
}
};
mRecyclerView.addOnScrollListener(endlessRecyclerOnScrollListener);
This is how I did endless scrolling and it works perfect for me. Thanks...
private boolean loading = true;
int pastVisiblesItems, visibleItemCount, totalItemCount;
LinearLayoutManager mLayoutManager;
mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener()
{
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy)
{
if(dy > 0) //check for scroll down
{
visibleItemCount = mLayoutManager.getChildCount();
totalItemCount = mLayoutManager.getItemCount();
pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition();
if (loading)
{
if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount)
{
loading = false;
Log.v("...", "We reach Last Item!");
// fetch new data here or whatever you want.
DoPagination();
}
}
}
}
});
How to implement pagination in recycler listview. I have to show 10 items per page. Can any one tell me how to deal with this?
You can add a scroll listener on recycler view and call the next paginated api on following condition:
recyclerList.addOnScrollListener(new RecyclerView.OnScrollListener() {
#Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
int visibleItemCount = mLayoutManager.getChildCount();
int totalItemCount = mLayoutManager.getItemCount();
int pastVisibleItems = mLayoutManager
.findFirstVisibleItemPosition();
int currentPos = pastVisibleItems + visibleItemCount;
if (currentPos >= totalItemCount) {
callNextApi();
}
}
}
});
I created Recycler View Grid. And I want realize pagination. But I do not understand how do it. I Found one answer enter link description here
But it not work for me. I have not method mLayoutManager.findFirstVisibleItemPosition(); in my LayOutManager. And method mRecyclerView.setOnScrollListener is deprecated. How can I realize pagination in Recycler View Grid?
Late answer but you can try this..and It works perfectly
allMovie_recycler_view.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
if (dy > 0) {
val layoutManager = allMovie_recycler_view.layoutManager as GridLayoutManager
val visibleItemCount = layoutManager.findLastCompletelyVisibleItemPosition()+1
if (visibleItemCount == layoutManager.itemCount){
//Load more data
}
}
}
})
Here is the proper way to paginate with StaggeredGridLayoutManager.
The only difference is in findFirstVisibleItemPositions(), which returns an int[] for the first visible position in each span.
mRecyclerView.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) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = mGridLayoutManager.getChildCount();
int totalItemCount = mGridLayoutManager.getItemCount();
int[] firstVisibleItemPositions = mGridLayoutManager.findFirstVisibleItemPositions(null);
if (!mIsLoading && !mIsLastPage) {
if ((firstVisibleItemPositions[0] + visibleItemCount) >= totalItemCount
&& firstVisibleItemPositions[0] >= 0
&& totalItemCount >= Config.PAGE_SIZE) {
loadMorePosts();
}
}
}
});
I know the answer is late. For others who may encounter this, use this solution.
mRecyclerView.addOnScrollListener(mRecyclerViewOnScrollListener);
private RecyclerView.OnScrollListener
mRecyclerViewOnScrollListener = 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) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = layoutManager.getChildCount();
int totalItemCount = layoutManager.getItemCount();
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
if (!isLoading && currentPage < totalPages) {
if ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount
&& firstVisibleItemPosition >= 0
&& totalItemCount >= numberOfItemsInAPage) {
loadMoreItems();
}
}
}
};