我的Recyclerview是一组消息列表。
每个项目都是一个线性布局,宽度使用 "wrap_content"。
问题在于,当插入新项时,如果比前一个项更大,则该项将采用前一个项的宽度(这使其看起来像具有大填充)。
我尝试在ViewHolder构造函数中使用 "setRecycable(false)",但导致一些问题,我想采用不同的方法。
感激任何帮助。谢谢
编辑:布局:
每个项目都是一个线性布局,宽度使用 "wrap_content"。
问题在于,当插入新项时,如果比前一个项更大,则该项将采用前一个项的宽度(这使其看起来像具有大填充)。
我尝试在ViewHolder构造函数中使用 "setRecycable(false)",但导致一些问题,我想采用不同的方法。
感激任何帮助。谢谢
编辑:布局:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="@+id/message_date_layout"
layout="@layout/time_bubble"/>
<LinearLayout
android:id="@+id/message_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:layout_marginEnd="30dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="30dp"
android:layout_marginStart="6dp"
android:background="@drawable/background_incoming_chat_bubble"
android:orientation="vertical"
android:padding="6dp">
//other views
</LinearLayout>
</LinearLayout>
创建ViewHolder
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ChatMessagesAdapter.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(context);
View v = null;
switch (viewType) {
case OUTGOING:
v = inflater.inflate(R.layout.chat_bubble_outgoing, parent, false);
break;
case INCOMING:
v = inflater.inflate(R.layout.chat_bubble_incoming, parent, false);
break;
}
viewHolder = new ChatMessagesAdapter.ViewHolder(v);
return viewHolder;
}
chat_bubble_outgoing
和chat_bubble_incoming
的布局吗? - sharp