在MVP模式中,我有一个通过改装从服务器获取的列表,而在实现分页时,我的infinteScrollListener仅返回固定的页码,并且列表跳至顶部。
我的代码如下:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_list);
ButterKnife.bind(this);
listAdapter = new ProductListAdapter(this);
mPresenter.start();
Intent intent = getIntent();
if (intent != null) {
mostTitle = intent.getStringExtra(TOPBENEFIT);
mostStatus.setText(mostTitle);
code = intent.getIntExtra(MOST_ITEMS_CODE, -1);
mPresenter.getAllTopBenefitgetAllTopBenefitFirst(currentPage, "", "", ADD_ONLY_IF_LIST_IS_EMPTY);
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
}
}
@Override
public void showAllTopBenefitProducts(List<ResponseAllProductList> list, ProductListAdapter.AddStrategy strategy) {
listAdapter.addList(list, strategy);
recyclerProductsList.setLayoutManager(linearLayoutManager);
recyclerProductsList.addOnScrollListener(new InfiniteScrollListener(linearLayoutManager) {
@Override
public void onLoadMore(int current_page) {
currentPage++;
mPresenter.getMostItemsCode(code, current_page, brandId, search, ADD_PAGINATION_ITEMS);
}
});
recyclerProductsList.setAdapter(listAdapter);
}
我的无限滚动侦听器类:
公共抽象类InfiniteScrollListener扩展 RecyclerView.OnScrollListener {
public static String TAG = InfiniteScrollListener.class.getSimpleName();
int firstVisibleItem, visibleItemCount, totalItemCount;
private int previousTotal = 0;
private boolean loading = true;
private int visibleThreshold = 2;
private int current_page = 1;
private LinearLayoutManager mLinearLayoutManager;
public InfiniteScrollListener(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)) {
int a = current_page + 1;
onLoadMore(a);
loading = true;
}
}
public void resetState() {
loading = true;
previousTotal = 0;
current_page = 1;
}
public abstract void onLoadMore(int current_page);
}