![](/img/trans.png)
[英]Resizing of Layout with Recycler View when the soft keyboard appears
[英]Scrolling a layout when the keyboard appears( Chat app with recycler view)
我想制作像 Telegram 這樣的聊天頁面,當鍵盤出現布局向上滾動但只有主體而不是應用程序欄時。 同樣在鍵盤出現后,回收器視圖必須是可滾動的,並且當鍵盤消失時布局必須向下滾動。 對不起我糟糕的英語。 謝謝。
我試過滾動視圖和
scrollLastPosition = myLayoutManager.findLastVisibleItemPosition()
scrollFirstPosition = myLayoutManager.findFirstVisibleItemPosition()
private fun layoutChanger() {
binding.messageRecycler.addOnLayoutChangeListener { _, _, _, _, bottom,
_, _, _, oldBottom ->
if (scrollLastPosition == messageArrayList.size - 1 && bottom < oldBottom) {
binding.messageRecycler.smoothScrollToPosition(messageArrayList.size - 1)
} else if (scrollLastPosition < messageArrayList.size - 1 && bottom != oldBottom) {
if (bottom > oldBottom) {
binding.messageRecycler.postDelayed({
binding.messageRecycler.scrollToPosition(scrollFirstPosition)
}, 0)
}
binding.messageRecycler.postDelayed({
binding.messageRecycler.smoothScrollToPosition(scrollLastPosition)
}, 10)
//binding.messageRecycler.smoothScrollToPosition(scrollLastPosition)
}
}
}
我的布局
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.core.widget.NestedScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="bottom"
tools:context=".view.MessageActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/messageRecycler"
android:layout_width="match_parent"
android:layout_height="640dp"
app:layout_constraintBottom_toTopOf="@+id/bottom"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
</androidx.recyclerview.widget.RecyclerView>
<LinearLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="@id/messageRecycler"
android:layout_alignParentBottom="true"
android:background="@drawable/back"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<EditText
android:id="@+id/writeMessage"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="6"
android:hint="@string/type_here"
android:importantForAutofill="no"
android:inputType="text"
android:padding="5dp"
tools:ignore="Suspicious0dp">
</EditText>
<ImageView
android:id="@+id/sendImage"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:background="@drawable/send"
android:contentDescription="@string/todo">
</ImageView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
</layout>
但沒有什么比 Telegram 更有效了。在這種布局中,它可以工作,但在鍵盤出現后,當我滾動回收器視圖時,編輯文本也會向下滾動並消失,我希望編輯文本不會移動。
在布局文件中,根據您的要求,使用 NestedScrollView 標記會適得其反,刪除該布局並直接使 ConstraintLayout 成為根視圖應該可以解決您的問題。
請刪除 NestedScrollView 以便您的布局看起來像這樣
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/messageRecycler"
android:layout_width="match_parent"
android:layout_height="640dp"
app:layout_constraintBottom_toTopOf="@+id/bottom"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
</androidx.recyclerview.widget.RecyclerView>
<LinearLayout
android:id="@+id/bottom"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_below="@id/messageRecycler"
android:layout_alignParentBottom="true"
android:background="@drawable/back"
android:gravity="center"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<EditText
android:id="@+id/writeMessage"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_weight="6"
android:hint="@string/type_here"
android:importantForAutofill="no"
android:inputType="text"
android:padding="5dp"
tools:ignore="Suspicious0dp">
</EditText>
<ImageView
android:id="@+id/sendImage"
android:layout_width="0dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:background="@drawable/send"
android:contentDescription="@string/todo">
</ImageView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.