如何通过tools:listitem使用androidx.recyclerview.widget.RecyclerView?

Kse*_*nia 7 android preview android-recyclerview android-tools-namespace androidx

如何搭配androidx.recyclerview.widget.RecyclerView使用tools:listitem?我有这样的布局:

<?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"/>
Run Code Online (Sandbox Code Playgroud)

但“ 设计”选项卡不显示预览:

在此处输入图片说明

而且,如果我androidx.recyclerview.widget.RecyclerView将此布局更改为ListView,则预览工作正常:

在此处输入图片说明

Tit*_*nde 11

从您的代码看来,您的recyclerview是XML的根元素,并且缺少xmlns:tools的引用。

尝试使用另一个根元素,作为约束布局,或者甚至按照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>
Run Code Online (Sandbox Code Playgroud)