我有一个带有底部标签栏的recyclerview我已经尝试使用大量的代码片段实现这一点但事件不会被调用 这就是我尝试它的方式,首先我创建了一个Listener,然后在我的片段中使用它。
public abstract class EndlessRecyclerViewScrollListener extends RecyclerView.OnScrollListener {
public static String TAG = EndlessRecyclerViewScrollListener.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 EndlessRecyclerViewScrollListener(
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);
}
这是我在片段中写的代码
rv_influencer.addOnScrollListener(new EndlessRecyclerViewScrollListener((linearLayoutManager)) {
@Override
public void onLoadMore(int current_page) {
page_no++;
getInfluencerData();
Toast.makeText(getActivity(), "Scroll", Toast.LENGTH_LONG).show();
}
});