检测反向RecyclerView的顶部

时间:2017-02-05 23:51:30

标签: java android android-recyclerview

我正在使用RecyclerView这样的反转[像聊天视图]

  mLinearLayoutManager = new LinearLayoutManager(this);
  mLinearLayoutManager.setReverseLayout(true);
  mLinearLayoutManager.setStackFromEnd(true);
  mRecyclerView.setLayoutManager(mLinearLayoutManager);

我试图检测用户何时滚动到RecyclerView

的最顶部

我试过这段代码但是不正确

  mRecyclerView.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);


                if (recyclerView.getAdapter().getItemCount() != 0) {
                    int firstVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findFirstCompletelyVisibleItemPosition();
                    if (firstVisibleItemPosition != RecyclerView.NO_POSITION && firstVisibleItemPosition == 0)
                        loadMoreMSGs();
                }


            }
        });

任何帮助?

1 个答案:

答案 0 :(得分:0)

mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (recyclerView.getAdapter().getItemCount() != 0) {
                int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
                if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount()-1)
                    loadMoreMSGs();
            }
        }
    });