I have a requirement for implementing pagination while scrolling up a RecyclerView. I am facing some issues as while adding content to the 0th position and notifying the adapter, recyclerview automatically scrolls to the top. Is there a way to disable that behaviour?
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
setFields(viewHolder, data);
if (position == 0 && getParentFragment().hasPrevious()) {
getParentFragment().callPreviousPage();
}
}
Where setFields(viewHolder, data);
method sets values to different fields.
if (position == 0 && getParentFragment().hasPrevious()) {
getParentFragment().callPreviousPage();
}
The above code is for calling web service when the position of the recycler view reaches 0 (pagination).
public synchronized void setList(final List<TournamentsData> list) {
this.list.addAll(0, list);
notifyDataSetChanged();
}
The above is the method for loading the data got from web service to the adapter.
Thank you so much in advance.
Code inside adapter.Please note that granular updates is better than calling notifydataset changed.Reference
public void addlisttop(List<MyInformationClass> list)
{
for(int i=0;i<list.size();i++)
{
myinformation.add(i,list.get(i));
notifyItemInserted(i);
}
}
the code to add items when the list is reaches the zero position
myRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
int itemno=myLayoutManager.findFirstVisibleItemPosition();
if(itemno==0)additems();
}
});
Code to add items
private void additems()
{
MyInformationClass myInformationClass=new MyInformationClass();
List<MyInformationClass> list=new ArrayList<>(10);
for(int i=0;i<10;i++)
{
myInformationClass=new MyInformationClass();
myInformationClass.maintext="BulkListItem"+ String.valueOf(number);
myInformationClass.subtext="subtext";
list.add(myInformationClass);
number=number+1;
}
myRecycleAdapter.addlisttop(list);
}
for complete reference.Refer this
来源:https://stackoverflow.com/questions/39558704/pagination-while-scrolling-recycler-view-to-the-top