下面列出了android.widget.SearchView.OnQueryTextListener#android.view.ViewTreeObserver.OnGlobalLayoutListener 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void notifyDataSetChanged() {
tabsContainer.removeAllViews();
tabCount = pager.getAdapter().getCount();
for (int i = 0; i < tabCount; i++) {
if (pager.getAdapter() instanceof IconTabProvider) {
addIconTab(i, ((IconTabProvider) pager.getAdapter()).getPageIconDrawable(i));
}
}
updateTabStyles();
getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
getViewTreeObserver().removeOnGlobalLayoutListener(this);
currentPosition = pager.getCurrentItem();
scrollToChild(currentPosition, 0);
}
});
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
scene = new SceneView(this);
setContentView(scene);
getSupportActionBarNotNull().setDisplayHomeAsUpEnabled(true);
setInfoText(R.string.info_objects_control);
// Waiting for scene to be laid out before setting up the items
scene.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
scene.getViewTreeObserver().removeOnGlobalLayoutListener(this);
showItems();
}
});
}
public void setListenerToRootView() {
final View activityRootView = getWindow().getDecorView().findViewById(android.R.id.content);
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int heightDiff = activityRootView.getRootView().getHeight() - activityRootView.getHeight();
if (heightDiff > 100) { // 99% of the time the height diff will be due to a keyboard.
if (isOpened == false) {
//Do two things, make the view top visible and the editText smaller
}
isOpened = true;
} else if (isOpened == true) {
isOpened = false;
final RecyclerView recyclerView = (RecyclerView) findViewById(R.id.main_view);
recyclerView.getAdapter().notifyDataSetChanged();
}
}
});
}
private void initWithContext(Context context) {
mScroller = new Scroller(context, new DecelerateInterpolator());
// XListView need the scroll event, and it will dispatch the event to
// user's listener (as a proxy).
super.setOnScrollListener(this);
m_context = context;
// init header view
mHeaderView = new XListViewHeader(context);
mHeaderViewContent = (RelativeLayout) mHeaderView
.findViewById(R.id.xlistview_header_content);
addHeaderView(mHeaderView);
// init header height
mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mHeaderViewHeight = mHeaderViewContent.getHeight();
getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
private void initView() {
mHeader = (ViewGroup) findViewById(R.id.rl_top);
mHeaderChild = mHeader.getChildAt(0);
mContent = findViewById(R.id.ll_content);
mHeader.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
mHeader.getViewTreeObserver().removeGlobalOnLayoutListener(this);
range = mHeader.getHeight();
mHeader.getLayoutParams().height = range;
mHeaderHeight = range;
}
});
}
private void initView(Context context) {
scroller = new Scroller(context, new DecelerateInterpolator());
headerView = new ZListViewHeader(context);
footerView = new ZListViewFooter(context);
headerViewContent = (RelativeLayout) headerView
.findViewById(R.id.xlistview_header_content);
headerView.getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
headerHeight = headerViewContent.getHeight();
getViewTreeObserver()
.removeGlobalOnLayoutListener(this);
}
});
addHeaderView(headerView);
}
/**
* Inflates the content, which is associated with a specific tab.
*
* @param tab
* The tab, whose content should be inflated, as an instance of the class {@link Tab}.
* The tab may not be null
* @param listener
* The layout listener, which should be notified, when the view has been inflated, as an
* instance of the type {@link OnGlobalLayoutListener} or null, if no listener should be
* notified
*/
private void inflateContent(@NonNull final Tab tab,
@Nullable final OnGlobalLayoutListener listener) {
Pair<View, Boolean> pair = contentViewRecycler.inflate(tab);
View view = pair.first;
if (listener != null) {
boolean inflated = pair.second;
if (inflated) {
view.getViewTreeObserver()
.addOnGlobalLayoutListener(new LayoutListenerWrapper(view, listener));
} else {
listener.onGlobalLayout();
}
}
}
/**
* Creates and returns a layout listener, which allows to position the content of a tab, after
* it has been inflated.
*
* @param tab
* The tab, whose content has been inflated, as an instance of the class {@link Tab}.
* The tab may not be null
* @return The layout listener, which has been created, as an instance of the type {@link
* OnGlobalLayoutListener}. The listener may not be null
*/
@NonNull
private OnGlobalLayoutListener createContentLayoutListener(@NonNull final Tab tab) {
return new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
View view = contentViewRecycler.getView(tab);
if (view != null) {
view.setX(0);
}
}
};
}
private void initWithContext(Context context) {
mScroller = new Scroller(context, new DecelerateInterpolator());
// XListView need the scroll event, and it will dispatch the event to
// user's listener (as a proxy).
super.setOnScrollListener(this);
m_context = context;
// init header view
mHeaderView = new XListViewHeader(context);
mHeaderViewContent = (RelativeLayout) mHeaderView.findViewById(R.id.xlistview_header_content);
addHeaderView(mHeaderView);
// init header height
mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mHeaderViewHeight = mHeaderViewContent.getHeight();
getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
}
/**
* Creates and returns a listener, which allows to observe, when the child of the dialog's
* scroll view has been layouted. If the scroll view's height is greater than necessary, its
* height is reduced to match the height of its child.
*
* @return The listener, which has been created, as an instance of the type {@link
* OnGlobalLayoutListener}. The listener may not be null
*/
@NonNull
private OnGlobalLayoutListener createScrollViewChildLayoutListener() {
return new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
View child = scrollView.getChildAt(0);
int childHeight = child.getHeight();
int containerHeight = scrollView.getHeight() - scrollView.getPaddingTop() -
scrollView.getPaddingBottom();
if (containerHeight > childHeight) {
LinearLayout.LayoutParams layoutParams =
(LinearLayout.LayoutParams) scrollView.getLayoutParams();
layoutParams.height = childHeight;
layoutParams.weight = 0;
scrollView.requestLayout();
}
ViewUtil.removeOnGlobalLayoutListener(child.getViewTreeObserver(), this);
}
};
}
public void moveToLeft() {
if (getWidth() == 0 || getHeight() == 0) {
final ViewTreeObserver observer = getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
moveToLeft();
getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
final RectF edges = mTempEdges;
getEdgesHelper(edges);
final float scale = mRenderer.scale;
mCenterX += Math.ceil(edges.left / scale);
updateCenter();
}
/**
* Creates and returns a layout listener, which allows to position a tab, which is a neighbor of
* the currently selected tab, when swiping horizontally.
*
* @param neighbor
* The tab item, which corresponds to the neighboring tab, as an instance of the class
* {@link TabItem}. The tab item may not be null
* @param dragDistance
* The distance of the swipe gesture in pixels as a {@link Float} value
* @return The layout listener, which has been created, as an instance of the type {@link
* OnGlobalLayoutListener}. The layout listener may not be null
*/
@NonNull
private OnGlobalLayoutListener createSwipeNeighborLayoutListener(
@NonNull final TabItem neighbor, final float dragDistance) {
return new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
float position;
if (dragDistance > 0) {
position = -getTabSwitcher().getWidth() + dragDistance - swipedTabDistance;
} else {
position = getTabSwitcher().getWidth() + dragDistance + swipedTabDistance;
}
getArithmetics().setPosition(Axis.X_AXIS, neighbor, position);
}
};
}
/**
* Creates and returns a layout listener, which allows to adapt the bottom margin of a tab, once
* its view has been inflated.
*
* @param item
* The item, which corresponds to the tab, whose view should be adapted, as an instance
* of the class {@link AbstractItem}. The item may not be null
* @return The layout listener, which has been created, as an instance of the type {@link
* OnGlobalLayoutListener}. The layout listener may not be null
*/
private OnGlobalLayoutListener createBottomMarginLayoutListener(
@NonNull final AbstractItem item) {
return new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
View view = item.getView();
if (tabViewBottomMargin == -1) {
tabViewBottomMargin = calculateBottomMargin(item);
}
FrameLayout.LayoutParams layoutParams =
(FrameLayout.LayoutParams) view.getLayoutParams();
layoutParams.bottomMargin = tabViewBottomMargin;
view.setLayoutParams(layoutParams);
}
};
}
/**
* Creates and returns a layout listener, which allows to adapt the size and position of a tab,
* once its view has been inflated.
*
* @param item
* The item, which corresponds to the tab, whose view should be adapted, as an instance
* of the class {@link AbstractItem}. The item may not be null
* @param dragging
* True, if the item is currently being dragged, false otherwise
* @param layoutListener
* The layout lister, which should be notified, when the created listener is invoked, as
* an instance of the type {@link OnGlobalLayoutListener} or null, if no listener should
* be notified
* @return The layout listener, which has been created, as an instance of the type {@link
* OnGlobalLayoutListener}. The layout listener may not be null
*/
@NonNull
private OnGlobalLayoutListener createInflateViewLayoutListener(@NonNull final AbstractItem item,
final boolean dragging,
@Nullable final OnGlobalLayoutListener layoutListener) {
return new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
adaptViewSize(item);
updateView(item, dragging);
if (layoutListener != null) {
layoutListener.onGlobalLayout();
}
}
};
}
/**
* Register soft input changed listener.
*
* @param window The window.
* @param listener The soft input changed listener.
*/
public static void registerSoftInputChangedListener(@NonNull final Window window,
@NonNull final OnSoftInputChangedListener listener) {
final int flags = window.getAttributes().flags;
if ((flags & WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS) != 0) {
window.clearFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
}
final FrameLayout contentView = window.findViewById(android.R.id.content);
final int[] decorViewInvisibleHeightPre = {getDecorViewInvisibleHeight(window)};
OnGlobalLayoutListener onGlobalLayoutListener = new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int height = getDecorViewInvisibleHeight(window);
if (decorViewInvisibleHeightPre[0] != height) {
listener.onSoftInputChanged(height);
decorViewInvisibleHeightPre[0] = height;
}
}
};
contentView.getViewTreeObserver().addOnGlobalLayoutListener(onGlobalLayoutListener);
contentView.setTag(TAG_ON_GLOBAL_LAYOUT_LISTENER, onGlobalLayoutListener);
}
/**
* Fix the bug of 5497 in Android.
* <p>It will clean the adjustResize</p>
*
* @param window The window.
*/
public static void fixAndroidBug5497(@NonNull final Window window) {
int softInputMode = window.getAttributes().softInputMode;
window.setSoftInputMode(softInputMode & ~WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
final FrameLayout contentView = window.findViewById(android.R.id.content);
final View contentViewChild = contentView.getChildAt(0);
final int paddingBottom = contentViewChild.getPaddingBottom();
final int[] contentViewInvisibleHeightPre5497 = {getContentViewInvisibleHeight(window)};
contentView.getViewTreeObserver()
.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
int height = getContentViewInvisibleHeight(window);
if (contentViewInvisibleHeightPre5497[0] != height) {
contentViewChild.setPadding(
contentViewChild.getPaddingLeft(),
contentViewChild.getPaddingTop(),
contentViewChild.getPaddingRight(),
paddingBottom + getDecorViewInvisibleHeight(window)
);
contentViewInvisibleHeightPre5497[0] = height;
}
}
});
}
@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (requestCenterMap) {
requestCenterMap = false;
final ViewTreeObserver observer = this.getViewTreeObserver();
if (observer.isAlive()) {
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
doCorrectPosition(true, false);
jumpTo(getOriginalMapWidth() / 2,
getOriginalMapHeight() / 2);
observer.removeGlobalOnLayoutListener(this);
}
});
}
} else {
doCorrectPosition(false, false);
}
}
private void init(Context context) {
mScroller = new Scroller(context, new DecelerateInterpolator());
super.setOnScrollListener(this);
// init header view
mHeaderView = new PullToRefreshListHeader(context);
mHeaderViewContent = (RelativeLayout) mHeaderView.findViewById(R.id.xlistview_header_content);
mHeaderTimeView = (TextView) mHeaderView.findViewById(R.id.xlistview_header_time);
addHeaderView(mHeaderView);
// init footer view
mFooterView = new PullToRefreshListFooter(context);
// init header height
mHeaderView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
mHeaderViewHeight = mHeaderViewContent.getHeight();
getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
});
MAX_X = dp2px(MAX_X);
MAX_Y = dp2px(MAX_Y);
mTouchState = TOUCH_STATE_NONE;
}
private void initializeScrollForRtl() {
final HorizontalScrollView scroll =
(HorizontalScrollView) findViewById(R.id.wallpaper_scroll_container);
if (scroll.getLayoutDirection() == View.LAYOUT_DIRECTION_RTL) {
final ViewTreeObserver observer = scroll.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
LinearLayout masterWallpaperList =
(LinearLayout) findViewById(R.id.master_wallpaper_list);
scroll.scrollTo(masterWallpaperList.getWidth(), 0);
scroll.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
}
}
public void notifyDataSetChanged() {
tabsContainer.removeAllViews();
tabCount = pager.getAdapter().getCount();
for (int i = 0; i < tabCount; i++)
addTabView(i, pager.getAdapter().getPageTitle(i).toString());
getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < 16)
getViewTreeObserver().removeGlobalOnLayoutListener(
this);
else
getViewTreeObserver().removeOnGlobalLayoutListener(
this);
currentPosition = pager.getCurrentItem();
setChooseTabViewTextColor(currentPosition);
scrollToChild(currentPosition, 0);
}
});
}
/**
* <br>调用时机:可见性 或者 布局状态改变的时候 如果view背景换了 那么一定invalited那么布局状态一定改变了
* <br>Register a callback to be invoked when the global layout state or the visibility of views within the view tree changes
*
* @param v
* @param gs 枚举表示 测量后是否移除此监听 目的是:为了防止被多次调用 (用完就移除 除非特殊一直想监听)
*/
public static void measure(final View v, final ListenerState gs, final OnMeasureListener oml) {
ViewTreeObserver vto = v.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
int height = v.getMeasuredHeight();
int width = v.getMeasuredWidth();
if (oml != null) {
oml.measureResult(v, width, height);
}
switch (gs) {
case MEASURE_REMOVE:
v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
break;
case MEASURE_CONTINUE:
break;
default:
break;
}
}
});
}
private void addOnGlobalLayoutListener(final int position) {
mOnGlobalLayoutListener = new OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
// int count = mFeedsListView.getHeaderViewsCount();
// int headerheight = 0;
// if (count > 0) {
// for (int i = 0; i < count; i++) {
// View view = mFeedsListView.getChildAt(i);
// headerheight += view.getHeight();
// }
// }
// mFeedsListView.setSelectionFromTop(position - 1, headerheight);
mRootView.getRootView().getViewTreeObserver()
.removeGlobalOnLayoutListener(mOnGlobalLayoutListener);
}
};
mRootView.getRootView().getViewTreeObserver()
.addOnGlobalLayoutListener(mOnGlobalLayoutListener);
}
/**
* @param children
* The child Views to add to parent.
* @param scrollToViewIdx
* The index of the View to scroll to after initialisation.
* @param sizeCallback
* A SizeCallback to interact with the HSV.
*/
public void initViews(View[] children, int scrollToViewIdx, SizeCallback sizeCallback) {
// A ViewGroup MUST be the only child of the HSV
ViewGroup parent = (ViewGroup) getChildAt(0);
// Add all the children, but add them invisible so that the layouts are calculated, but you can't see the Views
for (int i = 0; i < children.length; i++) {
children[i].setVisibility(View.INVISIBLE);
parent.addView(children[i]);
}
// Add a layout listener to this HSV
// This listener is responsible for arranging the child views.
OnGlobalLayoutListener listener = new MyOnGlobalLayoutListener(parent, children, scrollToViewIdx, sizeCallback);
getViewTreeObserver().addOnGlobalLayoutListener(listener);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
getViewTreeObserver().addOnGlobalLayoutListener(
new OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@Override
public void onGlobalLayout() {
createShader();
setMaskScale(1.0f);
if (mCallback != null) {
mCallback.onSetupAnimation(SpotlightView.this);
}
getViewTreeObserver()
.removeOnGlobalLayoutListener(this);
}
});
}
public void setSmallViewListener() {
this.rlSwitchScreen.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
X8MapVideoController.this.mapVideoCardView.changeSmallSize();
}
});
}
public void notifyDataSetChanged() {
tabsContainer.removeAllViews();
tabCount = pager.getAdapter().getCount();
for (int i = 0; i < tabCount; i++) {
if (pager.getAdapter() instanceof IconTabProvider) {
addIconTab(i, ((IconTabProvider) pager.getAdapter()).getPageIconResId(i));
} else {
addTextTab(i, pager.getAdapter().getPageTitle(i).toString());
}
}
updateTabStyles();
getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
getViewTreeObserver().removeOnGlobalLayoutListener(this);
currentPosition = pager.getCurrentItem();
scrollToChild(currentPosition, 0);
}
});
}
/**
* Helper method to remove a ViewTreeObserver correctly, i.e.
* avoiding the deprecated method on API level 16+.
* @param vto
* @param victim
*/
@SuppressWarnings("deprecation")
public static void removeViewTreeObserver(ViewTreeObserver vto, OnGlobalLayoutListener victim) {
if (MizLib.hasJellyBean()) {
vto.removeOnGlobalLayoutListener(victim);
} else {
vto.removeGlobalOnLayoutListener(victim);
}
}
public void notifyDataSetChanged() {
tabsContainer.removeAllViews();
tabCount = pager.getAdapter().getCount();
for (int i = 0; i < tabCount; i++) {
if (pager.getAdapter() instanceof IconTabProvider) {
addIconTab(i, ((IconTabProvider) pager.getAdapter()).getPageIconResId(i));
} else {
addTextTab(i, pager.getAdapter().getPageTitle(i).toString());
}
}
updateTabStyles();
getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
currentPosition = pager.getCurrentItem();
scrollToChild(currentPosition, 0);
}
});
}
public static void removeOnGlobalLayoutListener(View view,OnGlobalLayoutListener victim){
if(VERSION.SDK_INT>= VERSION_CODES.JELLY_BEAN){
view.getViewTreeObserver().removeOnGlobalLayoutListener(victim);
}else{
view.getViewTreeObserver().removeGlobalOnLayoutListener(victim);
}
}
/**
* If the soft keyboard is open, this method will close it. Currently only
* works for API 16 and above.
*/
private void closeKeyboardIfOpen()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
final View activityRootView = getWindow().getDecorView().getRootView();
final OnGlobalLayoutListener globalListener = new OnGlobalLayoutListener()
{
@Override
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onGlobalLayout()
{
Rect rect = new Rect();
// rect will be populated with the coordinates of your view that area still visible.
activityRootView.getWindowVisibleDisplayFrame(rect);
int heightDiff = activityRootView.getRootView().getHeight() - (rect.bottom - rect.top);
if (heightDiff > 100)
{
// If the difference is more than 100 pixels, it's probably caused by the soft keyboard being open. Now we want to close it.
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0); // Toggle the soft keyboard.
}
// Now we have to remove the OnGlobalLayoutListener, otherwise we will experience errors
activityRootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
};
activityRootView.getViewTreeObserver().addOnGlobalLayoutListener(globalListener);
}
}