在将回收站视图滚动到顶部时进行分页 [英] Pagination while scrolling recycler view to the top
问题描述
我有一个在向上滚动RecyclerView时实现分页的要求.我遇到一些问题,因为在将内容添加到第0位并通知适配器时,recyclerview会自动滚动到顶部.有办法禁用这种行为吗?
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();
}
}
setFields(viewHolder, data);
方法将值设置为不同字段的位置.
Where setFields(viewHolder, data);
method sets values to different fields.
if (position == 0 && getParentFragment().hasPrevious()) {
getParentFragment().callPreviousPage();
}
上面的代码用于在回收站视图的位置达到0(分页)时调用Web服务.
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();
}
上面是将从Web服务获取的数据加载到适配器的方法.
The above is the method for loading the data got from web service to the adapter.
非常感谢您.
推荐答案
适配器中的代码.请注意,细粒度更新比调用notifydataset更好.
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();
}
});
添加项目的代码
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
这篇关于在将回收站视图滚动到顶部时进行分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!