Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

动态添加数据刷新控件内容不会再崩溃了 看这。。。 #63

Open
ptrtony opened this issue Jul 19, 2018 · 6 comments
Open

Comments

@ptrtony
Copy link

ptrtony commented Jul 19, 2018

动态添加数据刷新控件的时候应该对每个容器进行判断,子控件不为空应该先移除子控件再添加子控件,不然就会出现app闪退崩溃的现象,或者会出现数据叠加显示问题

代码附上:
public void setDropDownMenu(@nonnull List tabTexts, @nonnull List popupViews, @nonnull View contentView) {

    if(tabTexts.size() != popupViews.size()) {
        throw new IllegalArgumentException("params not match, tabTexts.size() should be equal popupViews.size()");
    } else {
        if (this.tabMenuView.getChildCount()>0)this.tabMenuView.removeAllViews();
        for(int i = 0; i < tabTexts.size(); ++i) {
            this.addTab(tabTexts, i);
        }

        if (this.containerView.getChildAt(0)!=null){
            this.containerView.removeViewAt(0);
        }

        this.containerView.addView(contentView, 0);
        this.maskView = new View(this.getContext());
        this.maskView.setLayoutParams(new android.widget.FrameLayout.LayoutParams(-1, -1));
        this.maskView.setBackgroundColor(this.maskColor);
        this.maskView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
               DropDownMenu.this.closeMenu();
            }
        });

        if (this.containerView.getChildAt(1)!=null){
            this.containerView.removeViewAt(1);
        }
        this.containerView.addView(this.maskView, 1);
        this.maskView.setVisibility(8);
        if(this.containerView.getChildAt(2) != null) {
            this.containerView.removeViewAt(2);
        }

        this.popupMenuViews = new FrameLayout(this.getContext());
        this.popupMenuViews.setLayoutParams(new android.widget.FrameLayout.LayoutParams(-1, (int)((float) DisplayUtils.getScreenSize(this.getContext()).y * this.menuHeighPercent)));
        this.popupMenuViews.setVisibility(8);
        this.containerView.addView(this.popupMenuViews, 2);
        if (popupMenuViews.getChildCount()>0)popupMenuViews.removeAllViews();
        for(int i = 0; i < popupViews.size(); ++i) {
            ((View)popupViews.get(i)).setLayoutParams(new android.view.ViewGroup.LayoutParams(-1, -2));
            this.popupMenuViews.addView((View)popupViews.get(i), i);
        }

    }
}
@wwzww
Copy link

wwzww commented Sep 11, 2018

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

@ptrtony
Copy link
Author

ptrtony commented Sep 11, 2018

现在我项目中都是用的这段代码 没出现java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
有可能是你的子View没有移除干净导致的 你在检查下

@ptrtony
Copy link
Author

ptrtony commented Sep 11, 2018

或者调用的时候直接判断containerView有没有子View 如果有调用)containerView.removeAllViews();应该也是可以的

@androidty
Copy link

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
你可以看下你add的view是不是同一个引用

@NanaGithub
Copy link

java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

出现同样的问题,加了以上判断也没用。大概是添加的 子view本身存在父容器吧。比如 SmartRefreshLayout,这种问题该怎么解决。

@NanaGithub
Copy link

#1

看到这个,终于理解了,我的布局是在xml中,这样的话就很不方便了,看来得换种方式了。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants