如何从 LinearLayout 中删除所有 LinearLayout?

Posted

技术标签:

【中文标题】如何从 LinearLayout 中删除所有 LinearLayout?【英文标题】:How to delete all LinearLayouts from a LinearLayout? 【发布时间】:2017-06-11 22:19:09 【问题描述】:

我正在以编程方式创建很多 LinearLayout 并将它们放在另一个 LinearLayout 上:

public void loadList()
    LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearShowing);

    if(linearLayout.getChildCount() > 0)
        linearLayout.removeAllViews(); // THIS LINE DOESN'T WORK FOR LINEARLAYOUTS INSIDE LINEARLAYOUTS

    List<Mineral> showMineralsList = dbHandler.getAllMinerais("external");

    for (Mineral mineral : showMineralsList)
        id = mineral.getID();
        //testIds.add(id);
        int favorite = mineral.getFavorite();
        String nome = mineral.getName(),
                formulaQuimica = mineral.getFormulaQuimica();

        LinearLayout linearVertical = new LinearLayout(this);
        linearVertical.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams lvParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        linearVertical.setLayoutParams(lvParams);

        LinearLayout linearHorizontalOuter = new LinearLayout(this);
        linearHorizontalOuter.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams lhoParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        lhoParams.gravity = Gravity.CENTER_VERTICAL;
        linearHorizontalOuter.setLayoutParams(lhoParams);

        LinearLayout linearHorizontalInner = new LinearLayout(this);
        linearHorizontalInner.setOrientation(LinearLayout.HORIZONTAL);
        LinearLayout.LayoutParams lhiParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
        lhiParams.gravity = Gravity.CENTER_VERTICAL;
        linearHorizontalInner.setLayoutParams(lhiParams);

        ImageButton ibStar = new ImageButton(this);
        LinearLayout.LayoutParams ibsParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        ibsParams.setMargins(0, 0, vinteMargin, 0);
        ibStar.setLayoutParams(ibsParams);
        if(favorite == 0)
            ibStar.setBackgroundResource(R.drawable.ic_star_gray_24dp);
        else
            ibStar.setBackgroundResource(R.drawable.ic_star_blue_24dp);
        vId = generateViewId();
        vIds.put(vId, id);
        linearHorizontalInner.setId(vId);

        linearHorizontalInner.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                i = new Intent(Main.this, Detalhes.class);
                i.putExtra("id", vIds.get(view.getId()));
                startActivity(i);
            
        );

        vId = generateViewId();
        vIds.put(vId, id);
        ibStar.setId(vId);
        ibStar.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View view) 
                setFavorite(vIds.get(view.getId()));
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            
        );

        TextView tvName = new TextView(this);
        LinearLayout.LayoutParams tvnParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
        tvName.setText(nome);
        tvName.setLayoutParams(tvnParams);
        tvName.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);

        TextView tvFormula = new TextView(this);
        LinearLayout.LayoutParams tvfParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
        tvFormula.setText(formulaQuimica);
        tvFormula.setLayoutParams(tvfParams);
        tvFormula.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);

        TableLayout.LayoutParams lpDiv = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT, 5f);
        lpDiv.setMargins(0, dezMargin, 0, dezMargin);

        ImageView ivDiv = new ImageView(this);
        ivDiv.setBackgroundResource(R.drawable.divider);
        ivDiv.setLayoutParams(lpDiv);

        linearHorizontalInner.addView(tvName);
        linearHorizontalInner.addView(tvFormula);

        linearHorizontalOuter.addView(ibStar);
        linearHorizontalOuter.addView(linearHorizontalInner);

        linearVertical.addView(ivDiv);
        linearVertical.addView(linearHorizontalOuter);

        linearLayout.addView(linearVertical);
    

有时,在我的活动中,我必须重新加载该列表...所以我试图使用 linearLayout.removeAllViews(); 从“linearLayout”(我的 XML 布局文件上设置的主布局)中删除所有视图,但是它把我扔了:

java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.

【问题讨论】:

【参考方案1】:

试试这个代码

if(linearLayout.getChildCount() > 0)
        linearLayout.removeAllViews();

【讨论】:

大声笑,这是我发布的代码的第三行......我已经用我的答案解决了这个问题......谢谢!【参考方案2】:

我通过以下方式解决了这个问题:

为我的“linearVertical”(对应于我的主要父级 LinearLayout 的第一个子级)设置一个 ID; 每次创建新的 linearVertical 时都会将此 ID 添加到列表中; 迭代此列表并将在该列表中具有其 ID 的所有视图的可见性设置为 GONE。

代码:

private ArrayList<Integer> layoutsIds = new ArrayList<>();

public void loadList()
    resetViews();
    (...)
    (...)
    layoutId = generateViewId();
    linearVertical.setId(layoutId);
    layoutsIds.add(layoutId);
    (...)
    (...)


public void resetViews()
    for(int a=0; a<layoutIds.size(); a++)
        LinearLayout toRemove = (LinearLayout)findViewById(layoutIds.get(a));
        toRemove.setVisibility(View.GONE);
    

当然这可以改进,但是,基本上,这是我的解决方案(:

【讨论】:

以上是关于如何从 LinearLayout 中删除所有 LinearLayout?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 LinearLayout 中删除 ImageView

如何从 Android 中的 RemoteViews 中删除小部件?

从Android中的LinearLayout中删除视图时出现异常

如何从以前的 li 中删除类“show”?

如何在android的linearlayout中删除阴影?

拖放而不从所有者中删除