Skip to content

Commit 18e90af

Browse files
committed
Add a method to get descended views
1 parent a0dfb10 commit 18e90af

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

src/cn/trinea/android/common/util/ViewUtils.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package cn.trinea.android.common.util;
22

33
import java.lang.reflect.Field;
4+
import java.util.ArrayList;
5+
import java.util.List;
46

57
import android.view.View;
68
import android.view.View.OnClickListener;
@@ -228,4 +230,29 @@ public static void setSearchViewOnClickListener(View v, OnClickListener listener
228230
}
229231
}
230232
}
233+
234+
/**
235+
* get descended views from parent.
236+
*
237+
* @param parent
238+
* @param filter Type of views which will be returned.
239+
* @param includeSubClass Whether returned list will include views which are subclass of filter or not.
240+
* @return
241+
*/
242+
public static <T extends View> List<T> getDescendants(ViewGroup parent, Class<T> filter, boolean includeSubClass) {
243+
List<T> allDescendedView = new ArrayList<T>();
244+
245+
int childCount = parent.getChildCount();
246+
for (int i = 0; i < childCount; i++) {
247+
View child = parent.getChildAt(i);
248+
Class<? extends View> childsClass = child.getClass();
249+
if (includeSubClass && filter.isAssignableFrom(childsClass) || !includeSubClass && childsClass == filter) {
250+
allDescendedView.add(filter.cast(child));
251+
}
252+
if (child instanceof ViewGroup) {
253+
allDescendedView.addAll(getDescendants((ViewGroup) child, filter, includeSubClass));
254+
}
255+
}
256+
return allDescendedView;
257+
}
231258
}

0 commit comments

Comments
 (0)