【发布时间】:2018-01-15 15:32:38
【问题描述】:
当我将 RecyclerView 添加到布局时,它以垂直顺序显示为列表视图。我为此使用tools:listitem。有没有办法让它在 Android Studio 编辑器中显示为网格而不是列表?
【问题讨论】:
标签: android android-studio android-recyclerview
当我将 RecyclerView 添加到布局时,它以垂直顺序显示为列表视图。我为此使用tools:listitem。有没有办法让它在 Android Studio 编辑器中显示为网格而不是列表?
【问题讨论】:
标签: android android-studio android-recyclerview
您可以使用xmlns:tools="http://schemas.android.com/tools" 命名空间构建预览。
AndroidX[About]
<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/* resources预定义一个数据
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>
因此,您的预览将如下所示
【讨论】:
androidx.recyclerview.widget.GridLayoutManager。
这对我有用(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" />
【讨论】:
要在预览中水平显示列表,只需使用这两个属性
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"/>
【讨论】:
如果您只想在预览中查看效果而不更改应用行为,您可以像使用 listitem 一样使用“工具”命名空间:
<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"/>
【讨论】:
使用
app:layoutManager="GridLayoutManager"
app:spanCount="3"
tools:listitem="@layout/table_grid_item"
【讨论】: