DataBinding与RecycleView异常:ParentView已存在

时间:2018-03-29 15:12:09

标签: android android-recyclerview recycler-adapter android-databinding

当我尝试将其与回收视图一起使用或任何需要扩展layout.xml的情况时,我对DataBinding如此疑惑

我正在尝试将循环视图与与DataBinding对象连接的layout.xml连接起来。我做了这些步骤。

1)回收视图持有者。

public class StepperViewHolder extends RecyclerView.ViewHolder{
    public final StepperViewBinding STEPPER_VIEW;

    StepperViewHolder(@NonNull StepperViewBinding stepperViewBinding) {
        super(stepperViewBinding.getRoot());
        STEPPER_VIEW = stepperViewBinding;
    }
}

2)OnCreateViewHolder

@Override
public StepperViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    StepperViewBinding stepperViewBinding = DataBindingUtil.setContentView(mActivity, R.layout.stepper_view);
    return new StepperViewHolder(stepperViewBinding);
}

现在,这个例外正在抛出。

  

java.lang.IllegalStateException:指定的子级已有父级。您必须首先在孩子的父母上调用removeView()。                                                                                          在android.view.ViewGroup.addViewInner(ViewGroup.java:4309)                                                                                          在android.view.ViewGroup.addView(ViewGroup.java:4145)                                                                                          在android.view.ViewGroup.addView(ViewGroup.java:4086)                                                                                          在android.support.v7.widget.RecyclerView $ 5.addView(RecyclerView.java:746)                                                                                          在android.support.v7.widget.ChildHelper.addView(ChildHelper.java:107)

注意 之前当我尝试使用类似于DataBinding视图的回收视图进程的库时,我得到了这个异常。 我通过在addView()方法[在库代码中]之前删除视图父项来解决此异常。

在此异常情况下,addView()方法发生了同样的错误。 那么为什么会发生这种情况,我该如何避免呢?

除此之外,我尝试在将视图父级传递给ViewHolder之前删除视图父级,并且循环视图也可以工作,并且调用两种方法onCreateView() - onBindView(),但在移动屏幕上没有显示的视图! ! 这是我遵循的步骤: -

1)

private void checkParentView(StepperViewBinding stepperViewBinding){
    if(stepperViewBinding.getRoot().getParent() != null){
        ((ViewGroup)stepperViewBinding.getRoot().getParent()).removeView(stepperViewBinding.getRoot());
    }
}

2)

@Override
public StepperViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    StepperViewBinding stepperViewBinding = DataBindingUtil.setContentView(mActivity, R.layout.stepper_view);
    // To avoid IllegalStateException.
    // State: The specified child already has a parent. You must call removeView() on the child's parent first.
    checkParentView(stepperViewBinding);
    return new StepperViewHolder(stepperViewBinding);
}

解决 通过这种方式创建绑定视图的实例。

StepperViewBinding stepperViewBinding = DataBindingUtil.inflate(LayoutInflater.from(mActivity),
            R.layout.stepper_view, parent, false);

1 个答案:

答案 0 :(得分:0)

我相信你使用了DataBindingUtil的错误方法。从阅读文档时,setContentView()只应在您想要更改Activity的内容视图时使用,这在RecyclerView中不适用。相反,我认为你应该使用the inflate() method

@Override
public StepperViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    StepperViewBinding stepperViewBinding = DataBindingUtil.inflate(inflater, R.layout.stepper_view, parent, false);
    return new StepperViewHolder(stepperViewBinding);
}