我有一个具有不同类型的 ViewHolders 的 RecyclerView,它们具有不同的布局资源。有没有办法指定 tools:listItem 命名空间中的所有布局?如果我像这样指定所有 listItems 布局:
<androidx.recyclerview.widget.RecyclerView
......
tools:listitem="@layout/layout_1"
tools:listitem="@layout/layout_2"
tools:listitem="@layout/layout_3" />
Run Code Online (Sandbox Code Playgroud)
由于重复的项目,我收到了错误。我可以做这样的事情:
<androidx.recyclerview.widget.RecyclerView
......
tools:listitem0="@layout/layout_1"
tools:listitem1="@layout/layout_2"
tools:listitem2="@layout/layout_3" />
Run Code Online (Sandbox Code Playgroud)
不会出现错误,但打开布局的单击功能会丢失,因为命名空间中没有此类声明。使用tools:showIn时也是一样的。
\ tools:listitemxc2\xa0attribute 仅接受单个项目类型,因此一种可能且简单的方法是使用以下命令在单独的 xml 文件中定义所有不同的布局<include/>带有 LinearLayout 父级的标签在单独的 xml 文件中定义所有不同的布局。
1.创建res->layout->item_multiple_layouts.xml并使用 Vertical LinearLayout 按照您希望在 RecyclerView 中出现的顺序定义所有布局:
\n<?xml version="1.0" encoding="utf-8"?>\n<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"\n android:layout_width="match_parent"\n android:layout_height="wrap_content"\n android:orientation="vertical">\n\n <include layout="@layout/item_type_1" />\n <include layout="@layout/item_type_2" />\n <include layout="@layout/item_type_3" />\n <include layout="@layout/item_type_1" />\n <include layout="@layout/item_type_2" />\n <include layout="@layout/item_type_3" />\n\n</LinearLayout>\nRun Code Online (Sandbox Code Playgroud)\n2.要在 RecyclerView 中预览上述布局,您可以使用 itemCount=1 指向上述布局,以便仅渲染它们一次,如下所示:
\n<androidx.recyclerview.widget.RecyclerView\n tools:listitem="@layout/item_multiple_layouts"\n tools:itemCount="1"/>\nRun Code Online (Sandbox Code Playgroud)\n
小智 3
我认为没有任何干净的方法可以做到这一点。如果您想预览多个项目的相同布局,可以执行以下操作:
tools:listitem="@layout/layout_1"
tools:itemCount="5"
Run Code Online (Sandbox Code Playgroud)
为此,有一种不同的方法来实现自定义 ViewFlipper。但我认为这太过分了。我将只运行应用程序而不是编写代码:P
这是链接: https://medium.com/@AllanHasekawa/previewing-multiples-item-types-in-a-recyclerview-163aebc2f34a
| 归档时间: |
|
| 查看次数: |
708 次 |
| 最近记录: |