我正在用json归档我的数据到recyclerview。 我要在recyclerview中每10个项目中添加新数据。
所以 10个数据->最后在recyclerview中又获得了10个数据 ....
那么我如何检测到该用户位于recyclerview的结尾? 或者,如果有其他解决方案,则可以提交您的想法。我现在使用它,但有时它不起作用...
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
// MyLog.log("rcv1 " + newState);
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
MyLog.log("rcv1 dy: "+ dy);
if (dy>=0 ){
llHeading.setVisibility(View.GONE);
}else {
llHeading.setVisibility(View.VISIBLE);
}
totalItemCount = layoutManager.getItemCount();
visibleItemCount = layoutManager.getChildCount();
pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();
if (!loadingItems && (visibleItemCount + pastVisiblesItems) >= totalItemCount ) {
loadingItems = true;
MyLog.log("rcv1 came to an end.");
getDataClient("",rccount*10,ranking_sort_by,ranking_desc,1);
}
}
});
答案 0 :(得分:3)
在适配器中,您可以使用onBindViewHolder
然后检查位置。
public void onBindViewHolder(ViewHolder holder, int position) {
if (position == getItemCount() - 1) {
// You are at the end of list
}
}
答案 1 :(得分:0)
使用此:
recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
if (!recyclerView.canScrollVertically(1) && newState==RecyclerView.SCROLL_STATE_IDLE) {
Log.d("tag","scrolled to end");
}
}
});