下面列出了android.view.View#addOnLayoutChangeListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override public void onViewCreated(@NonNull View view, @Nullable Bundle inState) {
super.onViewCreated(view, inState);
content = Views.findRequired(view, R.id.contentContainer);
progress = Views.findRequired(view, R.id.progressContainer);
wait = true;
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft,
int oldTop, int oldRight, int oldBottom) {
v.removeOnLayoutChangeListener(this);
wait = false;
if (currentState == STATE_CONTENT_VISIBLE) {
content.setAlpha(1.0f);
content.setVisibility(View.VISIBLE);
progress.setVisibility(View.GONE);
} else {
content.setVisibility(View.GONE);
progress.setAlpha(1.0f);
progress.setVisibility(View.VISIBLE);
}
}
});
}
void addViewWithSubviewClippingEnabled(
View child, int index, ViewGroup.LayoutParams params) {
Assertions.assertCondition(mRemoveClippedSubviews);
Assertions.assertNotNull(mClippingRect);
Assertions.assertNotNull(mAllChildren);
addInArray(child, index);
// we add view as "clipped" and then run {@link #updateSubviewClipStatus} to conditionally
// attach it
int childIndexOffset = 0;
for (int i = 0; i < index; i++) {
if (!isChildInViewGroup(mAllChildren[i])) {
childIndexOffset++;
}
}
updateSubviewClipStatus(mClippingRect, index, childIndexOffset);
child.addOnLayoutChangeListener(mChildrenLayoutChangeListener);
}
@Override
public void onCreate(ItemBridgeAdapter.ViewHolder viewHolder) {
View headerView = viewHolder.getViewHolder().view;
headerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnHeaderClickedListener != null) {
mOnHeaderClickedListener.onHeaderClicked();
}
}
});
headerView.setFocusable(true);
headerView.setFocusableInTouchMode(true);
if (mWrapper != null) {
viewHolder.itemView.addOnLayoutChangeListener(sLayoutChangeListener);
} else {
headerView.addOnLayoutChangeListener(sLayoutChangeListener);
}
}
private void addOnLayoutChangeListener(@Nullable final View view) {
if (view == null) {
return;
}
view.addOnLayoutChangeListener(
new OnLayoutChangeListener() {
@Override
public void onLayoutChange(
View v,
int left,
int top,
int right,
int bottom,
int oldLeft,
int oldTop,
int oldRight,
int oldBottom) {
if (view.getVisibility() == VISIBLE) {
tryUpdateBadgeDrawableBounds(view);
}
}
});
}
@Override
public void onCreate(ItemBridgeAdapter.ViewHolder viewHolder) {
View headerView = viewHolder.getViewHolder().view;
headerView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mOnHeaderClickedListener != null) {
mOnHeaderClickedListener.onHeaderClicked();
}
}
});
headerView.setFocusable(true);
headerView.setFocusableInTouchMode(true);
if (mWrapper != null) {
viewHolder.itemView.addOnLayoutChangeListener(sLayoutChangeListener);
} else {
headerView.addOnLayoutChangeListener(sLayoutChangeListener);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final View view = getLayoutInflater().inflate(R.layout.activity_viewnatruetransitionanim, null);
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
v.removeOnLayoutChangeListener(this);
mHalfHeight = view.getHeight() / 2;
mEditModeContainerFL.setTranslationY(mHalfHeight);
mEditModeContainerFL.setAlpha(0f);
mCustomAnimator.setEditModeHalfHeight(mHalfHeight);
}
});
setContentView(view);
initView();
initListener();
}
private static PivotSetter getSetter(View view) {
PivotSetter setter = null;
if (layoutListeners == null) {
layoutListeners = new WeakHashMap<>();
} else {
setter = layoutListeners.get(view);
}
if (setter == null) {
setter = new PivotSetter();
view.addOnLayoutChangeListener(setter);
layoutListeners.put(view, setter);
}
return setter;
}
private void showRemoteViews(RemoteViews remoteViews) {
final View inflatedView = remoteViews.apply(mActivity, getBottomBarView());
if (mClickableIDs != null && mClickPendingIntent != null) {
for (int id: mClickableIDs) {
if (id < 0) return;
View view = inflatedView.findViewById(id);
if (view != null) view.setOnClickListener(mBottomBarClickListener);
}
}
getBottomBarView().addView(inflatedView, 1);
inflatedView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
inflatedView.removeOnLayoutChangeListener(this);
mFullscreenManager.setBottomControlsHeight(v.getHeight());
}
});
}
public FloatingActionModeHelper(FloatingActionMode mode, View target,
FloatingActionMode.Callback callback, int themeResId) {
mMode = mode;
mTarget = target;
mCallback = callback;
final Context context = target.getContext();
mMenu = new FloatingMenuImpl(target.getContext());
mView = new ViewManager(context, themeResId, mode, mMenu, callback);
final View root = mTarget.getRootView();
root.addOnLayoutChangeListener(this);
root.addOnAttachStateChangeListener(this);
mTarget.addOnAttachStateChangeListener(this);
}
public static void registerCircularRevealAnimation(final Context context, final View view, final RevealAnimationSetting revealSettings) {
final int startColor = revealSettings.getColorAccent();
final int endColor = getBackgroundColor(view);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
v.removeOnLayoutChangeListener(this);
view.setVisibility(View.VISIBLE);
int cx = revealSettings.getCenterX();
int cy = revealSettings.getCenterY();
int width = revealSettings.getWidth();
int height = revealSettings.getHeight();
int duration = context.getResources().getInteger(android.R.integer.config_longAnimTime);
//Simply use the diagonal of the view
float finalRadius = (float) Math.sqrt(width * width + height * height);
Animator anim = ViewAnimationUtils.createCircularReveal(v, cx, cy, 0, finalRadius).setDuration(duration);
anim.setInterpolator(new FastOutSlowInInterpolator());
anim.start();
startColorAnimation(view, startColor, endColor, duration);
}
});
}
}
@Override
public void setRemoveClippedSubviews(boolean removeClippedSubviews) {
if (removeClippedSubviews == mRemoveClippedSubviews) {
return;
}
mRemoveClippedSubviews = removeClippedSubviews;
if (removeClippedSubviews) {
mClippingRect = new Rect();
ReactClippingViewGroupHelper.calculateClippingRect(this, mClippingRect);
mAllChildrenCount = getChildCount();
int initialSize = Math.max(12, mAllChildrenCount);
mAllChildren = new View[initialSize];
mChildrenLayoutChangeListener = new ChildrenLayoutChangeListener(this);
for (int i = 0; i < mAllChildrenCount; i++) {
View child = getChildAt(i);
mAllChildren[i] = child;
child.addOnLayoutChangeListener(mChildrenLayoutChangeListener);
}
updateClippingRect();
} else {
// Add all clipped views back, deallocate additional arrays, remove layoutChangeListener
Assertions.assertNotNull(mClippingRect);
Assertions.assertNotNull(mAllChildren);
Assertions.assertNotNull(mChildrenLayoutChangeListener);
for (int i = 0; i < mAllChildrenCount; i++) {
mAllChildren[i].removeOnLayoutChangeListener(mChildrenLayoutChangeListener);
}
getDrawingRect(mClippingRect);
updateClippingToRect(mClippingRect);
mAllChildren = null;
mClippingRect = null;
mAllChildrenCount = 0;
mChildrenLayoutChangeListener = null;
}
}
/**
* shows circular revealed animation to a view.
*
* @param targetView view for animation target.
*/
private void circularRevealed(View targetView) {
targetView.addOnLayoutChangeListener(
new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(
View view,
int left,
int top,
int right,
int bottom,
int oldLeft,
int oldTop,
int oldRight,
int oldBottom) {
view.removeOnLayoutChangeListener(this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Animator animator =
ViewAnimationUtils.createCircularReveal(
view,
(view.getLeft() + view.getRight()) / 2,
(view.getTop() + view.getBottom()) / 2,
0f,
Math.max(view.getWidth(), view.getHeight()));
animator.setDuration(900);
animator.start();
}
}
});
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
final View contentView = inflater.inflate(R.layout.fragment_sign_in, container, false);
contentView.addOnLayoutChangeListener(new View.
OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
v.removeOnLayoutChangeListener(this);
setUpGridView(getView());
}
});
return contentView;
}
public static void runWhenViewIsFullyMeasured(@NonNull final View view, @NonNull final Runnable runnable) {
if (ViewCompat.isLaidOut(view)) {
runnable.run();
} else {
view.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(final View v, final int left, final int top, final int right,
final int bottom, final int oldLeft, final int oldTop, final int oldRight, final int oldBottom) {
view.removeOnLayoutChangeListener(this);
runnable.run();
}
});
}
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
View view = getView();
view.addOnLayoutChangeListener(this);
}
/**
* Should be called to allow this drawable to calculate its position within the current display
* frame. This allows it to apply to specified window padding.
*
* @see #detachView(View)
*/
public void setRelativeToView(@Nullable View view) {
if (view == null) {
return;
}
updateLocationOnScreen(view);
// Listen for changes that indicate the view has moved so the location can be updated
view.addOnLayoutChangeListener(attachedViewLayoutChangeListener);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
View view = getView();
view.addOnLayoutChangeListener(this);
}
public static void intPreScroll(final SmoothAppBarLayout smoothAppBarLayout, final View target, final int offset) {
target.addOnLayoutChangeListener(new OnPreScrollListener(smoothAppBarLayout, target, offset));
}
private void attemptToCorrectMediaPlayerStatus(NoPlayer player, View containerView) {
preventerListener = new OnPotentialBuggyDriverLayoutListener(player);
containerView.addOnLayoutChangeListener(preventerListener);
}
/**
*
* @param bottomChangeView 一个在软件盘弹出是bottom会发生改变的View,如:被软件键盘向上顶的View,布局的根View。如果没有监听到,说明该View的bottom值没有随软键盘的弹出而改变
* @param onKeyboardListener
*/
public static void setListener(View bottomChangeView, OnKeyboardListener onKeyboardListener) {
bottomChangeView.addOnLayoutChangeListener(new KeyboardListener(bottomChangeView, onKeyboardListener));
}