首页 > 解决方案 > windowSoftInput 隐藏recycleview中的项目

问题描述

我正在创建一个聊天应用程序,到目前为止它看起来像这样:

在此处输入图像描述

我开始输入的那一刻,它隐藏了最后两项,看起来像(edittext 隐藏了 g、h 消息):

在此处输入图像描述

我的 XML 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ChatActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rv_Messages"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintHeight_percent="0.9"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/ib_Send"
        style="?android:borderlessButtonStyle"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:adjustViewBounds="true"
        android:src="@drawable/ic_location_on_black_24dp"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <EditText
        android:id="@+id/et_Message"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:hint="enter message here"
        android:backgroundTint="@color/colorLightPurple"
        android:inputType="textMultiLine|textPersonName"
        app:layout_constraintHeight_percent="0.1"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/ib_Send"
        app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

我已经添加到我的清单中android:windowSoftInputMode="adjustPan"

有没有一种方法可以确保我的recycleview 将始终显示最新消息/确保edittext 不会隐藏?

谢谢

标签: androidandroid-layoutandroid-recyclerview

解决方案


使用相对和线性布局的组合,然后在recyclerview 中使用marginBottom。希望你能找到它。

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

    <include
        android:id="@+id/chat_toolbar"
        layout="@layout/app_bar_layout">
    </include>


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/private_messages_list_of_users"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/chat_toolbar"
        android:layout_above="@+id/chat_linear_layout"
        android:layout_marginBottom="6dp"
        >
    </androidx.recyclerview.widget.RecyclerView>


    <RelativeLayout
        android:id="@+id/chat_linear_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentBottom="true"
        android:background="@android:color/background_light"
        android:orientation="horizontal"
        >


        <ImageButton
            android:id="@+id/send_files_btn"
            android:layout_width="60dp"
            android:layout_height="50dp"
            android:src="@drawable/ic_attach_file_black_24dp"
            android:layout_alignParentStart="true"
            android:backgroundTint="@android:color/white"
            />


        <EditText
            android:id="@+id/input_message"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="type message here..."
            android:padding="15dp"
            android:maxLines="5"
            android:layout_toEndOf="@+id/send_files_btn"
            android:layout_toStartOf="@+id/send_message_btn"
            />


        <ImageButton
            android:id="@+id/send_message_btn"
            android:layout_width="60dp"
            android:layout_height="wrap_content"
            android:src="@drawable/send_message"
            android:layout_alignParentEnd="true"
            android:backgroundTint="@android:color/white"
            />

    </RelativeLayout>


</RelativeLayout>

看看我上面的例子......


推荐阅读