下面列出了android.view.ViewGroup#getChildAt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Tests scrollability within child views of v given a delta of dx.
*
* @param v View to test for horizontal scrollability
* @param checkV Whether the view v passed should itself be checked for scrollability (true),
* or just its children (false).
* @param dx Delta scrolled in pixels
* @param x X coordinate of the active touch point
* @param y Y coordinate of the active touch point
* @return true if child views of v can be scrolled by delta of dx.
*/
protected boolean canScrollHorizontal(View v, boolean checkV, int dx, int x, int y) {
if (v instanceof ViewGroup) {
final ViewGroup group = (ViewGroup) v;
final int scrollX = v.getScrollX();
final int scrollY = v.getScrollY();
final int count = group.getChildCount();
for (int i = count - 1; i >= 0; i--) {
// TODO: Add versioned support here for transformed views.
final View child = group.getChildAt(i);
if (x + scrollX >= child.getLeft() && x + scrollX < child.getRight()
&& y + scrollY >= child.getTop() && y + scrollY < child.getBottom()
&& canScrollHorizontal(child, true, dx, x + scrollX - child.getLeft(),
y + scrollY - child.getTop())) {
return true;
}
}
}
return checkV && ViewCompat.canScrollHorizontally(v, -dx);
}
/**
* find the clicked view while loop.
*
* @param view
* @param event
* @return
*/
private View getClickView(View view, MotionEvent event, View tagView) {
View clickView = null;
if (isClickView(view, event) && view.getVisibility() == View.VISIBLE) {
// if the click view is a layout with tag, just return.
if (CommonHelper.isViewHasTag(view)) {
tagView = view;
}
// traverse the layout
if (view instanceof ViewGroup) {
ViewGroup group = (ViewGroup) view;
for (int i = group.getChildCount() - 1; i >= 0; i--) {
View childView = group.getChildAt(i);
clickView = getClickView(childView, event, tagView);
if (clickView != null && CommonHelper.isViewHasTag(clickView)) {
return clickView;
}
}
}
if (tagView != null) {
clickView = tagView;
}
}
return clickView;
}
/**
* 为 DrawerLayout 布局设置状态栏透明
*
* @param activity 需要设置的activity
* @param drawerLayout DrawerLayout
*/
public static void setTransparentForDrawerLayout(Activity activity, DrawerLayout drawerLayout) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
} else {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
// 内容布局不是 LinearLayout 时,设置padding top
if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
contentLayout.getChildAt(1).setPadding(0, obtainHeight(activity), 0, 0);
}
// 设置属性
setDrawerLayoutProperty(drawerLayout, contentLayout);
}
/**
* set SearchView OnClickListener
*
* @param v
* @param listener
*/
public static void setSearchViewOnClickListener(View v, OnClickListener listener) {
if (v instanceof ViewGroup) {
ViewGroup group = (ViewGroup)v;
int count = group.getChildCount();
for (int i = 0; i < count; i++) {
View child = group.getChildAt(i);
if (child instanceof LinearLayout || child instanceof RelativeLayout) {
setSearchViewOnClickListener(child, listener);
}
if (child instanceof TextView) {
TextView text = (TextView)child;
text.setFocusable(false);
}
child.setOnClickListener(listener);
}
}
}
private static String getButtonName(View view ){
String viewName = "";
//获取文本值
if (view instanceof TextView){
viewName = ((TextView) view).getText().toString();
}else if (view instanceof ViewGroup){
ViewGroup viewGroup = (ViewGroup)view;
int count = viewGroup.getChildCount();
for (int i = 0; i < count; i++) {
View itemView = viewGroup.getChildAt(i);
String id = ViewUtils.getSimpleResourceName(itemView.getContext(), itemView.getId());
if (itemView instanceof TextView){
viewName = ((TextView) itemView).getText().toString();
break;
}else if (itemView instanceof ViewGroup){
String name =getButtonName(itemView);
if (null != name){
return name;
}
}
}
}
return viewName;
}
/**
* Adds all children of {@code viewGroup} (recursively) into {@code views}.
*
* @param views an {@code ArrayList} of {@code View}s
* @param viewGroup the {@code ViewGroup} to extract children from
* @param onlySufficientlyVisible if only sufficiently visible views should be returned
*/
private void addChildren(ArrayList<View> views, ViewGroup viewGroup, boolean onlySufficientlyVisible) {
if(viewGroup != null){
for (int i = 0; i < viewGroup.getChildCount(); i++) {
final View child = viewGroup.getChildAt(i);
if(onlySufficientlyVisible && isViewSufficientlyShown(child)) {
views.add(child);
}
else if(!onlySufficientlyVisible && child != null) {
views.add(child);
}
if (child instanceof ViewGroup) {
addChildren(views, (ViewGroup) child, onlySufficientlyVisible);
}
}
}
}
private void updateViewEnable(boolean enable, ViewGroup... parent) {
if (parent != null && parent.length > 0) {
for (ViewGroup group : parent) {
if (!(group instanceof RecyclerView)) {
int len = group.getChildCount();
for (int j = 0; j < len; j++) {
View subView = group.getChildAt(j);
if (subView instanceof X8RulerView) {
this.rulerView.setEnable(enable);
} else if (subView instanceof ViewGroup) {
updateViewEnable(enable, (ViewGroup) subView);
} else {
subView.setEnabled(enable);
}
}
} else if (group == this.isoRecycler || group == this.shutterRecycler) {
this.isoAdatper.setEnable(enable);
this.shutterAdapter.setEnable(enable);
}
}
}
}
public static void checkLayout(ViewGroup root, String prefix){
if(root == null) return;
for(int i = 0; i < root.getChildCount(); i++){
View v = root.getChildAt(i);
Log.i("111", prefix + ": " + v.getClass().getSimpleName());
if(v instanceof ViewGroup){
checkLayout(((ViewGroup) v), " " + prefix);
}
}
}
private boolean checkScrollableRecursively(ViewGroup viewGroup) {
if (viewGroup instanceof AdapterView) {
return true;
} else {
for (int i=0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
if (child instanceof ViewGroup) {
if (checkScrollableRecursively((ViewGroup) child)) {
return true;
}
}
}
}
return false;
}
private void setTouchListener(View view, View.OnTouchListener tl) {
if (view==null) return;
view.setOnTouchListener(tl);
if (view instanceof ViewGroup) {
ViewGroup vg = (ViewGroup)view;
for (int i = 0; i < vg.getChildCount(); i++) {
View vc = vg.getChildAt(i);
setTouchListener(vc, tl);
}
}
}
public static void loadSnapshotsIntoList(
ViewGroup valuesList, Label label, AppAccount appAccount) {
Context context = valuesList.getContext();
SnapshotLabelValue snapshotLabelValue = label.getSnapshotLabelValue();
valuesList.setVisibility(View.VISIBLE);
// Make sure it has the correct number of views, re-using as many as possible.
int childCount = valuesList.getChildCount();
int snapshotsCount = snapshotLabelValue.getSnapshotsCount();
if (childCount < snapshotsCount) {
for (int i = 0; i < snapshotsCount - childCount; i++) {
LayoutInflater.from(context).inflate(R.layout.snapshot_value_details, valuesList);
}
} else if (childCount > snapshotsCount) {
valuesList.removeViews(0, childCount - snapshotsCount);
}
SensorAppearanceProvider sensorAppearanceProvider =
AppSingleton.getInstance(context).getSensorAppearanceProvider(appAccount);
String valueFormat = context.getResources().getString(R.string.data_with_units);
for (int i = 0; i < snapshotsCount; i++) {
GoosciSnapshotValue.SnapshotLabelValue.SensorSnapshot snapshot =
snapshotLabelValue.getSnapshots(i);
ViewGroup snapshotLayout = (ViewGroup) valuesList.getChildAt(i);
GoosciSensorAppearance.BasicSensorAppearance appearance =
snapshot.getSensor().getRememberedAppearance();
TextView sensorName = (TextView) snapshotLayout.findViewById(R.id.sensor_name);
sensorName.setCompoundDrawablesRelative(null, null, null, null);
sensorName.setText(appearance.getName());
String value =
BuiltInSensorAppearance.formatValue(
snapshot.getValue(), appearance.getPointsAfterDecimal());
((TextView) snapshotLayout.findViewById(R.id.sensor_value))
.setText(String.format(valueFormat, value, appearance.getUnits()));
loadLargeDrawable(appearance, sensorAppearanceProvider, snapshotLayout, snapshot.getValue());
}
}
/**
* 为DrawerLayout 布局设置状态栏变色
*
* @param activity 需要设置的activity
* @param drawerLayout DrawerLayout
* @param color 状态栏颜色值
* @param statusBarAlpha 状态栏透明度
*/
public static void setColorForDrawerLayout(Activity activity, DrawerLayout drawerLayout, @ColorInt int color,
int statusBarAlpha) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
activity.getWindow().setStatusBarColor(Color.TRANSPARENT);
} else {
activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
}
// 生成一个状态栏大小的矩形
// 添加 statusBarView 到布局中
ViewGroup contentLayout = (ViewGroup) drawerLayout.getChildAt(0);
if (contentLayout.getChildCount() > 0 && contentLayout.getChildAt(0) instanceof StatusBarView) {
contentLayout.getChildAt(0).setBackgroundColor(calculateStatusColor(color, statusBarAlpha));
} else {
StatusBarView statusBarView = createStatusBarView(activity, color);
contentLayout.addView(statusBarView, 0);
}
// 内容布局不是 LinearLayout 时,设置padding top
if (!(contentLayout instanceof LinearLayout) && contentLayout.getChildAt(1) != null) {
contentLayout.getChildAt(1)
.setPadding(contentLayout.getPaddingLeft(), getStatusBarHeight(activity) + contentLayout.getPaddingTop(),
contentLayout.getPaddingRight(), contentLayout.getPaddingBottom());
}
// 设置属性
ViewGroup drawer = (ViewGroup) drawerLayout.getChildAt(1);
drawerLayout.setFitsSystemWindows(false);
contentLayout.setFitsSystemWindows(false);
contentLayout.setClipToPadding(true);
drawer.setFitsSystemWindows(false);
addTranslucentView(activity, statusBarAlpha);
}
private void setStorageModuleRecursively(ViewGroup container, StorageModule module) {
for (int i = 0; i < container.getChildCount(); i++) {
View child = container.getChildAt(i);
if (child instanceof AbsMaterialPreference) {
((AbsMaterialPreference) child).setStorageModule(module);
} else if (child instanceof ViewGroup) {
setStorageModuleRecursively((ViewGroup) child, module);
}
}
}
/**
* Reuses item views when it can
*/
public void updateMenuView(boolean cleared) {
final ViewGroup parent = (ViewGroup) mMenuView;
if (parent == null) return;
int childIndex = 0;
if (mMenu != null) {
mMenu.flagActionItems();
ArrayList<MenuItemImpl> visibleItems = mMenu.getVisibleItems();
final int itemCount = visibleItems.size();
for (int i = 0; i < itemCount; i++) {
MenuItemImpl item = visibleItems.get(i);
if (shouldIncludeItem(childIndex, item)) {
final View convertView = parent.getChildAt(childIndex);
final MenuItemImpl oldItem = convertView instanceof MenuView.ItemView ?
((MenuView.ItemView) convertView).getItemData() : null;
final View itemView = getItemView(item, convertView, parent);
if (item != oldItem) {
// Don't let old states linger with new data.
itemView.setPressed(false);
if (IS_HONEYCOMB) itemView.jumpDrawablesToCurrentState();
}
if (itemView != convertView) {
addItemView(itemView, childIndex);
}
childIndex++;
}
}
}
// Remove leftover views.
while (childIndex < parent.getChildCount()) {
if (!filterLeftoverView(parent, childIndex)) {
childIndex++;
}
}
}
/**
* Adapts the enable state of all children of a specific view group.
*
* @param viewGroup
* The view group, whose children's enabled states should be adapted, as an instance of
* the class {@link ViewGroup}. The view group may not be null
* @param enabled
* True, if the children should be enabled, false otherwise
*/
private void setEnabledOnViewGroup(@NonNull final ViewGroup viewGroup, final boolean enabled) {
viewGroup.setEnabled(enabled);
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
if (child instanceof ViewGroup) {
setEnabledOnViewGroup((ViewGroup) child, enabled);
} else {
child.setEnabled(enabled);
}
}
}
@SuppressWarnings("unchecked")
public static <T extends View> T findViewWithType(ViewGroup viewGroup, Class<T> clazz) {
for (int i = 0; i < viewGroup.getChildCount() - 1; i++) {
View view = viewGroup.getChildAt(i);
if (view.getClass() == clazz) {
return (T) view;
}
}
return null;
}
/**
* hide sub menus with animates
*
* @param viewGroup
* @param listener
*/
private void hideSubMenus(ViewGroup viewGroup, final AnimatorListenerAdapter listener) {
if (viewGroup == null) return;
int childCount = viewGroup.getChildCount();
for (int i = 0; i < childCount; i++) {
View view = viewGroup.getChildAt(i);
view.animate().translationY(mScreenHeight).setDuration(mDuration).setListener(listener).start();
}
}
public <Type extends View> Type findViewOfType(Class<Type> type) {
List<ViewGroup> groups = new ArrayList<>();
groups.add(this);
while (!groups.isEmpty()) {
ViewGroup group = groups.remove(0);
for (int i = 0; i < group.getChildCount(); i++) {
View child = group.getChildAt(i);
if (child.getClass().equals(type))
return (Type) child;
if (child instanceof ViewGroup)
groups.add((ViewGroup) child);
}
}
return null;
}
private void findAllFields(ViewGroup view) {
int child = view.getChildCount();
for (int i = 0; i < child; i++) {
View v = view.getChildAt(i);
if (v instanceof LinearLayout || v instanceof RelativeLayout) {
if (v.getVisibility() == View.VISIBLE)
findAllFields((ViewGroup) v);
}
if (v instanceof OField) {
OField field = (OField) v;
if (field.getVisibility() == View.VISIBLE)
mFormFieldControls.put(field.getFieldName(), field);
}
}
}
/**
* Private helper method to get the CellLayoutChildren given a CellLayout index.
*/
private static ShortcutAndWidgetContainer getCellLayoutChildrenForIndex(
ViewGroup container, int i) {
CellLayout parent = (CellLayout) container.getChildAt(i);
return parent.getShortcutsAndWidgets();
}