【问题标题】:Recyclerview scrolls down when soft keyboard appears or disappearsRecyclerview在软键盘出现或消失时向下滚动
【发布时间】:2017-10-25 16:12:09
【问题描述】:

问题是当软键盘出现或消失时,recyclerview 向下滚动几乎整行。

这是一个视觉示例:

活动的布局使用来自ChatKit UI library 的 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>

该活动还在清单中设置了adjustPan:

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

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

理想情况下,我不想设置键盘侦听器并在每次显示/隐藏键盘时以编程方式滚动到底部。看来我一定在某处做错了什么。有什么想法吗?

【问题讨论】:

    标签: java android android-layout android-recyclerview chat


    【解决方案1】:

    在实现android ui聊天时,我花了很长时间寻找解决键盘打开时滚动消息的问题。基本上,这些决定是基于将回收器滚动到键盘的高度,它看起来像一根拐杖。

    事实证明,您只需要很少的东西
    1。不要使用 layoutManager.stackFromEnd = true
    2.不要使用高0dp的ConstraintLayout作为recycler parent
    2.不要将具有 wrap_content 高度的 RelativeLayout 用作回收器父级

    设置示例:

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

    对于布局,您可以使用具有 match_parent 高度和从底部到 editText 高度的填充的 LinearLayout、RelativeLayout 或 ConstraintLayout。

    <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      相关资源
      最近更新 更多