[英]RecyclerView does not display any items when it is in a layout
I have a navigation drawer activity that contains a fragment that contains a RecyclerView.我有一个导航抽屉活动,其中包含一个包含 RecyclerView 的片段。 This RecyclerView works perfectly if it is the only component in this fragment, but I want to add a button above the RecyclerView and therefore insert the RecyclerView and the button in a LinearLayout.
如果此 RecyclerView 是此片段中的唯一组件,则它可以完美运行,但我想在 RecyclerView 上方添加一个按钮,因此将 RecyclerView 和按钮插入到 LinearLayout 中。 But then the RecyclerView has disappeared or no longer shows any elements.
但随后 RecyclerView 消失或不再显示任何元素。
How can I embed this RecyclerView in a layout?如何将此 RecyclerView 嵌入到布局中?
RecyclerView works with this one: 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 has disappeared in this one: 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>
See complete project here: https://github.com/jriegraf/MyApp在此处查看完整项目: https://github.com/jriegraf/MyApp
You have this code in HomeFragment
:您在
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));
}
This is your problem.这是你的问题。 Once you wrap
RecyclerView
into LinearLayout
, this一旦你将
RecyclerView
包装到LinearLayout
中,这
if (view instanceof RecyclerView)
isn't true any more, because now, view
's type is LinearLayout
.不再是真的,因为现在,
view
的类型是LinearLayout
。
If the view doesn't remove the recyclerView, you can ommit the if
statement, and change this line:如果视图没有移除 recyclerView,你可以省略
if
语句,并更改此行:
RecyclerView recyclerView = (RecyclerView) view;
into:进入:
RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.list);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.