我在下面的代码中有一个无休止的循环视图检查
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
public abstract class EndlessRecyclerOnScrollListener extends
RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerOnScrollListener.class
.getSimpleName();
private int previousTotal = 0;
private boolean loading = true;
private int visibleThreshold = 5;
int firstVisibleItem, visibleItemCount, totalItemCount;
private 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)) {
// End has been reached
// Do something
current_page++;
onLoadMore(current_page);
loading = true;
}
}
public abstract void onLoadMore(int current_page);
}在我的片段类中,我调用上面的类如下所示
mRecyclerView.setOnScrollListener(new EndlessRecyclerOnScrollListener(
mLayoutManager) {
@Override
public void onLoadMore(int current_page) {
if (SolutionEnterprises.mGetRCAAcceptedListDatas.size() < 100) {
from = from + limit;
new getHomeTicketList(false).execute();
}
}
});当我运行上面的代码时,当我在循环视图中滚动5项时,它会调用agian我的web,这很好,但是问题是当web在第5项后调用scrooll再循环查看第一位置,那么如何解决这个问题呢?
发布于 2017-01-24 12:57:53
you need to carefully with your adapter.
private ContractsAdapter contractListAdapter;
private void setAdapter(final ArrayList<ContractsModel> list) {
if (contractListAdapter == null) {
contractList = new ArrayList<>();
contractList.addAll(list);
contractListAdapter = new ContractsAdapter(this, contractList);
rvContracts.setAdapter(contractListAdapter);
} else {
if (isLoadMore) {
contractList.addAll(list);
contractListAdapter.notifyDataSetChanged();
isLoadMore = false;
} else {
contractList.clear();
contractList.addAll(list);
contractListAdapter.notifyDataSetChanged();
}
}
}https://stackoverflow.com/questions/40434777
复制相似问题