下面列出了android.view.ViewGroup#getTag ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns a Scene described by the resource file associated with the given
* <code>layoutId</code> parameter. If such a Scene has already been created for
* the given <code>sceneRoot</code>, that same Scene will be returned.
* This caching of layoutId-based scenes enables sharing of common scenes
* between those created in code and those referenced by {@link TransitionManager}
* XML resource files.
*
* @param sceneRoot The root of the hierarchy in which scene changes
* and transitions will take place.
* @param layoutId The id of a standard layout resource file.
* @param context The context used in the process of inflating
* the layout resource.
* @return The scene for the given root and layout id
*/
public static Scene getSceneForLayout(ViewGroup sceneRoot, int layoutId, Context context) {
SparseArray<Scene> scenes = (SparseArray<Scene>) sceneRoot.getTag(
com.android.internal.R.id.scene_layoutid_cache);
if (scenes == null) {
scenes = new SparseArray<Scene>();
sceneRoot.setTagInternal(com.android.internal.R.id.scene_layoutid_cache, scenes);
}
Scene scene = scenes.get(layoutId);
if (scene != null) {
return scene;
} else {
scene = new Scene(sceneRoot, layoutId, context);
scenes.put(layoutId, scene);
return scene;
}
}
/**
* (伪)双向 databinding: 同 {@link RecyclerViewAdapter#setDataTwoWay(RecyclerView, ListVM, List)}
*
* @param container
* @param vm
* @param datas
* @param <T>
*/
@BindingAdapter({"vm", "data"})
public static <T> void setDataTwoWay(final ViewGroup container, final ListVM<T> vm, List<T> datas){
if(vm == null){
return ;
}
bind(container, vm, datas);
if(vm instanceof TwoWayListVM){
boolean isInited = container.getTag(R.id.db_inited) != null;
if(!isInited) {
container.setTag(R.id.db_inited, true);
loadData(container, (TwoWayListVM<T>)vm, null, null);
}
}
}
private static ArrayList<View> findViewsWithTag(String TAG, ViewGroup rootView,
ArrayList<View> views) {
Object tag = rootView.getTag();
if (tag != null && tag.equals(TAG)) {
views.add(rootView);
}
for (int i = 0; i < rootView.getChildCount(); i++) {
View v = rootView.getChildAt(i);
tag = v.getTag();
if (tag != null && tag.equals(TAG)) {
views.add(v);
}
if (v instanceof ViewGroup) {
findViewsWithTag(TAG, (ViewGroup) v, views);
}
}
return views;
}
/**
* Returns a Scene described by the resource file associated with the given
* <code>layoutId</code> parameter. If such a Scene has already been created,
* that same Scene will be returned. This caching of layoutId-based scenes enables
* sharing of common scenes between those created in code and those referenced
* by {@link TransitionManager} XML resource files.
*
* @param sceneRoot The root of the hierarchy in which scene changes
* and transitions will take place.
* @param layoutId The id of a standard layout resource file.
* @param context The context used in the process of inflating
* the layout resource.
* @return
*/
@NonNull
public static Scene getSceneForLayout(@NonNull ViewGroup sceneRoot, int layoutId, @NonNull Context context) {
SparseArray<Scene> scenes = (SparseArray<Scene>) sceneRoot.getTag(R.id.scene_layoutid_cache);
if (scenes == null) {
scenes = new SparseArray<Scene>();
sceneRoot.setTag(R.id.scene_layoutid_cache, scenes);
}
Scene scene = scenes.get(layoutId);
if (scene != null) {
return scene;
} else {
scene = new Scene(sceneRoot, layoutId, context);
scenes.put(layoutId, scene);
return scene;
}
}
private void reuseView(ViewGroup view) {
if (view == null) {
return;
}
int tag = (Integer) view.getTag();
view.setVisibility(View.GONE);
if (tag == 1) {
textViews.add(view);
} else if (tag == 2) {
imageViews.add(view);
} else if (tag == 3) {
audioViews.add(view);
}
}
private void reuseView(ViewGroup view) {
if (view == null) {
return;
}
int tag = (Integer) view.getTag();
view.setVisibility(View.GONE);
if (tag == 1) {
textViews.add(view);
} else if (tag == 2) {
imageViews.add(view);
} else if (tag == 3) {
audioViews.add(view);
}
}
private void reuseView(ViewGroup view) {
if (view == null) {
return;
}
int tag = (Integer) view.getTag();
view.setVisibility(View.GONE);
if (tag == 1) {
textViews.add(view);
} else if (tag == 2) {
imageViews.add(view);
} else if (tag == 3) {
audioViews.add(view);
}
}
private void reuseView(ViewGroup view) {
if (view == null) {
return;
}
int tag = (Integer) view.getTag();
view.setVisibility(View.GONE);
if (tag == 1) {
textViews.add(view);
} else if (tag == 2) {
imageViews.add(view);
} else if (tag == 3) {
audioViews.add(view);
}
}
@NonNull
@Override
public Float get(@NonNull ViewGroup object) {
Float alpha = (Float) object.getTag(R.id.mtrl_internal_children_alpha_tag);
if (alpha != null) {
return alpha;
} else {
return 1f;
}
}
private void reuseView(ViewGroup view) {
if (view == null) {
return;
}
int tag = (Integer) view.getTag();
view.setVisibility(View.GONE);
if (tag == 1) {
textViews.add(view);
} else if (tag == 2) {
imageViews.add(view);
} else if (tag == 3) {
audioViews.add(view);
}
}
@NonNull
private static ArrayList<Transition> getRunningTransitions(@NonNull ViewGroup viewGroup) {
ArrayList<Transition> transitions = (ArrayList<Transition>) viewGroup.getTag(R.id.runningTransitions);
if (transitions == null) {
transitions = new ArrayList<Transition>();
viewGroup.setTag(R.id.runningTransitions, transitions);
}
return transitions;
}
private void onImageClick(View image) {
final ViewGroup parent = (ViewGroup) image.getParent();
final int itemPos = (int) parent.getTag(R.id.tag_item);
final int imagePos = (int) image.getTag(R.id.tag_item);
listener.onImageClick(itemPos, imagePos);
}
/**
* change to full screen mode
* @param hideStatusBarBackground hide status bar alpha Background when SDK > 21, true if hide it
*/
public static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
Window window = activity.getWindow();
ViewGroup mContentView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);
//set child View not fill the system window
View mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
ViewCompat.setFitsSystemWindows(mChildView, false);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int statusBarHeight = getStatusBarHeight(activity);
//First translucent status bar.
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//After LOLLIPOP just set LayoutParams.
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
if (hideStatusBarBackground) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(COLOR_TRANSLUCENT);
} else {
window.setStatusBarColor(calculateStatusBarColor(COLOR_TRANSLUCENT, DEFAULT_COLOR_ALPHA));
}
//must call requestApplyInsets, otherwise it will have space in screen bottom
if (mChildView != null) {
ViewCompat.requestApplyInsets(mChildView);
}
} else {
ViewGroup mDecorView = (ViewGroup) window.getDecorView();
if (mDecorView.getTag() != null && mDecorView.getTag() instanceof Boolean && (Boolean)mDecorView.getTag()) {
mChildView = mDecorView.getChildAt(0);
//remove fake status bar view.
mContentView.removeView(mChildView);
mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
//cancel the margin top
if (lp != null && lp.topMargin >= statusBarHeight) {
lp.topMargin -= statusBarHeight;
mChildView.setLayoutParams(lp);
}
}
mDecorView.setTag(false);
}
}
}
}
public void setTranslucentStatusBar() {
Window window = getWindow();
ViewGroup mContentView = findViewById(Window.ID_ANDROID_CONTENT);
// set child View not fill the system window
View mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
mChildView.setFitsSystemWindows(false);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int statusBarHeight = ViewUtils.getStatusBarHeight(this);
// First translucent status bar.
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
// After LOLLIPOP just set LayoutParams.
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(Color.TRANSPARENT);
// must call requestApplyInsets, otherwise it will have space in screen bottom
if (mChildView != null) {
ViewCompat.requestApplyInsets(mChildView);
}
} else {
ViewGroup mDecorView = (ViewGroup) window.getDecorView();
if (mDecorView.getTag() != null && mDecorView.getTag() instanceof Boolean && (Boolean)mDecorView.getTag()) {
mChildView = mDecorView.getChildAt(0);
// remove fake status bar view.
mContentView.removeView(mChildView);
mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
// cancel the margin top
if (lp != null && lp.topMargin >= statusBarHeight) {
lp.topMargin -= statusBarHeight;
mChildView.setLayoutParams(lp);
}
}
mDecorView.setTag(false);
}
}
}
}
@Override
@NonNull
public View getView(final int position, @Nullable final View convertView, @NonNull final ViewGroup parent) {
ViewGroup view = (ViewGroup) convertView;
ViewHolder viewHolder;
if (view == null) {
view = createView(parent);
viewHolder = new ViewHolder();
viewHolder.titleParent = (ViewGroup) view.findViewById(mTitleParentResId);
viewHolder.contentParent = (ViewGroup) view.findViewById(mContentParentResId);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
View titleView = getTitleView(position, viewHolder.titleView, viewHolder.titleParent);
if (!titleView.equals(viewHolder.titleView)) {
viewHolder.titleParent.removeAllViews();
viewHolder.titleParent.addView(titleView);
if (mActionViewResId == 0) {
view.setOnClickListener(new TitleViewOnClickListener(viewHolder.contentParent));
} else {
view.findViewById(mActionViewResId).setOnClickListener(new TitleViewOnClickListener(viewHolder.contentParent));
}
}
viewHolder.titleView = titleView;
View contentView = getContentView(position, viewHolder.contentView, viewHolder.contentParent);
if (!contentView.equals(viewHolder.contentView)) {
viewHolder.contentParent.removeAllViews();
viewHolder.contentParent.addView(contentView);
}
viewHolder.contentView = contentView;
viewHolder.contentParent.setVisibility(mExpandedIds.contains(getItemId(position)) ? View.VISIBLE : View.GONE);
viewHolder.contentParent.setTag(getItemId(position));
LayoutParams layoutParams = viewHolder.contentParent.getLayoutParams();
layoutParams.height = LayoutParams.WRAP_CONTENT;
viewHolder.contentParent.setLayoutParams(layoutParams);
return view;
}
/**
* change to full screen mode
* @param hideStatusBarBackground hide status bar alpha Background when SDK > 21, true if hide it
*/
public static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
Window window = activity.getWindow();
ViewGroup mContentView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int statusBarHeight = getStatusBarHeight(activity);
//First translucent status bar.
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//set child View not fill the system window
View mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
ViewCompat.setFitsSystemWindows(mChildView, false);
//must call requestApplyInsets, otherwise it will have space in screen bottom
ViewCompat.requestApplyInsets(mChildView);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//After LOLLIPOP just set LayoutParams.
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
if (hideStatusBarBackground) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(COLOR_TRANSLUCENT);
} else {
window.setStatusBarColor(calculateStatusBarColor(COLOR_TRANSLUCENT, DEFAULT_COLOR_ALPHA));
}
} else {
ViewGroup mDecorView = (ViewGroup) window.getDecorView();
if (mDecorView.getTag() != null && mDecorView.getTag() instanceof Boolean && (Boolean) mDecorView.getTag()) {
mChildView = mDecorView.getChildAt(0);
//remove fake status bar view.
mDecorView.removeView(mChildView);
mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
//cancel the margin top
if (lp != null && lp.topMargin >= statusBarHeight) {
lp.topMargin -= statusBarHeight;
mChildView.setLayoutParams(lp);
}
}
mDecorView.setTag(false);
}
}
}
}
/**
* change to full screen mode
*
* @param hideStatusBarBackground hide status bar alpha Background when SDK > 21, true if hide it
*/
public static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
Window window = activity.getWindow();
ViewGroup mContentView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);
//set child View not fill the system window
View mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
ViewCompat.setFitsSystemWindows(mChildView, false);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int statusBarHeight = getStatusBarHeight(activity);
//First translucent status bar.
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//After LOLLIPOP just set LayoutParams.
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
if (hideStatusBarBackground) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(COLOR_TRANSLUCENT);
} else {
window.setStatusBarColor(calculateStatusBarColor(COLOR_TRANSLUCENT, DEFAULT_COLOR_ALPHA));
}
//must call requestApplyInsets, otherwise it will have space in screen bottom
if (mChildView != null) {
ViewCompat.requestApplyInsets(mChildView);
}
} else {
ViewGroup mDecorView = (ViewGroup) window.getDecorView();
if (mDecorView.getTag() != null && mDecorView.getTag() instanceof Boolean &&
(Boolean) mDecorView.getTag()) {
mChildView = mDecorView.getChildAt(0);
//remove fake status bar view.
mContentView.removeView(mChildView);
mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
//cancel the margin top
if (lp != null && lp.topMargin >= statusBarHeight) {
lp.topMargin -= statusBarHeight;
mChildView.setLayoutParams(lp);
}
}
mDecorView.setTag(false);
}
}
}
}
/**
* change to full screen mode
*
* @param hideStatusBarBackground hide status bar alpha Background when SDK > 21, true if hide it
*/
public static void translucentStatusBar(Activity activity, boolean hideStatusBarBackground) {
Window window = activity.getWindow();
ViewGroup mContentView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);
//set child View not fill the system window
View mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
ViewCompat.setFitsSystemWindows(mChildView, false);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
int statusBarHeight = getStatusBarHeight(activity);
//First translucent status bar.
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//After LOLLIPOP just set LayoutParams.
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
if (hideStatusBarBackground) {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(COLOR_TRANSLUCENT);
} else {
window.setStatusBarColor(calculateStatusBarColor(COLOR_TRANSLUCENT, DEFAULT_COLOR_ALPHA));
}
//must call requestApplyInsets, otherwise it will have space in screen bottom
if (mChildView != null) {
ViewCompat.requestApplyInsets(mChildView);
}
} else {
ViewGroup mDecorView = (ViewGroup) window.getDecorView();
if (mDecorView.getTag() != null && mDecorView.getTag() instanceof Boolean &&
(Boolean) mDecorView.getTag()) {
mChildView = mDecorView.getChildAt(0);
//remove fake status bar view.
mContentView.removeView(mChildView);
mChildView = mContentView.getChildAt(0);
if (mChildView != null) {
FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) mChildView.getLayoutParams();
//cancel the margin top
if (lp != null && lp.topMargin >= statusBarHeight) {
lp.topMargin -= statusBarHeight;
mChildView.setLayoutParams(lp);
}
}
mDecorView.setTag(false);
}
}
}
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
ViewGroup view = (ViewGroup) convertView;
ViewHolder viewHolder;
if (view == null) {
view = createView(parent);
viewHolder = new ViewHolder();
viewHolder.titleParent = (ViewGroup) view.findViewById(mTitleParentResId);
viewHolder.contentParent = (ViewGroup) view.findViewById(mContentParentResId);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
View titleView = getTitleView(position, viewHolder.titleView, viewHolder.titleParent);
if (titleView != viewHolder.titleView) {
viewHolder.titleParent.removeAllViews();
viewHolder.titleParent.addView(titleView);
if (mActionViewResId == 0) {
view.setOnClickListener(new TitleViewOnClickListener(viewHolder.contentParent));
} else {
view.findViewById(mActionViewResId).setOnClickListener(new TitleViewOnClickListener(viewHolder.contentParent));
}
}
viewHolder.titleView = titleView;
View contentView = getContentView(position, viewHolder.contentView, viewHolder.contentParent);
if (contentView != viewHolder.contentView) {
viewHolder.contentParent.removeAllViews();
viewHolder.contentParent.addView(contentView);
}
viewHolder.contentView = contentView;
viewHolder.contentParent.setVisibility(mExpandedIds.contains(getItemId(position)) ? View.VISIBLE : View.GONE);
viewHolder.contentParent.setTag(getItemId(position));
LayoutParams layoutParams = viewHolder.contentParent.getLayoutParams();
layoutParams.height = LayoutParams.WRAP_CONTENT;
viewHolder.contentParent.setLayoutParams(layoutParams);
return view;
}
@Override
public View getView(final int position, final View convertView, final ViewGroup parent) {
ViewGroup view = (ViewGroup) convertView;
ViewHolder viewHolder;
if (view == null) {
view = createView(parent);
viewHolder = new ViewHolder();
viewHolder.titleParent = (ViewGroup) view.findViewById(mTitleParentResId);
viewHolder.contentParent = (ViewGroup) view.findViewById(mContentParentResId);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
View titleView = getTitleView(position, viewHolder.titleView, viewHolder.titleParent);
if (titleView != viewHolder.titleView) {
viewHolder.titleParent.removeAllViews();
viewHolder.titleParent.addView(titleView);
if (mActionViewResId == 0) {
view.setOnClickListener(new TitleViewOnClickListener(viewHolder.contentParent));
} else {
view.findViewById(mActionViewResId).setOnClickListener(new TitleViewOnClickListener(viewHolder.contentParent));
}
}
viewHolder.titleView = titleView;
View contentView = getContentView(position, viewHolder.contentView, viewHolder.contentParent);
if (contentView != viewHolder.contentView) {
viewHolder.contentParent.removeAllViews();
viewHolder.contentParent.addView(contentView);
}
viewHolder.contentView = contentView;
viewHolder.contentParent.setVisibility(mExpandedIds.contains(getItemId(position)) ? View.VISIBLE : View.GONE);
viewHolder.contentParent.setTag(getItemId(position));
ViewGroup.LayoutParams layoutParams = viewHolder.contentParent.getLayoutParams();
layoutParams.height = LayoutParams.WRAP_CONTENT;
viewHolder.contentParent.setLayoutParams(layoutParams);
return view;
}