我来这里是因为我根本不了解我的代码无法正常工作。
很快,我的目标是“重新加载”代表列表项的View。由于我的列表项可以在其子项中包含其他列表项,因此我想给一个新的列表项充气,然后将那些子项从旧的项转移到新的项。

我收到“指定的孩子已经有一个父母。您必须首先在该孩子的父母上调用removeView()。”错误,但我确实在孩子的父母上调用了removeView(以某种方式不起作用)(请参阅后面的代码)

这是布局设计的方式(我将删除一些行,以便于阅读):

<?xml version="1.0" encoding="utf-8"?>
 <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">

    <data>
        <variable
            name="mainListItem"
            type="com.plg.lirs.data.LirsDataEntity" />
    </data>

    <LinearLayout android:id="@+id/main_list_item_global_layout">

        <LinearLayout android:id="@+id/main_list_item_parent_layout"
            app:mainListItemParentLayout="@{mainListItem}">

        <!-- contains a bunch of views and stuff, nothing important here -->

        </LinearLayout>

        <LinearLayout android:id="@+id/main_list_item_children_layout"
            android:animateLayoutChanges="true">

        <!-- here are all the children i want to transfer, all the children here are inflated from this layout -->

        </LinearLayout>

    </LinearLayout>

</layout>


现在在这里,我的代码来膨胀这个布局:

/* entity is just a logical class that contains my data
olderView is the old view representing the old list item */
private fun inflateItem(entity: LirsDataEntity, olderView: View? = null) : View {
        val itemBinding = DataBindingUtil.inflate<MainListItemBinding>(inflater, R.layout.main_list_item, null, false, null)
       // the itemBinding.root will be added into the parent's children layout later on, after this function
       // + i've tried with true as attachToParent, doesn't change

       /* HERE is the core of the problem. My goal is : if an olderView is provided, then transfer the children from the old one to the new one */
        if(olderView != null) {
            val olderChildrenLayout = olderView.findViewById<LinearLayout>(R.id.main_list_item_children_layout) // here is the LinearLayout that contains the children
            val children = olderChildrenLayout.children
            children.forEach {
                olderChildrenLayout.removeView(it) // remove the children from the old parent
                itemBinding.mainListItemChildrenLayout.addView(it) // add it to the new parent
                // at this point i get the error
            }
        }

        entity.ui.reset() // not important here
        itemBinding.mainListItem = entity

        /* some listeners are set here */

        return itemBinding.root
    }


谢谢阅读 !

最佳答案

我找出问题所在。
当调用removeView()时,android会尝试对其进行动画处理,从而将子视图放置到一个变量中,该变量包含当前正在被动画化的子代。然后,当尝试更改子视图的父视图(我们希望为空)时,它将检查当前视图是否处于动画状态。诚然,父母没有改变(至少目前,我不知道以后是否会改变)。这就是为什么我们不能调用addView()的原因。

解决方案是存储LayoutTransition类,然后将其设置为null,进行传输,然后将其重置。它不会使孩子动起来,但至少会起作用。

这是使该工作正常的一些代码:


public class JavaUtils {
    public static void transferChildren(@NotNull final ViewGroup depart, @NotNull final ViewGroup arrival) {
        LayoutTransition transition = depart.getLayoutTransition();
        depart.setLayoutTransition(null);
            while(depart.getChildCount() > 0) {
            View c = depart.getChildAt(0);
            depart.removeViewAt(0);
            arrival.addView(c);
        }
        depart.setLayoutTransition(transition);
    }
}


对于Kotlin用户:



fun ViewGroup.transferChildrenTo(arrival: ViewGroup) {
    val transition: LayoutTransition = layoutTransition
    layoutTransition = null
    while (childCount > 0) {
        val c: View = getChildAt(0)
        removeViewAt(0)
        arrival.addView(c)
    }
    layoutTransition = transition
}

09-28 12:33