Is there a way to show a preview of the contents of a RecyclerView as a Grid in the Android Studio editor?

When I add a RecyclerView to the layout, it displays as a list in vertical order. For this I use tools:listitem. Is there a way for it to appear as a grid, but not as a list in the Android Studio editor?

+25
source share
4 answers

You can create a preview using the namespace 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" />

From Android studio 3.0you can pre-define data through tools:textinitem.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" />

As a result, your preview will look like

More here

+31

, "tools", 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"/>
+33

To display the list horizontally in preview mode, simply use these two attributes

tools:orientation="horizontal"
tools:layoutManager="android.support.v7.widget.LinearLayoutManager"

here is the final code

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

Use

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

Source: https://habr.com/ru/post/1683219/


All Articles