android - 如何通过工具:listitem?使用androidx.recyclerview.widget.RecyclerView

标签 android android-recyclerview preview androidx android-tools-namespace

如何使用 androidx.recyclerview.widget.RecyclerViewtools: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"/>

但是设计标签不显示预览:

enter image description here

如果我将此布局中的 androidx.recyclerview.widget.RecyclerView 更改为 ListView,则预览有效:

enter image description here

最佳答案

从您的代码看来,您的 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>

关于android - 如何通过工具:listitem?使用androidx.recyclerview.widget.RecyclerView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51687173/

相关文章:

android - 项目单击时的 RecyclerView 动画

android - 出于性能目的,在 AppCompat 1.4.0 及更高版本的 textview 中禁用 emoji2

android - 滚动到 NestedScrollView 内的 recyclerview 中的位置?

macos - 如何在底层文件被修改时自动刷新在 macOS 预览中打开的 PDF 文档?

email - 如何在不同的电子邮件客户端上模拟 HTML 的预览

Android Studio - 在 SYNC 后执行的 gradle 任务

javascript - 防止 Android 上的 native Twitter 应用程序打开推文 URL

android - 如何播放 ftp 流媒体文件中的视频?

android - 如何在 ListView MySql -Android 上实现搜索对话框

android - 卡住相机预览的方法