IllegalStateException: The specified child already has a parent,removeview()问题解决办法


       我在使用DropDownMenu的时候将布局文件的recycleview传进setDropDownMenu的方法里面,出现了IllegalStateException :The specified child already has a parent. You must call removeView() 的错误。原来发现我的Activity的recycleview和我传进方法里面用到的view的parent是不一致的,所以需要我在activity里面new一个recycleview。它的parent是不属于activity的。贴出关键代码入选如下 :
 final RecyclerView mRecyclerViewDelivery =new RecyclerView(this);
        mRecyclerViewDelivery.setLayoutManager(new LinearLayoutManager(mcontext));
        mRecyclerViewDelivery.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.VERTICAL, 6, getResources().getColor(R.color.black)));
        mDeliveryPresenter = new DeliveryPresenter(mcontext);
        mDeliveryPresenter.initDeliveryList(mRecyclerViewDelivery);
        mRecyclerViewDelivery.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        
        dropDownMenu.setDropDownMenu(Arrays.asList(headers), popupViews, mRecyclerViewDelivery);

 
 
        
public void setDropDownMenu(@NonNull List<String> tabTexts, @NonNull List<View> popupViews, @NonNull View contentView) {
        if (tabTexts.size() != popupViews.size()) {
            throw new IllegalArgumentException("params not match, tabTexts.size() should be equal popupViews.size()");
        }

        for (int i = 0; i < tabTexts.size(); i++) {
            addTab(tabTexts, i);
        }
        if (containerView.getChildAt(0) != null){
            containerView.removeViewAt(0);
        }
        containerView.addView(contentView, 0);
        maskView = new View(getContext());
        maskView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));
        maskView.setBackgroundColor(maskColor);
        maskView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                closeMenu();
            }
        });
        if (containerView.getChildAt(1) != null){
            containerView.removeViewAt(1);
        }
        containerView.addView(maskView, 1);
        maskView.setVisibility(GONE);
        if (containerView.getChildAt(2) != null){
            containerView.removeViewAt(2);
        }
        popupMenuViews = new FrameLayout(getContext());
        popupMenuViews.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (int) (DeviceUtils.getScreenSize(getContext()).y*menuHeighPercent)));
        popupMenuViews.setVisibility(GONE);
        containerView.addView(popupMenuViews, 2);

        for (int i = 0; i < popupViews.size(); i++) {
            popupViews.get(i).setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            popupMenuViews.addView(popupViews.get(i), i);
        }

    }

 
 



猜你喜欢

转载自blog.csdn.net/qq_28670711/article/details/76529203