有没有办法在Android Studio编辑器中显示RecyclerView的内容预览为网格?

Ana*_*mar 27 android android-studio android-recyclerview

当我将RecyclerView添加到布局时,它以垂直顺序显示为列表视图.我正在使用tools:listitem这个.有没有办法,它在Android Studio编辑器中显示为网格而不是列表?

Tou*_*hid 38

这对我有用(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" />
Run Code Online (Sandbox Code Playgroud)


ale*_*pfx 34

如果您只想在预览中看到效果而不更改应用程序行为,您可以像使用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"/>
Run Code Online (Sandbox Code Playgroud)


yoA*_*ex5 33

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

<!-- AndroidX -->
<androidx.recyclerview.widget.RecyclerView
    tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"

<!-- support -->
<android.support.v7.widget.RecyclerView
    tools:layoutManager="android.support.v7.widget.GridLayoutManager"

    <!-- common -->
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:itemCount="5"
    tools:listitem="@layout/item"
    tools:orientation="horizontal"
    tools:scrollbars="horizontal"
    tools:spanCount="2" />
Run Code Online (Sandbox Code Playgroud)

Android studio 3.0您可以通过tools:text中预定义数据item.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    tools:text="@tools:sample/last_names"
    tools:textColor="@android:color/white" />
Run Code Online (Sandbox Code Playgroud)

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

请在此处阅读更多内容 - https://android.jlelse.eu/android-tools-attributes-listitem-sample-data-rocks-bbf49aaa9f07


Ana*_*mar 7

使用

app:layoutManager="GridLayoutManager"
app:spanCount="3"
tools:listitem="@layout/table_grid_item"
Run Code Online (Sandbox Code Playgroud)


San*_*eev 5

要在预览中水平显示列表,只需使用这两个属性

tools:orientation="horizontal"
tools:layoutManager="android.support.v7.widget.LinearLayoutManager"
Run Code Online (Sandbox Code Playgroud)

这是最终的代码

<android.support.v7.widget.RecyclerView
        ...
        tools:listitem="@layout/single_item_layout"
        tools:orientation="horizontal"
        tools:layoutManager="android.support.v7.widget.LinearLayoutManager"/>
Run Code Online (Sandbox Code Playgroud)