我想声明并隐藏一些关于活动的观点,但它太复杂并且耗费了大量时间。我可以用循环或其他东西来做这件事吗?
//Startup
//Hide msgs
CardView msgUser1v = (CardView) findViewById(R.id.msgUser1v);
msgUser1v.setVisibility(View.GONE);
CardView msgBot1v = (CardView) findViewById(R.id.msgBot1v);
msgBot1v.setVisibility(View.GONE);
CardView msgUser2v = (CardView) findViewById(R.id.msgUser2v);
msgUser2v.setVisibility(View.GONE);
CardView msgBot2v = (CardView) findViewById(R.id.msgBot2v);
msgBot2v.setVisibility(View.GONE);
CardView msgUser3v = (CardView) findViewById(R.id.msgUser3v);
msgUser3v.setVisibility(View.GONE);
CardView msgBot3v = (CardView) findViewById(R.id.msgBot3v);
msgBot3v.setVisibility(View.GONE);
CardView msgUser4v = (CardView) findViewById(R.id.msgUser4v);
msgUser4v.setVisibility(View.GONE);
CardView msgBot4v = (CardView) findViewById(R.id.msgBot4v);
msgBot4v.setVisibility(View.GONE);
CardView msgUser5v = (CardView) findViewById(R.id.msgUser5v);
msgUser5v.setVisibility(View.GONE);
CardView msgBot5v = (CardView) findViewById(R.id.msgBot5v);
msgBot5v.setVisibility(View.GONE);
答案 0 :(得分:0)
我认为这是一个很好的方法:
List<CardView> cards = new ArrayList<CardView>();
for (int i = 1 ; i <= cardsCount ; i++)
{
int id = getResources().getIdentifier("msgBot"+i+"v", "id", context.getPackageName());
cards.add((CardView) findViewById(id));
}
现在您拥有卡阵列中的所有卡片视图。 如果你想让它们看不见,你可以这样轻松地做到:
for(CardView c : cards){
c.setVisibility(View.INVISIBLE);
}
答案 1 :(得分:0)
如果您的卡片视图位于某个容器(如相对布局)下,那么您可以遍历子项。
int childs = relativeLayout.getChildCount();
for(int i=0; i<childs; i++){
View child = relativeLayout.getChildAt(i);
if(child instanceOf CardView){
child.setVisibility(View.GONE);
}
}
您可以进一步放置更多条件检查以排除某些卡片视图。
如果CardViews
只是容器中的项目,并且您想要隐藏所有这些项目,那么yoi可以简单地设置父布局的可见性。
relativeLayout.setVisibility(View.GONE);