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

Muk*_*rma 48 android listview android-softkeyboard android-recyclerview

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

recyclerView.scrollToPosition(adapter.getCount());
Run Code Online (Sandbox Code Playgroud)

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

android:windowSoftInputMode="adjustResize"
Run Code Online (Sandbox Code Playgroud)

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

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

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

jih*_*kim 62

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

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

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

mut*_*kan 51

添加您的活动或片段:

    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);
                }
            }
        });
    }
Run Code Online (Sandbox Code Playgroud)


ani*_*art 19

这个对我有用

LinearLayoutManager layoutManager = new LinearLayoutManager(context);
layoutManager.setStackFromEnd(true);
recyclerView.setLayoutManager(layoutManager);
Run Code Online (Sandbox Code Playgroud)

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

Kut*_*haw 7

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

    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);
                  }
                });
              }
         });
   }
Run Code Online (Sandbox Code Playgroud)


won*_*suc 6

这有效。

private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private LinearLayoutManager mManager;

...

mManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mManager);

(initialize and set adapter.)

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

private void scrollToBottom() {
    mManager.smoothScrollToPosition(mRecyclerView, null, mAdapter.getItemCount());
}
Run Code Online (Sandbox Code Playgroud)


mp5*_*501 6

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

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