简体   繁体   English

当软键盘出现或消失时,Recyclerview 向下滚动

[英]Recyclerview scrolls down when soft keyboard appears or disappears

The issue is that the recyclerview scrolls down almost a complete row when the soft keyboard appears or disappears.问题是当软键盘出现或消失时,recyclerview 几乎向下滚动了一整行。

Here is a visual example:这是一个视觉示例:

在此处输入图片说明

The layout for the activity uses a MessageInput and MessageList object from the ChatKit UI library and is as follows:活动的布局使用ChatKit UI 库中的 MessageInput 和 MessageList 对象,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="apps.cal.calchat.ChatActivity.ChatView">

    <TextView
        android:id="@+id/chatIsTypingTextView"
        android:layout_width="match_parent"
        android:layout_marginLeft="8dp"
        android:layout_height="wrap_content"
        android:layout_above="@+id/chatMessageInput"/>

    <com.stfalcon.chatkit.messages.MessageInput
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:id="@+id/chatMessageInput"
        android:layout_width="0dp"
        android:layout_height="wrap_content" />

    <com.stfalcon.chatkit.messages.MessagesList
        android:id="@+id/chatMessagesList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/chatIsTypingTextView"
        />

</RelativeLayout>

The activity also has adjustPan set in the manifest:该活动还在清单中设置了 adjustPan:

    <activity
        android:name=".ChatActivity.ChatView"
        android:windowSoftInputMode="adjustPan|stateHidden"/>

The Recyclerviews linear layout manager is set as vertical, with a reverse layout and with the default item animator: Recyclerviews 线性布局管理器设置为垂直,具有反向布局和默认项目动画:

public <MESSAGE extends IMessage>
    void setAdapter(MessagesListAdapter<MESSAGE> adapter, boolean reverseLayout) {
        SimpleItemAnimator itemAnimator = new DefaultItemAnimator();
        itemAnimator.setSupportsChangeAnimations(false);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getContext(),
                LinearLayoutManager.VERTICAL, reverseLayout);

        setItemAnimator(itemAnimator);
        setLayoutManager(layoutManager);
        adapter.setLayoutManager(layoutManager);
        adapter.setStyle(messagesListStyle);

        addOnScrollListener(new RecyclerScrollMoreListener(layoutManager, adapter));
        super.setAdapter(adapter);
    }

I ideally do not want to set up a keyboard listener and programatically scroll to the bottom every time the keyboard is shown/hidden.理想情况下,我不想在每次显示/隐藏键盘时设置键盘侦听器并以编程方式滚动到底部。 It seems that I must be doing something wrong somewhere.看来我一定是在某个地方做错了什么。 Any ideas?有什么想法吗?

When implementing the android ui chat, I spent a long time looking for a solution to the problem with scrolling messages when the keyboard opens.在实现android ui聊天时,我花了很长时间寻找解决键盘打开时滚动消息问题的方法。 Basically, the decisions were based on scrolling through the recycler to the height of the keyboard and it looked like a crutch.基本上,这些决定是基于将回收器滚动到键盘的高度,它看起来像一个拐杖。

As it turned out, you need only few things事实证明,你只需要很少的东西
1. Do not use layoutManager.stackFromEnd = true 1. 不要使用 layoutManager.stackFromEnd = true
2. Do not use ConstraintLayout with high 0dp as a recycler parent 2.不要使用0dp高的ConstraintLayout作为recycler parent
2. Do not use RelativeLayout with wrap_content height as a recycler parent 2.不要使用RelativeLayout和wrap_content height作为recycler parent

Setting example:设置示例:

        val adapter = MessagesAdapter ()
        val layoutManager = LinearLayoutManager (this, RecyclerView.VERTICAL, true)
        rvMessages.layoutManager = layoutManager
        rvMessages.adapter = adapter

For layout you can use LinearLayout, RelativeLayout or ConstraintLayout with match_parent height and padding from bottom to editText height.对于布局,您可以使用 LinearLayout、RelativeLayout 或 ConstraintLayout 与 match_parent 高度和从底部到 editText 高度的填充。

<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
    android: layout_width = "match_parent"
    android: layout_height = "match_parent"
    android: orientation = "vertical">

    <androidx.recyclerview.widget.RecyclerView
        android: id = "@ + id / rvMessages"
        android: layout_width = "match_parent"
        android: layout_height = "0dp"
        android: layout_weight = "1" />

    <EditText
        android: id = "@ + id / etMessage"
        android: layout_width = "match_parent"
        android: layout_height = "wrap_content" />

</LinearLayout>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM