IllegalStateException: The specified child already has a parent问题解决办法

10 篇文章 0 订阅


       我在使用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));
 
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); } }


dropDownMenu.setDropDownMenu(Arrays.asList(headers), popupViews, mRecyclerViewDelivery);

 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值