下面列出了android.view.ViewTreeObserver#OnGlobalLayoutListener ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void onAttachedToWindow(final RecyclerView view) {
super.onAttachedToWindow(view);
this.recyclerView = view;
layoutHelper = new LayoutHelper(this, recyclerView);
cacheHelper = new CacheHelper(flowLayoutOptions.itemsPerLine, layoutHelper.visibleAreaWidth());
if (layoutHelper.visibleAreaWidth() == 0) {
if (globalLayoutListener == null) {
globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
globalLayoutListener = null;
cacheHelper.contentAreaWidth(layoutHelper.visibleAreaWidth());
}
};
}
view.getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
}
}
@Nullable
@Override
public ViewTreeObserver.OnGlobalLayoutListener inflate(
Map<Action, View.OnClickListener> actionListeners,
View.OnClickListener dismissOnClickListener) {
View root = inflater.inflate(R.layout.banner, null);
bannerRoot = root.findViewById(R.id.banner_root);
bannerContentRoot = root.findViewById(R.id.banner_content_root);
bannerBody = root.findViewById(R.id.banner_body);
bannerImage = root.findViewById(R.id.banner_image);
bannerTitle = root.findViewById(R.id.banner_title);
if (message.getMessageType().equals(MessageType.BANNER)) {
BannerMessage bannerMessage = (BannerMessage) message;
setMessage(bannerMessage);
setLayoutConfig(config);
setSwipeDismissListener(dismissOnClickListener);
setActionListener(actionListeners.get(bannerMessage.getAction()));
}
return null;
}
@SuppressWarnings("deprecation")
public static void removeOnGlobalLayoutListenerCompat(View v, ViewTreeObserver.OnGlobalLayoutListener listener) {
if (hasJellyBean()) {
v.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
} else {
v.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
}
}
public static void removeGlobalLayoutObserver(final ViewTreeObserver observer,
ViewTreeObserver.OnGlobalLayoutListener layoutListener) {
if (Build.VERSION.SDK_INT < 16) {
observer.removeGlobalOnLayoutListener(layoutListener);
} else {
observer.removeOnGlobalLayoutListener(layoutListener);
}
}
/**
* Recommend invoked by {@link Activity#onCreate(Bundle)}
* For align the height of the keyboard to {@code target} as much as possible.
* For save the refresh the keyboard height to shared-preferences.
*
* @param activity contain the view
* @param target whose height will be align to the keyboard height.
* @param lis the listener to listen in: keyboard is showing or not.
* @see #saveKeyboardHeight(Context, int)
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public static ViewTreeObserver.OnGlobalLayoutListener attach(final Activity activity,
IPanelHeightTarget target,
/* Nullable */
OnKeyboardShowingListener lis) {
final ViewGroup contentView = activity.findViewById(android.R.id.content);
final boolean isFullScreen = ViewUtil.isFullScreen(activity);
final boolean isTranslucentStatus = ViewUtil.isTranslucentStatus(activity);
final boolean isFitSystemWindows = ViewUtil.isFitsSystemWindows(activity);
// get the screen height.
final Display display = activity.getWindowManager().getDefaultDisplay();
final int screenHeight;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
final Point screenSize = new Point();
display.getSize(screenSize);
screenHeight = screenSize.y;
} else {
//noinspection deprecation
screenHeight = display.getHeight();
}
ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new KeyboardStatusListener(
isFullScreen,
isTranslucentStatus,
isFitSystemWindows,
contentView,
target,
lis,
screenHeight);
contentView.getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
return globalLayoutListener;
}
@TargetApi(JELLY_BEAN)
public static void removeOnGlobalLayoutListener(ViewTreeObserver viewTreeObserver, ViewTreeObserver.OnGlobalLayoutListener listener) {
if (App.isCompatible(JELLY_BEAN)) {
viewTreeObserver.removeOnGlobalLayoutListener(listener);
} else {
viewTreeObserver.removeGlobalOnLayoutListener(listener);
}
}
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private void removeGlobalOnLayoutListener(ViewTreeObserver obs, ViewTreeObserver.OnGlobalLayoutListener listener) {
if (obs == null)
return;
if (Build.VERSION.SDK_INT < 16) {
obs.removeGlobalOnLayoutListener(listener);
} else {
obs.removeOnGlobalLayoutListener(listener);
}
}
/**
* @see android.view.ViewTreeObserver#removeOnGlobalLayoutListener()
*/
@SuppressWarnings("deprecation")
public static void removeOnGlobalLayoutListener(
View view, ViewTreeObserver.OnGlobalLayoutListener listener) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
} else {
view.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
}
}
public static void removeOnGlobalLayoutListener(View view, ViewTreeObserver.OnGlobalLayoutListener victim) {
if (view == null || victim == null) {
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(victim);
} else {
view.getViewTreeObserver().removeGlobalOnLayoutListener(victim);
}
}
@NonNull
@Override
public ViewTreeObserver.OnGlobalLayoutListener inflate(
Map<Action, View.OnClickListener> actionListeners,
View.OnClickListener dismissOnClickListener) {
View root = inflater.inflate(R.layout.card, null);
bodyScroll = root.findViewById(R.id.body_scroll);
primaryButton = root.findViewById(R.id.primary_button);
secondaryButton = root.findViewById(R.id.secondary_button);
imageView = root.findViewById(R.id.image_view);
messageBody = root.findViewById(R.id.message_body);
messageTitle = root.findViewById(R.id.message_title);
cardRoot = root.findViewById(R.id.card_root);
cardContentRoot = root.findViewById(R.id.card_content_root);
if (message.getMessageType().equals(MessageType.CARD)) {
cardMessage = (CardMessage) message;
setMessage(cardMessage);
setImage(cardMessage);
setButtons(actionListeners);
setLayoutConfig(config);
setDismissListener(dismissOnClickListener);
setViewBgColorFromHex(cardContentRoot, cardMessage.getBackgroundHexColor());
}
return layoutListener;
}
private ViewTreeObserver.OnGlobalLayoutListener getLayoutListener() {
return new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
boolean animationStarted = mAnimationStarted;
resetAll();
if (mAutoStart || animationStarted) {
startShimmerAnimation();
}
}
};
}
/**
* Recommend invoked by {@link Activity#onCreate(Bundle)}
* For align the height of the keyboard to {@code target} as much as possible.
* For save the refresh the keyboard height to shared-preferences.
*
* @param activity contain the view
* @param target whose height will be align to the keyboard height.
* @param lis the listener to listen in: keyboard is showing or not.
* @see #saveKeyboardHeight(Context, int)
*/
@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
public static ViewTreeObserver.OnGlobalLayoutListener attach(final Activity activity,
IPanelHeightTarget target,
/* Nullable */
OnKeyboardShowingListener lis) {
final ViewGroup contentView = activity.findViewById(android.R.id.content);
final boolean isFullScreen = ViewUtil.isFullScreen(activity);
final boolean isTranslucentStatus = ViewUtil.isTranslucentStatus(activity);
final boolean isFitSystemWindows = ViewUtil.isFitsSystemWindows(activity);
// get the screen height.
final Display display = activity.getWindowManager().getDefaultDisplay();
final int screenHeight;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
final Point screenSize = new Point();
display.getSize(screenSize);
screenHeight = screenSize.y;
} else {
//noinspection deprecation
screenHeight = display.getHeight();
}
ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new KeyboardStatusListener(
isFullScreen,
isTranslucentStatus,
isFitSystemWindows,
contentView,
target,
lis,
screenHeight);
contentView.getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
return globalLayoutListener;
}
@Nullable
@Override
public ViewTreeObserver.OnGlobalLayoutListener inflate(
Map<Action, View.OnClickListener> actionListeners,
View.OnClickListener dismissOnClickListener) {
View v = inflater.inflate(R.layout.image, null);
imageRoot = v.findViewById(R.id.image_root);
imageContentRoot = v.findViewById(R.id.image_content_root);
imageView = v.findViewById(R.id.image_view);
collapseButton = v.findViewById(R.id.collapse_button);
// Setup ImageView.
imageView.setMaxHeight(config.getMaxImageHeight());
imageView.setMaxWidth(config.getMaxImageWidth());
if (message.getMessageType().equals(MessageType.IMAGE_ONLY)) {
ImageOnlyMessage msg = (ImageOnlyMessage) message;
imageView.setVisibility(
(msg.getImageData() == null || TextUtils.isEmpty(msg.getImageData().getImageUrl()))
? View.GONE
: View.VISIBLE);
imageView.setOnClickListener(actionListeners.get(msg.getAction()));
}
// Setup dismiss button.
imageRoot.setDismissListener(dismissOnClickListener);
collapseButton.setOnClickListener(dismissOnClickListener);
return null;
}
@Test
public void streamListener_onNotifiedModalMessage_setsLayoutListener() {
resumeActivity(activity);
ViewTreeObserver.OnGlobalLayoutListener mockListener =
mock(ViewTreeObserver.OnGlobalLayoutListener.class);
modalBindingWrapper.setLayoutListener(mockListener);
listener.displayMessage(MODAL_MESSAGE_MODEL, callbacks);
modalBindingWrapper.getImageView().getViewTreeObserver().dispatchOnGlobalLayout();
verify(mockListener).onGlobalLayout();
}
/**
* Remove a previously installed global layout callback.
* @param observer the view observer
* @param victim the victim
*/
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
public static void removeOnGlobalLayoutListener(ViewTreeObserver observer, ViewTreeObserver.OnGlobalLayoutListener victim) {
// Future (API16+)...
if (Build.VERSION.SDK_INT >= 16) {
observer.removeOnGlobalLayoutListener(victim);
}
// Legacy...
else {
observer.removeGlobalOnLayoutListener(victim);
}
}
void start(ViewCanary viewCanary, ViewCanaryConfig config) {
Handler handler = ThreadUtil.createIfNotExistHandler(VIEW_CANARY_HANDLER);
callbacks = new SimpleActivityLifecycleCallbacks() {
private Map<Activity, ViewTreeObserver.OnGlobalLayoutListener> mOnGlobalLayoutListenerMap = new HashMap<>();
private Map<Activity, List<List<ViewIdWithSize>>> mRecentLayoutListRecords = new HashMap<>();
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
super.onActivityCreated(activity, savedInstanceState);
mRecentLayoutListRecords.put(activity, new ArrayList<>());
}
@Override
public void onActivityDestroyed(Activity activity) {
super.onActivityDestroyed(activity);
mRecentLayoutListRecords.remove(activity);
}
@Override
public void onActivityResumed(Activity activity) {
super.onActivityResumed(activity);
ViewGroup parent = (ViewGroup) activity.getWindow().getDecorView();
Runnable callback = inspectInner(new WeakReference<>(activity), viewCanary, config, mRecentLayoutListRecords);
ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = () -> {
handler.removeCallbacks(callback);
handler.postDelayed(callback, INSPECT_DELAY_TIME_MILLIS);
};
mOnGlobalLayoutListenerMap.put(activity, onGlobalLayoutListener);
parent.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
}
@Override
public void onActivityPaused(Activity activity) {
super.onActivityPaused(activity);
ViewTreeObserver.OnGlobalLayoutListener onGlobalLayoutListener = mOnGlobalLayoutListenerMap.remove(activity);
ViewGroup parent = (ViewGroup) activity.getWindow().getDecorView();
if (onGlobalLayoutListener != null) {
parent.getViewTreeObserver().removeOnGlobalLayoutListener(onGlobalLayoutListener);
}
}
};
GodEye.instance().getApplication().registerActivityLifecycleCallbacks(callbacks);
}
/**
* @see #attach(Activity, IPanelHeightTarget, OnKeyboardShowingListener)
*/
public static ViewTreeObserver.OnGlobalLayoutListener attach(final Activity activity,
IPanelHeightTarget target) {
return attach(activity, target, null);
}
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
config.useAccelerometer = false;
config.useGyroscope = false;
config.useCompass = false;
config.useRotationVectorSensor = false;
config.maxSimultaneousSounds = 16;
FileHandle home = new FileHandle(getContext().getExternalFilesDir(null));
final Client client = new Client(home, 360);
initialize(client, config);
Cvars.Client.Display.StatusBar.addStateListener(new CvarStateAdapter<Boolean>() {
@Override
public void onChanged(Cvar<Boolean> cvar, Boolean from, final Boolean to) {
getHandler().post(new Runnable() {
@Override
public void run() {
if (to.booleanValue()) {
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
} else {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
}
});
}
});
final ViewTreeObserver.OnGlobalLayoutListener globalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
Rect visibleDisplayFrame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(visibleDisplayFrame);
int height = /*content.getRootView().getHeight() -*/ Gdx.graphics.getHeight() - visibleDisplayFrame.height();
client.updateScreenBounds(0, height, 0, 0); // TODO: other params are unused for now
}
};
final View content = getWindow().getDecorView().findViewById(android.R.id.content);
content.getViewTreeObserver().addOnGlobalLayoutListener(globalLayoutListener);
Gdx.app.addLifecycleListener(new LifecycleListener() {
@Override
public void pause() {}
@Override
public void resume() {}
@Override
public void dispose() {
content.getViewTreeObserver().removeOnGlobalLayoutListener(globalLayoutListener);
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (mBubbleLayout == null)
{
mBubbleLayout = new BubbleLayout(getContext());
}
if (mAddView != null)
{
mBubbleLayout.addView(mAddView);
}
setContentView(mBubbleLayout);
final Window window = getWindow();
if (window == null) return;
if (mSoftShowUp)
{
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
}
window.setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
onAutoPosition();
setLook();
// mBubbleLayout.post(new Runnable()
// {
// @Override
// public void run()
// {
// dialogPosition();
// }
// });
mBubbleLayout.measure(0, 0);
dialogPosition();
mOnGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener()
{
int lastWidth, lastHeight;
@Override
public void onGlobalLayout()
{
if (lastWidth == mBubbleLayout.getMeasuredWidth() && lastHeight == mBubbleLayout.getMeasuredHeight()) return;
dialogPosition();
lastWidth = mBubbleLayout.getMeasuredWidth();
lastHeight = mBubbleLayout.getMeasuredHeight();
}
};
mBubbleLayout.getViewTreeObserver().addOnGlobalLayoutListener(mOnGlobalLayoutListener);
mBubbleLayout.setOnClickEdgeListener(new BubbleLayout.OnClickEdgeListener()
{
@Override
public void edge()
{
if (BubbleDialog.this.mCancelable)
{
dismiss();
}
}
});
}
public DefaultUnRegister(Activity activity, ViewTreeObserver.OnGlobalLayoutListener listener) {
this.activityRef = new WeakReference<>(activity);
this.listenerRef = new WeakReference<>(listener);
}