【问题标题】:Recycler view showing single item显示单个项目的回收站视图
【发布时间】:2016-08-03 06:04:22
【问题描述】:

我遇到了一个奇怪的错误,recyclerview 只显示一个项目。下面是我的 recyclerview 适配器的代码:

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 布局文件的链接: layout files

【问题讨论】:

标签: android android-recyclerview


【解决方案1】:

不要使用match_parent 作为项目视图的高度。一项垂直填满整个屏幕,因此您看不到另一项。

【讨论】:

  • Google 请添加一些 Lint 警告,这次让你烦人的 lint 有用!
  • 哦,谢谢,了不起的人,在我的例子中,我创建了 match_parent 作为高度的项目。因此,所有屏幕都保留在第一项之后。
  • 让我给你买杯咖啡!你救了我的理智!谢谢!
  • 对我不起作用。只有将Button 放在RecyclerView 之前,才会显示所有项目。有什么建议吗?
【解决方案2】:

当您为 recyclerview 创建 row.xml 时,应遵循以下几点:

  1. 始终使用“wrap_content”作为行高,否则在“match_parent”中它将占据整个屏幕的单行。

  2. 你也可以用dp取高度。

【讨论】:

  • 我有同样的问题尝试滚动并看到了其余的项目
【解决方案3】:

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

LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)

代替:

LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

【讨论】:

  • 这也是我的错误。谢谢你。
【解决方案4】:

尝试将项目视图中使用的布局更改为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>

【讨论】:

    【解决方案5】:

    content_main.xml如下

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

    在 row_list.xml 文件中进行以下更改

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

    我做了上面的更改,它运行。

    【讨论】:

      【解决方案6】:

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

      正确的方法

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

      方法错误

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

      【讨论】:

        【解决方案7】:

        我今天遇到这个问题,1001 分钟后,我发现这是来自 RecyclerView 中的这一行:

        android:layout_marginTop="10dp"
        

        我的 RecyclerView 放在 NestedScrollView 里面,我认为这是间接原因。 所以我把这里放在这里让其他人遇到同样的问题。 here the image

        【讨论】:

          【解决方案8】:

          1) 如果你的 recyclerview 是垂直的,那么设置 recyclerview 的高度 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" />
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-09-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2021-03-03
            相关资源
            最近更新 更多