刚好今天做项目,要实现一个功能,顺便也学习使用了查找某个Activity的view,废话不说直接上代码
//找到某个VIEW
case R.id.myorder_cancel:
//获取ListView
ListView lv = (ListView)findViewById(R.id.myorder_list);
//查找其下的子view个数,并根据子view数量遍历
for (int i = 0; i < lv.getChildCount(); i++) {
View v = lv.getChildAt(i);
//查找是否有RelativeLayout
if ( v instanceof RelativeLayout){
RelativeLayout rlayout = (RelativeLayout)lv.getChildAt(i);
//同上遍历
for (int j = 0; j < rlayout.getChildCount(); j++) {
View vv = rlayout.getChildAt(j);
//查找在RelativeLayout布局中是否有ImageView
if ( vv instanceof ImageView){
ImageView myImageView = (ImageView)rlayout.getChildAt(j);
if(myImageView.getId() == R.id.close_order){
if(!myImageView.isShown()){
isAllOpen = false;
break;
}
isAllOpen = true;
}
}
}
}
}
if(isAllOpen){
for (int i = 0; i < lv.getChildCount(); i++) {
View v = lv.getChildAt(i);
if ( v instanceof RelativeLayout){
RelativeLayout rlayout = (RelativeLayout)lv.getChildAt(i);
for (int j = 0; j < rlayout.getChildCount(); j++) {
View vv = rlayout.getChildAt(j);
if ( vv instanceof ImageView){
ImageView myImageView = (ImageView)rlayout.getChildAt(j);
if(myImageView.getId() == R.id.close_order){
myImageView.setVisibility(View.GONE);
}
}
}
}
}
isAllOpen = false;
}else{
for (int i = 0; i < lv.getChildCount(); i++) {
View v = lv.getChildAt(i);
if ( v instanceof RelativeLayout){
RelativeLayout rlayout = (RelativeLayout)lv.getChildAt(i);
for (int j = 0; j < rlayout.getChildCount(); j++) {
View vv = rlayout.getChildAt(j);
if ( vv instanceof ImageView){
ImageView myImageView = (ImageView)rlayout.getChildAt(j);
if(myImageView.getId() == R.id.close_order){
myImageView.setVisibility(View.VISIBLE);
}
}
}
}
}
isAllOpen = true;
}
break;