tools:listitem用于扩展RecyclerView的自定义视图

har*_*sim 4 android android-layout android-studio android-recyclerview

我尝试用来tools:listitem在上显示样式正确的样本数据RecyclerView。使用原始视图时,这可以正常工作。

但是,当扩展 RecyclerView创建自己的自定义视图以添加一些功能时,它只是在Android Studio的布局预览中显示了空白区域。

扩展视图时,如何使用视图的所有布局预览内容?

我的视图如下所示:

class TestRecyclerView(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int
) : RecyclerView(context, attrs, defStyle) { }
Run Code Online (Sandbox Code Playgroud)

有任何想法吗?

Vin*_*nce 9

你只需要buildrebuild您的Android Studio中的项目和自定义视图似乎有样本数据。

经过测试,可以正常工作!我的Android Studio是Android 3.3 Beta 3

<com.test.my.TestRecyclerView
        android:id="@+id/your_id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:listitem="@layout/your_item"/>
Run Code Online (Sandbox Code Playgroud)

不要忘了app:layoutManager在您的自定义中添加一个属性RecyclerView

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

  • 我发现了你的问题。您必须在您的recyclerview✔中添加一个“ app:layoutManager”(更新后的帖子) (2认同)