当前位置:  开发笔记 > Android > 正文

当键盘打开时,Recyclerview不会滚动结束

如何解决《当键盘打开时,Recyclerview不会滚动结束》经验,为你挑选了5个好方法。

我在我的应用程序中使用recylerview,每当将新元素添加到recyclerview时,它就会使用滚动到最后一个元素

recyclerView.scrollToPosition(adapter.getCount());

但是,每当键盘打开时(因为editTextView),它会调整视图大小并且recyclerview变小,但无法滚动到最后一个元素.

android:windowSoftInputMode="adjustResize"

我甚至尝试使用以下代码滚动到最后一个元素,但它不起作用

editTextView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(hasFocus){
                recyclerView.scrollToPosition(chatAdapter.getItemCount());
            }
        }
    });

我可以尝试adjustPan将平移向上移动,但它隐藏了我的工具栏.请提出任何解决问题的方法.



1> jihoon kim..:

您可以使用捕获键盘更改recyclerview.addOnLayoutChangeListener().如果bottom小于,oldBottom则键盘处于up状态.

if ( bottom < oldBottom) { 
   recyclerview.postDelayed(new Runnable() { 
       @Override 
       public void run() {
           recyclerview.scrollToPosition(bottomPosition); 
       }
    }, 100);
}


它不起作用.但是,如果你将scrollToPosition更改为smoothScrollToPosition,那么它的工作原理.
这实际上使用`post()`方法而不是`postDelayed()`更顺畅.如果你试试这个,请确保删除`100`毫秒参数,因为`post()`只接受Runnable对象.
没有上述方法对我不起作用:(当键盘启动时,我仍然无法将循环器视图滚动到底部..

2> mutkan..:

添加您的活动或片段:

    if (Build.VERSION.SDK_INT >= 11) {
        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) {
                    recyclerView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            recyclerView.smoothScrollToPosition(
                                    recyclerView.getAdapter().getItemCount() - 1);
                        }
                    }, 100);
                }
            }
        });
    }



3> anisart..:

这个对我有用

LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);


如果您要查找的内容不会滚动到底部,而是滚动以使最后一个项目在键盘打开后仍然可见,则是一个很好的解决方案。正是我想要的东西,谢谢!
非常适合聊天消息列表!

4> Kutae Shaw..:

虽然这是一个老问题,但我今天遇到了这个问题,并且我发现上述方法都不起作用.这是我的解决方案

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



5> mp501..:

我发现这postDelayed是不必要的,并且当回收站滚动到项目中间的某个位置时,使用适配器位置并不能解决问题。我达到了想要的外观:

recycler.addOnLayoutChangeListener((view, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
    if (bottom < oldBottom) {
        messageRecycler.scrollBy(0, oldBottom - bottom);
    }
})

推荐阅读
雯颜哥_135
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有