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

自古美人都是妖i 提交于 2019-12-04 22:21:07

问题


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:

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


回答1:


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>


来源:https://stackoverflow.com/questions/51687173/how-to-use-androidx-recyclerview-widget-recyclerview-with-toolslistitem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!