在Android Studio编辑器中是否有一种方法可以预览RecyclerView的内容?

344

当我将RecyclerView添加到布局中时,它会显示为空白屏幕。是否有一种方式,如通过tools命名空间,在RecyclerView内容的预览中显示?

5个回答

713

@oRRs是正确的!

我正在使用Android Studio 1.4 RC2,现在你可以指定任何自定义布局。

我尝试了一个自定义的CardView,它能够工作。

tools:listitem="@android:layout/simple_list_item_checked"

1
请参考 @oRRs 的评论: tools:listitem="@android:layout/simple_list_item_checked" - Philippe David
2
有没有办法以网格模式预览? - user3473590
31
尝试这个: app:layoutManager="GridLayoutManager" (应用栅格布局管理器) tools:listitem="@layout/layout_list_item_select_seat"(工具列表项为“选择座位”布局) app:spanCount="5"(栅格列数为5列) - atabouraya
4
如果您也希望将方向设置为水平方向,您可以使用以下代码:tools:orientation="horizontal" - android developer
4
除了指定 tools:orientation="horizontal"android:orientation="horizontal" 外,根据 https://dev59.com/uVsV5IYBdhLWcg3w4iFA,我还需要指定 app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" - Michael Osofsky
显示剩余6条评论

173

Android 工具和布局管理器

tools 命名空间可以启用设计时功能(例如在片段中显示哪个布局)或编译时行为(例如将哪种缩小模式应用于您的 XML 资源)。这是一个非常强大的特性,它正在不断发展,允许您不必每次编译代码就可以查看更改。

AndroidX[关于]GridLayoutManager

implementation 'androidx.recyclerview:recyclerview:1.1.0'
<androidx.recyclerview.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    tools:listitem="@layout/item"
    tools:itemCount="10"
    tools:orientation="vertical"
    tools:scrollbars="vertical"
    tools:spanCount="3"/>

支持库LinearLayoutManager

implementation 'com.android.support:recyclerview-v7:28.0.0'

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    tools:layoutManager="android.support.v7.widget.LinearLayoutManager"
    tools:listitem="@layout/item"
    tools:itemCount="3"
    tools:orientation="horizontal"
    tools:scrollbars="horizontal" />

在Android Studio 3.0中引入的另一个很酷的功能是通过工具属性预定义数据,使用@tools:sample/*资源来轻松可视化您的布局结构。

item.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="100dp"
    android:layout_height="150dp"
    android:layout_marginRight="15dp"
    android:layout_marginBottom="10dp"
    android:orientation="vertical"
    tools:background="@tools:sample/backgrounds/scenic">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorWhite"
        tools:text="@tools:sample/first_names" />

</FrameLayout>

模拟器结果:


2
这应该被标记为答案,因为它详细并且与RecylerView兼容。ListViews现在已经不再使用了。 - Abhinav Saxena
我不得不切换到属性区中的较少属性选项卡,才能看到listitem选项,我可以直接在XML代码中输入它! - George Udosen
1
如果您正在使用自己的列表项布局,并且只看到一个列表项,则请检查布局中的layout_height="wrap_content"。 - Jeffrey
1
有趣的是,只有在使用ListView而不是RecyclerView时才能正常工作。有什么想法吗?我复制粘贴以确保我没有在我的RecyclerView中搞砸了什么。但实际上它可以工作,所以这是一个有效的XML。 - xarlymg89
"@tools:sample/*" 是一种保留的 Android 资源类型,您可以将其注入到布局中作为占位数据。last_names - 常见的姓氏。完整的官方文档请参考:https://developer.android.com/studio/write/tool-attributes#toolssample_resources - yoAlex5
感谢使用工具:itemCount! - Mikkel Larsen

7
首先,在您的项目 XML 中添加以下行,以在编辑项目时预览列表:
tools:showIn="@layout/activity_my_recyclerview_item"

接下来,在RecyclerView的XML文件中添加以下代码,以预览列表中的项目外观:

tools:listitem="@layout/adapter_item"

showIn 很好用 - Erdal G.

4

如果您已经有一个custom_item布局:

<androidx.recyclerview.widget.RecyclerView
             android:id="@+id/recyclerView"
             ...
             ...
             **tools:listitem="@layout/name_of_your_custom_item_view"**
             ...>
</androidx.recyclerview.widget.RecyclerView>

3

从Android Studio 1.3.1开始,在预览中显示默认列表项,但目前还不能指定自己的列表项。希望将来会实现。


20
在AS 1.4中,您可以从几个预定义布局中进行选择,例如:tools:listitem="@android:layout/simple_list_item_checked"。只需在布局编辑器中右键单击RecyclerView,然后选择“预览列表内容”。不幸的是,您仍然不能将其用于自己的布局,至少对我来说会引发渲染错误。 - oRRs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接