我如何获取“ RecyclerView”当前可见的项目。已经尝试过recyclerview的其他方法,但是我无法获得解决方案,所以请帮助并指导我
答案 0 :(得分:1)
private RecyclerView.OnScrollListener recyclerViewOnScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
super.onScrollStateChanged(recyclerView, newState);
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
int visibleItemCount = linearLayoutManager.getChildCount();
int totalItemCount = linearLayoutManager.getItemCount();
int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
final int lastItem = firstVisibleItemPosition + visibleItemCount;
}
};
全局声明LinearLayoutManger,
私有LinearLayoutManager linearLayoutManager;
像这样初始化RecyclerView,
linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
notificationListRecyclerView.setLayoutManager(linearLayoutManager);
notificationListRecyclerView.addOnScrollListener(recyclerViewOnScrollListener);