Android – 如何find具有共同属性的多个视图

我有一个与多个ImageView的布局,其中一些图像需要有相同的onClickListener。 我希望我的代码是灵活的,并能够获得这些View的集合,在运行时迭代和添加侦听器。

有没有像findViewById这样的方法,将返回View的集合,而不是一个单一的?

Solutions Collecting From Web of "Android – 如何find具有共同属性的多个视图"

我终于写了这个方法( 更新感谢@SuitUp(更正的用户名)):

  private static ArrayList<View> getViewsByTag(ViewGroup root, String tag){ ArrayList<View> views = new ArrayList<View>(); final int childCount = root.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = root.getChildAt(i); if (child instanceof ViewGroup) { views.addAll(getViewsByTag((ViewGroup) child, tag)); } final Object tagObj = child.getTag(); if (tagObj != null && tagObj.equals(tag)) { views.add(child); } } return views; } 

它将返回所有具有android:tag="TAG_NAME"属性的视图。 请享用 ;)

Shlomi Schwartz的方法有一个缺陷,它不收集ViewGroups Views 。 这是我的修复:

 private static ArrayList<View> getViewsByTag(ViewGroup root, String tag){ ArrayList<View> views = new ArrayList<View>(); final int childCount = root.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = root.getChildAt(i); if (child instanceof ViewGroup) { views.addAll(getViewsByTag((ViewGroup) child, tag)); } final Object tagObj = child.getTag(); if (tagObj != null && tagObj.equals(tag)) { views.add(child); } } return views; } 

findViewById没有返回一组视图的方法,但是你可以迭代你的视图并设置一个onClickListener像这样:

 for (int i = 0;i < layout.getChildCount();i++) { View child = layout.getChildAt(i); //you can check for view tag or something to see if it satisfies your criteria //child.setOnClickListener... } 

UPD:对于recursion视图层次结构(这是一个真实项目中的示例,我们在这里以recursion方式查看刷新,但是您可以对嵌套视图执行任何操作):

 private void recursiveViewRefresh(ViewGroup view) { for (int i = 0;i < view.getChildCount();i++) { View child = view.getChildAt(i); try { ViewGroup viewGroup = (ViewGroup) child; recursiveViewRefresh(viewGroup); } catch (ClassCastException e) { //just ignore the exception - it is used as a check } singleViewRefresh(child); } } 

这种方法提供了获得符合给定条件的视图的一般方法。 使用简单的实现一个ViewMatcher接口

 private static List<View> getMatchingViews(ViewGroup root, ViewMatcher viewMatcher){ List<View> views = new ArrayList<View>(); final int childCount = root.getChildCount(); for (int i = 0; i < childCount; i++) { final View child = root.getChildAt(i); if (child instanceof ViewGroup) { views.addAll(getMatchingViews((ViewGroup) child, viewMatcher)); } if(viewMatcher.matches(child)){ views.add(child); } } return views; } public interface ViewMatcher{ public boolean matches(View v); } // Example1: matches views with the given tag public class TagMatcher implements ViewMatcher { private String tag; public TagMatcher(String tag){ this.tag = tag; } public boolean matches(View v){ String tag = v.getTag(); return this.tag.equals(tag); } } // Example2: matches views that have visibility GONE public class GoneMatcher implements ViewMatcher { public boolean matches(View v){ v.getVisibility() == View.GONE } } 
 @Override protected void onClick(View view){ switch(view.getId()){ case R.id.whatEverImageViewId : //.... break ; case R.id.whatEverImageViewId 2 : //.... break ; .... 

你可以使用for循环来添加监听器

你可以使用switch()来实现多个小部件。

 switch(viewobject.getId()) { case R.id.imageview1: /* ... */ break; case R.id.imageview2: /* ... */ break; }