【问题标题】:tools:listitem for custom view extending RecyclerViewtools:listitem 用于扩展 RecyclerView 的自定义视图
【发布时间】:2019-04-11 18:57:21
【问题描述】:

我尝试使用tools:listitemRecyclerView 上显示正确样式的样本数据。这在使用原始视图时效果很好。

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

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

我的视图如下所示:

class TestRecyclerView(
    context: Context,
    attrs: AttributeSet? = null,
    defStyle: Int
) : RecyclerView(context, attrs, defStyle) { }

有什么想法吗?

【问题讨论】:

  • 预览似乎适用于其他默认视图,例如TextView(一个自定义的EditText 就像一个普通的一样呈现)。也许它与设计时间属性的实现有关?例如。他们检查RecyclerView 而不是T extends RecyclerView

标签: android android-studio android-layout android-recyclerview


【解决方案1】:
<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"/>

不要忘记在您的自定义RecyclerView 中添加app:layoutManager 属性!

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

【讨论】:

  • 不,这不起作用。未构建时,布局预览会显示一个灰色区域,其中包含视图的名称。建成后,它会显示一个白色/空白区域。如前所述,它似乎适用于 TextView(可能还有其他)的简单扩展,但不适用于扩展 RecyclerView 上的 tools:listitem。或者我的TestRecyclerView 真的对你有用吗?
  • 我无法让它工作。我创建了一个空白演示项目 (github.com/hardysim/custom-RecyclerView) 来说明它。我用 Android Studio 3.2 stable 和 3.3 Beta 3 测试过。请看一下。
  • 好的!我去试试
  • 我发现了你的问题。您必须在您的回收站视图中添加app:layoutManager ✔(更新后的帖子)
  • 就是这样。接得好!谢谢。
猜你喜欢
  • 2023-02-02
  • 2019-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多