如何获得回收器的顶级项目滚动查看



感谢您抽出宝贵的时间来阅读此线程。我正在开发一个安卓应用程序,其中回收器视图的项目中有回收器视图。

我想在滚动回收器视图(父回收

器视图)到达父项第一项的上部位置时执行一些任务(即,如果第一项的上部位置完全可见)。

到目前为止我已经尝试过了,但它没有给出我需要的预期结果

    recyclerView.addOnScrollListener(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);
            LinearLayoutManager linearLayoutManager1 = (LinearLayoutManager) recyclerView.getLayoutManager();
            int displayedPosition = linearLayoutManager1.findFirstCompletelyVisibleItemPosition();
            if(displayedPosition==0){
            //Is this the place where top position of first item is reached ?
                ((ControllableAppBarLayout) getActivity().findViewById(R.id.app_bar_layout)).expandToolbar(true);
            }

        }
    });

通过使用这两种方法的两种组合,您可以存档所需的内容。

 if (linearLayoutManager1.findFirstVisibleItemPosition() == 0
     && linearLayoutManager1.findFirstCompletelyVisibleItemPosition() == 0) {
    // reach first fully visible item.
}

来自操作问题代码片段..

  1. 当心 NullPointerException for linearLayoutManager1,检查 null
  2. 当心 ClassCastException for linearLayoutManager1,检查实例

最新更新