美文网首页
让RecyclerView滑动到底部

让RecyclerView滑动到底部

作者: IOXusu | 来源:发表于2023-02-23 12:11 被阅读0次

问题:常见的滑动到最后一行但是显示不全的情况?
如果以下方法还是不行,可以考虑添加一定的延时

1.普通场景下滑动到最后一行

recyclerView.scrollToPosition(mAdapter.getData().size() - 1);

2.弹出软键盘时滑动到最后一行

recyclerView.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
            if (bottom < oldBottom) {
                linearLayoutManager.smoothScrollToPosition(recyclerView, null, mAdapter.getItemCount());
            }
        });

如果不生效,尝试另一种

recyclerView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v,int left, int top, int right, int bottom,int oldLeft, int oldTop, int oldRight, int oldBottom) {
            if (bottom < oldBottom) {
                final int lastAdapterItem = mAdapter.getItemCount() - 1;
                recyclerView.post(new Runnable() {
                  @Override
                  public void run() {
                    int recyclerViewPositionOffset = -1000000;
                    View bottomView = linearLayoutManager.findViewByPosition(lastAdapterItem);
                    if (bottomView != null) {
                       recyclerViewPositionOffset = 0 - bottomView.getHeight();
                     }
                     linearLayoutManager.scrollToPositionWithOffset(lastAdapterItem, recyclerViewPositionOffset);
                  }
                });
              }
         });
   }

相关文章

网友评论

      本文标题:让RecyclerView滑动到底部

      本文链接:https://www.haomeiwen.com/subject/xknzkdtx.html