显示单个项目的回收视图

我正面临一个奇怪的错误,回收站只显示了一个单一的项目。下面是我的回收视图适配器的代码:

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {




List<chat> chats;
String username;
final int My=0,Their=1;


public ChatAdapter(List<chat> chats) {
this.chats = chats;
this.username = PushApp.getApp().getSPF().getStringData(Constants.ANAME);
}




@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder;
LayoutInflater inflater = LayoutInflater.from(parent.getContext());


switch (viewType) {
case My:
View v1 = inflater.inflate(R.layout.my_chat_child, parent, false);
viewHolder = new MyChatHolder(v1);
break;
case Their:
View v2 = inflater.inflate(R.layout.their_chat_child, parent, false);
viewHolder = new TheirMessageHolder(v2);
break;
default:
View v = inflater.inflate(R.layout.my_chat_child, parent, false);
viewHolder = new MyChatHolder(v);
break;
}


return viewHolder;


}


@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (holder.getItemViewType()) {
case My:
MyChatHolder myChatHolder = (MyChatHolder) holder;
myChatHolder.setMyMessage(chats.get(position).getMessage());
break;


case Their:
TheirMessageHolder theirMessageHolder = (TheirMessageHolder) holder;
theirMessageHolder.setTheirChat(chats.get(position).getFrom(), chats.get(position).getMessage());
break;
}
}


@Override
public int getItemViewType(int position) {
if(username.equalsIgnoreCase(chats.get(position).getFrom()))
return My;
return Their;
}


@Override
public int getItemCount() {
return chats.size();
}}

我已经在其他应用程序中使用了这个代码,它工作得很完美。我已经检查了聊天数据,这也是完美的。

下面是 git repo 布局文件的链接: 布局文件

68265 次浏览

不要使用 match_parent作为项目视图的高度。一个项目垂直地填充整个屏幕,这样你就看不到另一个项目了。

当您为回收视图创建 row.xml 时,应该遵循以下步骤:

  1. 对于行的高度,始终使用“ wrash _ content”,否则在“ match _ father”中,它将占据整个屏幕的一行。

  2. 也可以用 dp 表示高度。

1)如果你的回收景观是垂直的,那么设置回收景观 match_parentrow_item.xml的高度也是 match_parent

2)如果你的回收视图是水平的,那么设置回收视图的宽度 match_parentrow_item.xml宽度也 match_parent

例如:- 水平回收视图

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:layout_marginRight="60dp" />

Row _ item. xml

<TextView
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:background="@drawable/rect"
android:gravity="center"
android:maxLines="2"
android:padding="10dp"
android:textColor="@color/white"
android:textSize="25sp" />

尝试将项目视图中使用的布局更改为 FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item"
android:layout_width="match_parent"
android:layout_height="?listPreferredItemHeight"
android:clickable="true"
android:focusable="true"
android:foreground="?selectableItemBackground">


<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"/>
</FrameLayout>

Content _ main.xml 如下所示

android:layout_width="match_parent"
android:layout_height="match_parent"

IN row _ list.xml 文件进行以下更改

android:layout_width="match_parent"
android:layout_height="wrap_content"

我做了以上的改变,它运行。

我的错误是我不小心使用了:

LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)

而不是:

LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

检查日志后,确保列表中添加了多个项目,但在 UI 中,只显示1个项目意味着检查这个属性。 在 item _ xml 文件中,将属性更改为

方向正确

 android:layout_width="match_parent"
android:layout_height="wrap_content"

错误的方法

 android:layout_width="match_parent"
android:layout_height="match_parent"

我今天有这个问题,在1001分钟后,我发现这个问题来自于回收视图中的这条线:

android:layout_marginTop="10dp"

我的回收视图被放在了 NestedScrollView 中,我认为这是间接原因。 因此,我把这里的任何人遇到同样的问题。 < a href = “ https://i.stack.imgur.com/7YAki.png”rel = “ nofollow noReferrer”> 这里的图像