簡體   English   中英

當軟鍵盤出現或消失時,Recyclerview 向下滾動

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

問題是當軟鍵盤出現或消失時,recyclerview 幾乎向下滾動了一整行。

這是一個視覺示例:

在此處輸入圖片說明

活動的布局使用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>

該活動還在清單中設置了 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);
    }

理想情況下,我不想在每次顯示/隱藏鍵盤時設置鍵盤偵聽器並以編程方式滾動到底部。 看來我一定是在某個地方做錯了什么。 有什么想法嗎?

在實現android ui聊天時,我花了很長時間尋找解決鍵盤打開時滾動消息問題的方法。 基本上,這些決定是基於將回收器滾動到鍵盤的高度,它看起來像一個拐杖。

事實證明,你只需要很少的東西
1. 不要使用 layoutManager.stackFromEnd = true
2.不要使用0dp高的ConstraintLayout作為recycler parent
2.不要使用RelativeLayout和wrap_content height作為recycler parent

設置示例:

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

對於布局,您可以使用 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