下面列出了android.view.ViewGroup#removeViewAt ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 调整 parent 的 child views 到指定的数量,并存入 target
*/
@SuppressWarnings("unchecked")
public static <V extends View> void fitChildViews(ViewGroup parent, V[] target, ViewPool<V> pool) {
int childCount = parent.getChildCount();
// 移除多出的项
while (target.length < childCount) {
V view = (V) parent.getChildAt(0);
parent.removeViewAt(0);
pool.recycle(view);
childCount--;
}
// 收集已有的项
for (int i = 0; i < childCount; i++) {
target[i] = (V) parent.getChildAt(i);
}
// 添加并收集新增的项
while (target.length > childCount) {
target[childCount] = pool.obtain(parent);
parent.addView(target[childCount]);
childCount++;
}
}
public View applyTo(View target, int tipsId) {
mTargetView = target;
ViewGroup parent = (ViewGroup) target.getParent();
if (parent == null) {
return null;
}
View tipsView;
if (parent instanceof TipsContainer) {
tipsView = addTipsViewToContainer(target, parent, tipsId);
} else {
TipsContainer tipsContainerView = new TipsContainer(target.getContext());
ViewGroup.LayoutParams targetParams = target.getLayoutParams();
int index = parent.indexOfChild(target);
parent.removeViewAt(index);
parent.addView(tipsContainerView, index, targetParams);
Drawable background = target.getBackground();
if (background != null) {
tipsContainerView.setBackground(background);
}
tipsView = addTipsViewToContainer(target, tipsContainerView, tipsId);
}
return tipsView;
}
/**
* 调整 parent 的 child views 到指定的数量,并存入 target
*/
@SuppressWarnings("unchecked")
public static <V extends View> void fitChildViews(ViewGroup parent, V[] target, ViewPool<V> pool) {
int childCount = parent.getChildCount();
// 移除多出的项
while (target.length < childCount) {
V view = (V) parent.getChildAt(0);
parent.removeViewAt(0);
pool.recycle(view);
childCount--;
}
// 收集已有的项
for (int i = 0; i < childCount; i++) {
target[i] = (V) parent.getChildAt(i);
}
// 添加并收集新增的项
while (target.length > childCount) {
target[childCount] = pool.obtain(parent);
parent.addView(target[childCount]);
childCount++;
}
}
private void detachTrackerFrameLayout(Activity activity) {
if (activity == null || activity instanceof TabActivity) {
return;
}
try {
ViewGroup container = (ViewGroup) activity.findViewById(android.R.id.content);
if (container == null) {
return;
}
if (container.getChildAt(0) instanceof TrackerFrameLayout) {
container.removeViewAt(0);
}
} catch (Exception e) {
TrackerLog.e(e.toString());
}
}
@TargetApi(Build.VERSION_CODES.KITKAT)
private static void clearPreviousSetting(Activity activity) {
ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
int count = decorView.getChildCount();
if (count > 0 && decorView.getChildAt(count - 1) instanceof StatusBarView) {
decorView.removeViewAt(count - 1);
ViewGroup rootView = (ViewGroup) ((ViewGroup) activity.findViewById(android.R.id.content)).getChildAt(0);
rootView.setPadding(0, 0, 0, 0);
}
}
private static void insertLayoutIntoViewGroup(ViewGroup viewGroup,
PullToRefreshLayout pullToRefreshLayout) {
// Move all children to PullToRefreshLayout. This code looks a bit silly but the child
// indices change every time we remove a View (so we can't just iterate through)
View child = viewGroup.getChildAt(0);
while (child != null) {
viewGroup.removeViewAt(0);
pullToRefreshLayout.addView(child);
child = viewGroup.getChildAt(0);
}
viewGroup.addView(pullToRefreshLayout, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
}
/**
* 添加半透明矩形条
*
* @param activity 需要设置的 activity
* @param statusBarAlpha 透明值
*/
private static void addTranslucentView(Activity activity, int statusBarAlpha) {
ViewGroup contentView = (ViewGroup) activity.findViewById(android.R.id.content);
// 移除半透明矩形,以免叠加
if (contentView.getChildCount() > 1) {
contentView.removeViewAt(1);
}
contentView.addView(createTranslucentStatusBarView(activity, statusBarAlpha));
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
//屏幕旋转时刷新一下title
super.onConfigurationChanged(newConfig);
ViewGroup root = (ViewGroup) getRootView();
if (root.getChildAt(0) instanceof TitleBar) {
root.removeViewAt(0);
initTitle();
}
}
@Override
public final View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = super.onCreateView(inflater, container, savedInstanceState);
ListView lv = (ListView) layout.findViewById(android.R.id.list);
ViewGroup parent = (ViewGroup) lv.getParent();
// Remove ListView and add PullToRefreshListView in its place
int lvIndex = parent.indexOfChild(lv);
parent.removeViewAt(lvIndex);
mPullToRefreshListView = onCreatePullToRefreshListView(inflater, savedInstanceState);
parent.addView(mPullToRefreshListView, lvIndex, lv.getLayoutParams());
return layout;
}
private static void insertLayoutIntoViewGroup(ViewGroup viewGroup,
PullToRefreshLayout pullToRefreshLayout) {
// Move all children to PullToRefreshLayout. This code looks a bit silly but the child
// indices change every time we remove a View (so we can't just iterate through)
View child = viewGroup.getChildAt(0);
while (child != null) {
viewGroup.removeViewAt(0);
pullToRefreshLayout.addView(child);
child = viewGroup.getChildAt(0);
}
viewGroup.addView(pullToRefreshLayout, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
}
private static void insertLayoutIntoViewGroup(ViewGroup viewGroup,
PullToRefreshLayout pullToRefreshLayout) {
// Move all children to PullToRefreshLayout. This code looks a bit silly but the child
// indices change every time we remove a View (so we can't just iterate through)
View child = viewGroup.getChildAt(0);
while (child != null) {
viewGroup.removeViewAt(0);
pullToRefreshLayout.addView(child);
child = viewGroup.getChildAt(0);
}
viewGroup.addView(pullToRefreshLayout, ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
}
public AlertPage setContentView(View view) {
ViewGroup parent = (ViewGroup)mTvContent.getParent();
int index = parent.indexOfChild(mTvContent);
parent.removeViewAt(index);
parent.addView(view, index);
return this;
}
/**
* This method can be overriden to recycle (remove) {@param ad} from {@param wrapper} view before adding to wrap.
* By default it will look for {@param ad} in the direct children of {@param wrapper} and remove the first occurence.
* See the super's implementation for instance.
* The NativeExpressHolder recycled by the RecyclerView may be a different
* instance than the one used previously for this position. Clear the
* wrapper of any subviews in case it has a different
* AdView associated with it
*/
@Override
protected void recycleAdViewWrapper(@NonNull ViewGroup wrapper, @NonNull NativeExpressAdView ad) {
for (int i = 0; i < wrapper.getChildCount(); i++) {
View v = wrapper.getChildAt(i);
if (v instanceof NativeExpressAdView) {
wrapper.removeViewAt(i);
break;
}
}
}
public void remove() {
if (getParent() == null || _labelViewContainerID == -1) {
return;
}
ViewGroup frameContainer = (ViewGroup) getParent();
assert (frameContainer.getChildCount() == 2);
View target = frameContainer.getChildAt(0);
ViewGroup parentContainer = (ViewGroup) frameContainer.getParent();
int groupIndex = parentContainer.indexOfChild(frameContainer);
if (frameContainer.getParent() instanceof RelativeLayout) {
for (int i = 0; i < parentContainer.getChildCount(); i++) {
if (i == groupIndex) {
continue;
}
View view = parentContainer.getChildAt(i);
RelativeLayout.LayoutParams para = (RelativeLayout.LayoutParams) view.getLayoutParams();
for (int j = 0; j < para.getRules().length; j++) {
if (para.getRules()[j] == _labelViewContainerID) {
para.getRules()[j] = target.getId();
}
}
view.setLayoutParams(para);
}
}
ViewGroup.LayoutParams frameLayoutParam = frameContainer.getLayoutParams();
target.setLayoutParams(frameLayoutParam);
parentContainer.removeViewAt(groupIndex);
frameContainer.removeView(target);
frameContainer.removeView(this);
parentContainer.addView(target,groupIndex);
_labelViewContainerID = -1;
}
private static void removeContainerView(ViewGroup tipsContainerView, View targetView) {
ViewGroup parent = (ViewGroup) tipsContainerView.getParent();
ViewGroup.LayoutParams targetParams = tipsContainerView.getLayoutParams();
int index = parent.indexOfChild(tipsContainerView);
parent.removeViewAt(index);
if (targetView.getParent() != null) {
((ViewGroup) targetView.getParent()).removeView(targetView);
}
parent.addView(targetView, index, targetParams);
}
/**
* Remove all action buttons from the layout, in case the layout needs to be re-used.
*/
public void removeActionButtons (ViewGroup actionButtonViewGroup) {
while (actionButtonViewGroup.getChildCount() > 0) {
actionButtonViewGroup.removeViewAt(0);
}
}
/**
* Moves a fragment to its expected final state or the fragment manager's state, depending
* on whether the fragment manager's state is raised properly.
*
* @param f The fragment to change.
*/
void moveFragmentToExpectedState(final Fragment f) {
if (f == null) {
return;
}
int nextState = mCurState;
if (f.mRemoving) {
if (f.isInBackStack()) {
nextState = Math.min(nextState, Fragment.CREATED);
} else {
nextState = Math.min(nextState, Fragment.INITIALIZING);
}
}
moveToState(f, nextState, f.getNextTransition(), f.getNextTransitionStyle(), false);
if (f.mView != null) {
// Move the view if it is out of order
Fragment underFragment = findFragmentUnder(f);
if (underFragment != null) {
final View underView = underFragment.mView;
// make sure this fragment is in the right order.
final ViewGroup container = f.mContainer;
int underIndex = container.indexOfChild(underView);
int viewIndex = container.indexOfChild(f.mView);
if (viewIndex < underIndex) {
container.removeViewAt(viewIndex);
container.addView(f.mView, underIndex);
}
}
if (f.mIsNewlyAdded && f.mContainer != null) {
// Make it visible and run the animations
f.mView.setTransitionAlpha(1f);
f.mIsNewlyAdded = false;
// run animations:
Animator anim = loadAnimator(f, f.getNextTransition(), true, f.getNextTransitionStyle());
if (anim != null) {
anim.setTarget(f.mView);
setHWLayerAnimListenerIfAlpha(f.mView, anim);
anim.start();
}
}
}
if (f.mHiddenChanged) {
completeShowHideFragment(f);
}
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
position = adjustIndexForDirectionality(position, getCount());
container.removeViewAt(position);
}
/**
* Filter the child view at index and remove it if appropriate.
* @param parent Parent to filter from
* @param childIndex Index to filter
* @return true if the child view at index was removed
*/
protected boolean filterLeftoverView(ViewGroup parent, int childIndex) {
parent.removeViewAt(childIndex);
return true;
}
/**
* Filter the child view at index and remove it if appropriate.
* @param parent Parent to filter from
* @param childIndex Index to filter
* @return true if the child view at index was removed
*/
protected boolean filterLeftoverView(ViewGroup parent, int childIndex) {
parent.removeViewAt(childIndex);
return true;
}