如何在Recycler View中获取当前可见的物品

时间:2018-06-25 07:17:15

标签: android android-recyclerview

我如何获取“ RecyclerView”当前可见的项目。已经尝试过recyclerview的其他方法,但是我无法获得解决方案,所以请帮助并指导我

1 个答案:

答案 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);