RecyclerView仅显示首条消息排查:已正常获取但无法全部展示
RecyclerView仅显示第一条消息的问题排查与解决
嘿,我仔细看了你的代码实现,马上就找到问题根源了——消息Item的布局高度设置错误,导致后续消息被隐藏了。
问题分析
你的消息Item布局文件(messages_layout_of_users.xml)中,根布局ConstraintLayout的android:layout_height被设置为match_parent,这会让每一条消息Item都占据整个屏幕的高度。第一条消息占满RecyclerView的可视区域后,后面的消息虽然已经被加载到messagesList中,但都被挤到了屏幕之外,自然无法看到。
另外,聊天页面布局里的RecyclerView高度设为wrap_content也不太合理,会导致RecyclerView无法充分利用可用空间,影响滚动体验。
修复方案
1. 修改消息Item布局
把Item根布局的高度从match_parent改为wrap_content,让每个Item只适配自身内容的高度:
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 关键修改 --> <TextView android:textSize="15dp" android:id="@+id/message_text" android:background="@drawable/messages_text_background" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.constraint.ConstraintLayout>
2. 优化RecyclerView布局设置
将聊天页面里的RecyclerView高度改为match_parent,让它填满父布局的可用空间:
<android.support.v7.widget.RecyclerView android:id="@+id/messageList" android:layout_width="match_parent" android:layout_height="match_parent" <!-- 关键修改 --> android:scrollbars="vertical"> </android.support.v7.widget.RecyclerView>
做完这两处修改后,重新运行程序,所有消息应该就能正常依次显示,并且可以正常滚动查看了。
内容的提问来源于stack exchange,提问作者user9027554
相关产品推荐
相关产品推荐

