我正在尝试使用 Recycler 视图创建一个聊天屏幕。由于用户进入屏幕时的聊天,列表应该滚动到最后一个位置。
我的问题是当键盘打开列表隐藏时。如果我们使用调整平移,整个视图将向上,我试图只滚动列表,我希望标题栏在键盘打开时保持相同的位置
我正在尝试使用 Recycler 视图创建一个聊天屏幕。由于用户进入屏幕时的聊天,列表应该滚动到最后一个位置。
我的问题是当键盘打开列表隐藏时。如果我们使用调整平移,整个视图将向上,我试图只滚动列表,我希望标题栏在键盘打开时保持相同的位置
将此添加到您的代码中并检查....它对我来说很好....
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 (mRecyclerAdapter != null)
{
if (bottom < oldBottom)
{
mRecyclerView.smoothScrollToPosition(mRecyclerAdapter.getItemCount() - 1);
}
}
}
});
当您收到新消息时添加此行....
mRecyclerView.smoothScrollToPosition(mRecyclerAdapter.getItemCount() - 1);
无需在清单中添加 android:windowSoftInputMode。只需将您的回收站视图保持在底部视图上方即可。希望这会有所帮助。`
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<RelativeLayout
android:id="@+id/rlBottom"
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_gravity="bottom"
android:layout_margin="@dimen/dimen_8"
android:background="@drawable/shape_write_msg"
android:elevation="@dimen/dimen_2">
<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="@dimen/dimen_12"
android:layout_marginStart="@dimen/dimen_12"
android:layout_toEndOf="@+id/ivAdd"
android:layout_toStartOf="@+id/ivSend"
android:background="@color/white"
android:focusable="true"
android:hint="@string/label_type_here"
android:inputType="textCapSentences"
android:textColor="@color/black"
android:textCursorDrawable="@null"
android:textSize="@dimen/sp_12"
android:visibility="visible" />
<ImageView
android:id="@+id/ivSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="@dimen/dimen_4"
android:padding="@dimen/dimen_8"
android:src="@drawable/drawable_send"
android:visibility="visible" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
`