【问题标题】:How to use androidx.recyclerview.widget.RecyclerView with tools:listitem?如何将 androidx.recyclerview.widget.RecyclerView 与工具一起使用:listitem?
【发布时间】:2019-01-12 04:56:30
【问题描述】:

如何使用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"/>

设计标签不显示预览:

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

【问题讨论】:

  • 我复制了您的代码,当我在代码中添加此行 tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager" 时,我遇到了同样的问题(看不到我的项目),因此如果不需要,您可以将其删除跨度>
  • @crammeur,但我有这个问题,没有这条线
  • 我在android studio的显示中看到这个java.lang.ClassNotFoundException: androidx.recyclerview.widget.GridLayoutManager可以解释为什么你不能有显示。所以他无法显示,因为他没有找到这个
  • 你使用implementation 'androidx.recyclerview:recyclerview:1.0.0-beta01'还是implementation 'androidx.recyclerview:recyclerview:1.0.0-alpha1',因为当我使用alpha时我可以看到但当我更改时我看不到预览
  • 尝试清理项目并重建它

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


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2016-06-19
    • 2013-11-17
    • 1970-01-01
    • 1970-01-01
    • 2011-01-08
    • 1970-01-01
    • 2011-02-20
    • 2019-12-18
    • 2016-12-15
    相关资源
    最近更新 更多