import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = this.getWindow().getDecorView();
// 遍历你想要遍历的的父视图,并返回视图集合
List<View> viewList = getAllChildViews(view);
for (int i = 0; i < viewList.size(); i++) {
View v = viewList.get(i);
if (v != null) {
// 拦截监听触摸事件
v.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
}
return false;
}
});
}
}
}
/**
* 获取传入指定视图下的所有子视图
*
* @param view
* @return
*/
private List<View> getAllChildViews(View view) {
List<View> allChildViews = new ArrayList<>();
if (view != null && view instanceof ViewGroup) {
ViewGroup vp = (ViewGroup) view;
for (int i = 0; i < vp.getChildCount(); i++) {
View viewChild = vp.getChildAt(i);
allChildViews.add(viewChild);
// 递归
allChildViews.addAll(getAllChildViews(viewChild));
}
}
return allChildViews;
}
}
android遍历所有子视图
猜你喜欢
转载自blog.csdn.net/u011038298/article/details/84819498
今日推荐
周排行