RecyclerView在打开键盘时无法滚动到末尾

66

我在我的应用程序中使用RecyclerView,每当新元素添加到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来向上移动视图,但这会隐藏我的工具栏。 请建议任何解决此问题的方法。


在 onFocusChange 中,new handler().postDelay(runnable, 500)。 - tiny sunlight
我在这里回答了我的解决方案:https://dev59.com/uKfja4cB1Zd3GeqPpQZ3#65337149 - Hylke
12个回答

0

layoutManager.scrollToPositionWithOffset(chatMessageAdapter.getItemCount() - 1, 0);

布局管理器.scrollToPositionWithOffset(chatMessageAdapter.getItemCount() - 1, 0);


-2

RecyclerView绑定在NestedScrollView内部

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

</android.support.v4.widget.NestedScrollView>

6
这将直接影响主线程并使应用程序变慢。 - turbandroid

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接