【问题标题】:RecyclerView does not display any items when it is in a layoutRecyclerView 在布局中不显示任何项目
【发布时间】:2020-06-27 21:35:42
【问题描述】:

我有一个导航抽屉活动,其中包含一个包含 RecyclerView 的片段。如果此 RecyclerView 是此片段中的唯一组件,则它可以完美运行,但我想在 RecyclerView 上方添加一个按钮,因此将 RecyclerView 和按钮插入到 LinearLayout 中。但随后 RecyclerView 已经消失或不再显示任何元素。

如何将此 RecyclerView 嵌入到布局中?

RecyclerView 与这个一起工作:

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/list"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:layout_marginLeft="16dp"
  android:layout_marginRight="16dp"
  android:name="com.company.myapp.ui.ItemFragment"
  app:layoutManager="LinearLayoutManager"
  tools:context=".ui.home.HomeFragment"
  tools:listitem="@layout/fragment_item" />

RecyclerView 已经在这个消失了:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical">

  <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Delete Item" />

  <androidx.recyclerview.widget.RecyclerView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:name="com.company.myapp.ui.ItemFragment"
    app:layoutManager="LinearLayoutManager"
    tools:context=".ui.home.HomeFragment"
    tools:listitem="@layout/fragment_item" />

</LinearLayout>

在此处查看完整项目:https://github.com/jriegraf/MyApp

【问题讨论】:

    标签: java android android-layout android-recyclerview


    【解决方案1】:

    你有这个代码在HomeFragment:

    // Set the adapter
    if (view instanceof RecyclerView) {
      Context context = view.getContext();
      RecyclerView recyclerView = (RecyclerView) view;
      if (mColumnCount <= 1) {
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
      } else {
        recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
      }
      recyclerView.setAdapter(new MyItemRecyclerViewAdapter(DummyContent.ITEMS, mListener));
    }
    

    这是你的问题。一旦你将RecyclerView 包装成LinearLayout,这个

    if (view instanceof RecyclerView)
    

    不再是真的,因为现在,view 的类型是 LinearLayout

    如果视图没有移除recyclerView,可以省略if语句,改下这一行:

    RecyclerView recyclerView = (RecyclerView) view;
    

    进入:

    RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-02
      相关资源
      最近更新 更多