如何通过tools:listitem使用androidx.recyclerview.widget.RecyclerView? [英] How to use androidx.recyclerview.widget.RecyclerView with tools:listitem?
问题描述
如何将androidx.recyclerview.widget.RecyclerView
与tools:listitem
一起使用?
我有这样的布局:
How to use androidx.recyclerview.widget.RecyclerView
with tools:listitem
?
I have this layout:
<?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/recyclerViewActors"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
tools:listitem="@layout/list_item_actor"
tools:itemCount="5"
tools:orientation="horizontal"
tools:scrollbars="horizontal"
tools:spanCount="2"/>
但设计标签不显示预览:
如果我将此布局中的androidx.recyclerview.widget.RecyclerView
更改为ListView
,则预览工作正常:
And if I change androidx.recyclerview.widget.RecyclerView
in this layout to ListView
, the preview works:
推荐答案
从您的代码中看来,您的recyclerview是XML的根元素,并且缺少xmlns:tools中的引用
From your code it seems that your recyclerview is the root element of the XML, and is missing the reference from xmlns:tools
尝试使用另一个根元素,作为约束布局,或者甚至按照 google sunflowerapp :
Try to use another root element, as a constraint layout or even just layout as per example of google sunflowerapp:
<layout 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">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/plant_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingLeft="@dimen/margin_normal"
android:paddingRight="@dimen/margin_normal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:context="com.google.samples.apps.sunflower.GardenActivity"
tools:listitem="@layout/list_item_plant" />
</layout>
这篇关于如何通过tools:listitem使用androidx.recyclerview.widget.RecyclerView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!