我是以编程方式创建了很多LinearLayouts并将它们放在另一个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);
}
}
有时,在我的Activity中,我必须重新加载该列表...所以我试图使用linearLayout.removeAllViews();从“linearLayout”(我的XML布局文件中的主要布局集)中删除所有视图,但是它引发了我:
java.lang.IllegalArgumentException: No suitable parent found from the given view. Please provide a valid view.
答案 0 :(得分:0)
我通过以下方式解决了这个问题:
代码:
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);
}
}
当然这可以改进,但基本上,这是我的解决方案(:
答案 1 :(得分:0)
试试此代码
if(linearLayout.getChildCount() > 0)
linearLayout.removeAllViews();