How to get Top Item of recyclerview on scroll

被刻印的时光 ゝ 提交于 2019-12-11 20:36:52

问题


Thanks for giving your precious time to read this thread. I am developing an android app where there is recyclerview within recyclerview's item.

I want to perform some task when on scrolling the recyclerview(parent recyclerview) reaches to the upper position of the first item of parent (i.e; if the upper position of the first item is completely visible).

I've tried this so far but it is not giving the desired result what I need

    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);
            }


        }
    });

回答1:


By using two combination of these method you can archive what you needed.

 if (linearLayoutManager1.findFirstVisibleItemPosition() == 0
     && linearLayoutManager1.findFirstCompletelyVisibleItemPosition() == 0) {

    // reach first fully visible item.

}

from op question code snippet..

  1. beware of NullPointerException for linearLayoutManager1, check null
  2. beware of ClassCastException for linearLayoutManager1, check instanceof


来源:https://stackoverflow.com/questions/36113642/how-to-get-top-item-of-recyclerview-on-scroll

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!