Android Recycler View 可以包含非容器视图而不是视图容器

问题描述 投票:0回答:1

所以基本上 Recycler view 会膨胀包裹在(Layout Holder)中的 item View。例如。 对于回收商查看适配器中充气的物品如下:

// list_item.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">
    <Button
        android:id="@+id/option"
        android:layout_height="50dp"
        android:layout_width="match_parent"
        android:text="TESTING"/>
</LinearLayout>

不可以吗

<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <Button
        android:id="@+id/option"
        android:layout_height="50dp"
        android:layout_width="match_parent"
        android:text="TESTING"/>
</merge>

因为 Recycler 视图是容器视图,可以容纳简单的非容器视图和容器视图(如线性布局等) 容器视图 - 可以容纳简单视图(文本视图、按钮等)以及容器视图(如线性布局等)的视图。 简单视图 - 不能包含任何其他视图。

在适配器充气期间,我们将其固定在根部:-

final View itemView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.dialog_locker_open_item, parent, true);

以上给出例外。

它应该合并,但在通货膨胀期间得到预期。

android android-recyclerview recyclerlistview
1个回答
0
投票

每个

ViewHolder
需要包含一个根视图(
ViewHolder
的构造函数参数)所以
RecyclerView
可以在需要时分离和回收它。

它不是为了跟踪每个项目的“多个非容器视图”而设计的(如果您使用

<merge>
标签进行膨胀,就会发生这种情况)。

还有

inflate
attachToParent=true
在概念上是错误的,并且总是会失败,因为
RecyclerView.Adapter
不是负责在
RecyclerView
中布置视图的组件(这是
RecyclerView.LayoutManager
的职责)。

© www.soinside.com 2019 - 2024. All rights reserved.