在Android Studio编辑器中,有没有一种方法可以显示RecyclerView内容的网格预览?

64
当我将RecyclerView添加到布局中时,它以垂直顺序显示为列表视图。我正在使用tools:listitem来实现这个功能。有没有一种方法可以在Android Studio编辑器中以网格形式而不是列表形式显示它?
5个回答

76

Android RecycrerView 预览

您可以使用xmlns:tools="http://schemas.android.com/tools"命名空间构建预览。

关于 AndroidX,请参阅[相关]

<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="7"
    tools:orientation="vertical"
    tools:scrollbars="vertical"
    tools:spanCount="4"/>

支持

<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="match_parent"

    tools:layoutManager="android.support.v7.widget.GridLayoutManager"
    tools:listitem="@layout/item"
    tools:itemCount="7"
    tools:orientation="vertical"
    tools:scrollbars="vertical"
    tools:spanCount="4"/>

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_marginBottom="15dp"
    android:orientation="vertical"
    tools:background="@tools:sample/avatars">

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

</FrameLayout>

因此,您的预览将如下所示

[更多示例]


1
如果您正在使用AndroidX,则现在应使用androidx.recyclerview.widget.GridLayoutManager作为布局管理器。 - Julian Honma

43

如果您只想在预览中看到效果而不更改应用程序行为,可以像使用listitem一样使用“tools”命名空间:

<android.support.v7.widget.RecyclerView
        android:id="@+id/rcv_collection"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layoutManager="android.support.v7.widget.GridLayoutManager"
        tools:spanCount="2"
        tools:listitem="@layout/item_collection"/>

41

这对我有用(AndroidX): 对于3列网格

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rvServiceList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    app:spanCount="3"
    tools:itemCount="20"
    tools:listitem="@layout/row_item_service" />

6

使用

app:layoutManager="GridLayoutManager"
app:spanCount="3"
tools:listitem="@layout/table_grid_item"

5
为了在预览中水平显示列表,只需使用这两个属性。
tools:orientation="horizontal"
tools:layoutManager="android.support.v7.widget.LinearLayoutManager"

这是最终代码

<android.support.v7.widget.RecyclerView
        ...
        tools:listitem="@layout/single_item_layout"
        tools:orientation="horizontal"
        tools:layoutManager="android.support.v7.widget.LinearLayoutManager"/>

寻找网格,但这是用于水平列表。 - Anand Kumar
1
@AnandKumar 请看我上面的alexpfx的回答,他在这里提到了如何显示网格布局管理器,链接在这里:https://dev59.com/BFcO5IYBdhLWcg3wmCr5#47402709 - Sanjeev

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