How to use androidx.recyclerview.widget.RecyclerView with tools:listitem?
Asked Answered
K

1

10

How to use androidx.recyclerview.widget.RecyclerView with tools:listitem? I have this layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/recyclerViewActors"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    tools:listitem="@layout/list_item_actor"
    tools:itemCount="5"
    tools:orientation="horizontal"
    tools:scrollbars="horizontal"
    tools:spanCount="2"/>

but Design tab doesn't show preview:

enter image description here

And if I change androidx.recyclerview.widget.RecyclerView in this layout to ListView, the preview works:

enter image description here

Kylie answered 4/8, 2018 at 15:43 Comment(5)
I copy past your code and is when I add this line tools:layoutManager="androidx.recyclerview.widget.GridLayoutManager" in my code I have the same issue (don't see my items) so you can remove it if is not neededFlynn
@crammeur, but I have this issue and without this lineKylie
I see in the display of android studio this java.lang.ClassNotFoundException: androidx.recyclerview.widget.GridLayoutManager can explain why you can't have the display. So he can't display because he not find thisFlynn
Do you use implementation 'androidx.recyclerview:recyclerview:1.0.0-beta01' or implementation 'androidx.recyclerview:recyclerview:1.0.0-alpha1' because when I use alpha I can see but when I change I don't see previewFlynn
try to clean the project and rebuild itRhombohedron
N
19

From your code it seems that your recyclerview is the root element of the XML, and is missing the reference from xmlns:tools

Try to use another root element, as a constraint layout or even just layout as per example of google sunflowerapp:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/plant_list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:paddingLeft="@dimen/margin_normal"
            android:paddingRight="@dimen/margin_normal"
            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
            tools:context="com.google.samples.apps.sunflower.GardenActivity"
            tools:listitem="@layout/list_item_plant" />

</layout>
Natasha answered 1/1, 2019 at 12:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.