DropDownMenu
DropDownMenu copied to clipboard
添加popuwindow的时候报错
The specified child already has a parent. You must call removeView() on the child's parent first.
View doubleList = LayoutInflater.from(this).inflate(R.layout.view_double_listview,null); mainlist = (ListView)doubleList.findViewById(R.id.classify_mainlist); morelist = (ListView)doubleList.findViewById(R.id.classify_morelist); mainAdapter = new ClassifyMainAdapter(this, mainDataList); mainAdapter.setSelectItem(0); mainlist.setAdapter(mainAdapter); mainlist.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { initAdapter(Constant.MORELISTTXT[position]); mainAdapter.setSelectItem(position); mainAdapter.notifyDataSetChanged(); } }); mainlist.setChoiceMode(ListView.CHOICE_MODE_SINGLE); initAdapter(Constant.MORELISTTXT[0]); morelist.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView> parent, View view, int position, long id) { moreAdapter.setSelectItem(position); moreAdapter.notifyDataSetChanged(); } }); //init popupViews popupViews.add(doubleList); popupViews.add(doubleList); //内容区域 TextView contentView = new TextView(this); contentView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); contentView.setText("内容显示区域"); contentView.setGravity(Gravity.CENTER); contentView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20); popupViews.add(contentView);
//设置Dropmenu
dropDownMenu.setDropDownMenu(Arrays.asList(headers), popupViews, contentView);
应该是重复调用了setDropDownMenu方法,如果嵌套在ViewPager里面很可能出现
只用了一次setDropDownMenu啊,没有使用viewpager,下边的内容还是用的一个TextView contentView = new TextView(this); 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); }
说是在 popupMenuViews.addView(popupViews.get(i), i);添加布局的时候出现了错误,让先移除布局
我也出现了这个问题 我的这个下面是个RecyclerView
我也是用的RecycerView,我又把代码重新写了一遍,之前的错误没了,由于最近在做工程,具体是哪出了问题也没有去深究
@Bind(R.id.lawyer_ll) LinearLayout lawyerLl;
lawyerDrop.setDropDownMenu(Arrays.asList(headers), popupViews, lawyerLl);
上面这句话报错:The specified child already has a parent. You must call removeView() on the child's parent first.
lawyerLl这个是内容区域吧,
View contentView = LayoutInflater.from(this).inflate(R.layout.view_recyclerview, null);
mDropDownMenu.setDropDownMenu(Arrays.asList(headers), popupViews, contentView);
我是这样写的
你用include把你的recyclerview包起来了是吗?
没有啊,就是加载一个单独的布局作为内容显示区域
OK 我这边也可以了 谢了 老哥
^_^
清空View集合再添加元素
//init popupViews popupViews.clear(); popupViews.add(typeView); popupViews.add(priceView); popupViews.add(cityView);
否则执行
case 5: {//退出登录成功 endProgressDialog(); Toast.makeText(MainActivity.this, "退出登录成功", Toast.LENGTH_LONG).show(); bottomNavigation.setCurrentItem(0); viewPager.setAdapter(adapter); } break;
退出登录重新切换到第一个Fragment的时候popupViews会重复添加元素,导致popupViews和headers的元素数目不一致抛出异常
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()"); }
导致APP闪退。
你是每次回到第一个fragment都要重新加载数据吗
mDropDownMenu.setDropDownMenu(Arrays.asList(headers), popupViews, recyclerView);
The specified child already has a parent. You must call removeView() on the child's parent first.
这个错误有人解决了吗
我这里也出现了这样的错误,我是通过这么修改的: 在setDropDownMenu(){ ... 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); if (popupMenuViews.getChildCount()>0) popupMenuViews.removeAllViews(); for (int i = 0; i < popupViews.size(); i++) { popupViews.get(i).setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); parentRemove(popupViews.get(i));//关键是这一句,这是我自定义的一个方法 popupMenuViews.addView(popupViews.get(i), i); } ... } public void parentRemove(View v) { ViewGroup parent = (ViewGroup) v.getParent(); if (parent != null) { parent.removeAllViews(); } }