本文介绍了检测反向RecyclerView的顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用RecyclerView,它像这样[像聊天视图]一样反转了

I am using a RecyclerView which is reversed like this [like a chat view]

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

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

and I am trying to detect when the user scrolls to most top of the RecyclerView

我已经尝试过此代码,但这是不正确的

I have tried this code but it is not correct

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


            }
        });

有帮助吗?

推荐答案

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

这篇关于检测反向RecyclerView的顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 12:35