android.view.ViewTreeObserver Java Examples

The following examples show how to use android.view.ViewTreeObserver. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: HorizontalSampleActivity.java    From LadderLayoutManager with Apache License 2.0 6 votes vote down vote up
public VH(View itemView) {
    super(itemView);
    iv = (ImageView) itemView.findViewById(R.id.iv);
    tv = (TextView) itemView.findViewById(R.id.tv);
    ivc = (HWRatioContainer) itemView.findViewById(R.id.ivc);
    ivc.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                ivc.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                ivc.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
            ivc.setTranslationX(ivc.getWidth() >> 4);
        }
    });

}
 
Example #2
Source File: NavigatorView.java    From Mortar-architect with MIT License 6 votes vote down vote up
private void measureAndGetTransition(final View newView, final View previousView, final boolean removePreviousView, final ViewTransitionDirection direction, final ViewTransition transition, final Callback callback) {
    int width = newView.getWidth();
    int height = newView.getHeight();

    if (width > 0 && height > 0) {
        callback.onAnimatorReady(getAnimator(previousView, newView, removePreviousView, direction, transition, callback));
        return;
    }

    newView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            final ViewTreeObserver observer = newView.getViewTreeObserver();
            if (observer.isAlive()) {
                observer.removeOnPreDrawListener(this);
            }

            callback.onAnimatorReady(getAnimator(previousView, newView, removePreviousView, direction, transition, callback));
            return true;
        }
    });
}
 
Example #3
Source File: MapLayer.java    From MapView with MIT License 6 votes vote down vote up
public void setImage(Picture image) {
    this.image = image;

    if (mapView.getWidth() == 0) {
        ViewTreeObserver vto = mapView.getViewTreeObserver();
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                if (!hasMeasured) {
                    initMapLayer();
                    hasMeasured = true;
                }
                return true;
            }
        });
    } else {
        initMapLayer();
    }
}
 
Example #4
Source File: LEDView.java    From LEDView with Apache License 2.0 6 votes vote down vote up
private void init(){

        mHolder = getHolder();
        mHolder.addCallback(this);
        mThread = new HandlerThread("led-looper");
        mThread.start();
        mHandler = new Handler(mThread.getLooper());

        mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);


        getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                mLEDHight = getWidth();
                mLEDWidth = getHeight();
                Log.d("scott","   led size " + " h : " + mLEDHight + "   w : " + mLEDWidth);
                getViewTreeObserver().removeOnPreDrawListener(this);
                return false;
            }
        });
    }
 
Example #5
Source File: AndroidBug5497Workaround.java    From RxTools-master with Apache License 2.0 6 votes vote down vote up
private AndroidBug5497Workaround(Activity activity) {
    //获取状态栏的高度
    int resourceId = activity.getResources().getIdentifier("status_bar_height", "dimen", "android");
    statusBarHeight = activity.getResources().getDimensionPixelSize(resourceId);
    this.activity = activity;
    FrameLayout content = (FrameLayout)activity.findViewById(android.R.id.content);
    mChildOfContent = content.getChildAt(0);

    //界面出现变动都会调用这个监听事件
    mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            if (isfirst) {
                contentHeight = mChildOfContent.getHeight();//兼容华为等机型
                isfirst = false;
            }
            possiblyResizeChildOfContent();
        }
    });

    frameLayoutParams = (FrameLayout.LayoutParams)
            mChildOfContent.getLayoutParams();
}
 
Example #6
Source File: ViewTooltip.java    From ViewTooltip with Apache License 2.0 6 votes vote down vote up
public void setup(final Rect viewRect, int screenWidth) {
    this.viewRect = new Rect(viewRect);
    final Rect myRect = new Rect(viewRect);

    final boolean changed = adjustSize(myRect, screenWidth);
    if (!changed) {
        onSetup(myRect);
    } else {
        getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                onSetup(myRect);
                getViewTreeObserver().removeOnPreDrawListener(this);
                return false;
            }
        });
    }
}
 
Example #7
Source File: BannerBindingWrapper.java    From firebase-android-sdk with Apache License 2.0 6 votes vote down vote up
@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;
}
 
Example #8
Source File: EmbedBottomSheet.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onShow(DialogInterface dialog) {
    if (pipVideoView != null && videoView.isInline()) {
        videoView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                videoView.getViewTreeObserver().removeOnPreDrawListener(this);
                /*AndroidUtilities.runOnUIThread(new Runnable() {
                    @Override
                    public void run() {*/

                    /*}
                }, 100);*/
                return true;
            }
        });
    }
}
 
Example #9
Source File: DocActivityView.java    From Mupdf with Apache License 2.0 6 votes vote down vote up
protected void hidePages()
{
	LinearLayout pages = (LinearLayout) findViewById(R.id.pages_container);
	if (null == pages)
		return;

	if (pages.getVisibility() == View.GONE)
		return;

	pages.setVisibility(View.GONE);
	ViewTreeObserver observer = mDocView.getViewTreeObserver();
	observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
	{
		@Override
		public void onGlobalLayout()
		{
			mDocView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
			mDocView.onHidePages();
		}
	});
}
 
Example #10
Source File: Utils.java    From citrus with Apache License 2.0 6 votes vote down vote up
/**
 * Fix floating action button's layout bug.
 */
public static void setFabLayoutParams(final FloatingActionButton floatingActionButton,
        final OnCanSetLayoutParamsListener onCanSetLayoutParamsListener) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        ViewTreeObserver viewTreeObserver = floatingActionButton.getViewTreeObserver();
        if (viewTreeObserver.isAlive()) {
            viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                @Override
                public void onGlobalLayout() {
                    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                        floatingActionButton.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    } else {
                        floatingActionButton.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                    onCanSetLayoutParamsListener.onCanSetLayoutParams();
                }
            });
        }
    }
}
 
Example #11
Source File: ExpandTextView.java    From ViewUtils with Apache License 2.0 6 votes vote down vote up
public void setText(CharSequence charSequence) {
    textContent = charSequence;
    textView.setText(charSequence.toString());
    ViewTreeObserver viewTreeObserver = textView.getViewTreeObserver();
    viewTreeObserver.addOnPreDrawListener(new OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            if (!isInitTextView) {
                return true;
            }
            textLines = textView.getLineCount();
            isExpandNeeded = textLines > expandLines;
            isInitTextView = false;
            if (isExpandNeeded) {
                isShrink = true;
                doAnimation(textLines, expandLines, WHAT_ANIMATION_END);
            } else {
                isShrink = false;
                doNotExpand();
            }
            return true;
        }
    });

}
 
Example #12
Source File: SliderAdapter.java    From LoyalNativeSlider with MIT License 6 votes vote down vote up
private void collectionConfiguration(final View layer, final int position) {
    if (mSetViewListener != null && enable_layout_observer) {
        layer.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                int debug = layer.getHeight();
                Log.d("checkLayoutSlideHeight", debug + " px");
                measurement_height.append(position, layer.getHeight());
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    layer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
                if (enable_layout_observer) {
                    mSetViewListener.onLayoutGenerated(measurement_height);
                }
            }
        });
    }
}
 
Example #13
Source File: MatchLiveFragment.java    From SprintNBA with Apache License 2.0 6 votes vote down vote up
private void initData() {
    showLoadingDialog();
    mid = getArguments().getString("mid");
    adapter = new MatchLiveAdapter(list, mActivity, R.layout.item_list_match_live);
    lvMatchLive.setAdapter(adapter);
    lvMatchLive.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {
                lvMatchLive.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                lvMatchLive.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
            mListViewHeight = lvMatchLive.getHeight();
            lvMatchLive.setOnScrollListener(new OnLvScrollListener(mListViewHeight) {
                @Override
                public void onBottom() {
                    presenter.getMoreContent();
                }
            });
        }
    });
    presenter = new MatchLivePresenter(mActivity, this, mid);
}
 
Example #14
Source File: DialogsActivity.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onConfigurationChanged(Configuration newConfig)
{
    super.onConfigurationChanged(newConfig);
    if (!onlySelect && floatingButton != null)
    {
        floatingButton.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
        {
            @Override
            public void onGlobalLayout()
            {
                floatingButton.setTranslationY(floatingHidden ? (!FeaturedSettings.tabSettings.hideTabs && FeaturedSettings.tabSettings.tabsToBottom) ? AndroidUtilities.dp(150) : AndroidUtilities.dp(100) : 0);
                unreadFloatingButtonContainer.setTranslationY(floatingHidden ? AndroidUtilities.dp(74) : 0);
                floatingButton.setClickable(!floatingHidden);
                if (floatingButton != null)
                {
                    floatingButton.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
            }
        });
    }
}
 
Example #15
Source File: InputMethodUtils.java    From timecat with Apache License 2.0 6 votes vote down vote up
/**
 * 设置View树监听,以便判断键盘是否弹出。
 * 【只有当Activity的windowSoftInputMode设置为adjustResize时才有效】
 */
private void detectKeyboard() {
    final View activityRootView = activity.findViewById(android.R.id.content);
    if (activityRootView != null) {
        ViewTreeObserver observer = activityRootView.getViewTreeObserver();
        if (observer == null) {
            return;
        }
        observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                final Rect r = new Rect();
                activityRootView.getWindowVisibleDisplayFrame(r);
                int heightDiff = DisplayUtils.getScreenHeight() - (r.bottom - r.top);
                boolean show = heightDiff >= sKeyBoardHeight / 3;
                setKeyboardShowing(show);// 设置键盘是否展开状态
                if (show) {
                    int keyboardHeight = heightDiff - DisplayUtils.getStatusBarHeight();
                    // 设置新的键盘高度
                    setKeyBoardHeight(keyboardHeight);
                    haskownKeyboardHeight = true;
                }
            }
        });
    }
}
 
Example #16
Source File: ViewTarget.java    From giffun with Apache License 2.0 6 votes vote down vote up
public void getSize(SizeReadyCallback cb) {
    int currentWidth = getViewWidthOrParam();
    int currentHeight = getViewHeightOrParam();
    if (isSizeValid(currentWidth) && isSizeValid(currentHeight)) {
        cb.onSizeReady(currentWidth, currentHeight);
    } else {
        // We want to notify callbacks in the order they were added and we only expect one or two callbacks to
        // be added a time, so a List is a reasonable choice.
        if (!cbs.contains(cb)) {
            cbs.add(cb);
        }
        if (layoutListener == null) {
            final ViewTreeObserver observer = view.getViewTreeObserver();
            layoutListener = new SizeDeterminerLayoutListener(this);
            observer.addOnPreDrawListener(layoutListener);
        }
    }
}
 
Example #17
Source File: ListViewFragment.java    From spruce-android with MIT License 6 votes vote down vote up
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, @Nullable Bundle savedInstanceState) {
    listView = (ListView) container.findViewById(R.id.list_view);

    // Create the animator after the list view has finished laying out
    listView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            initSpruce();
        }
    });

    // Mock data objects
    List<ExampleData> placeHolderList = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        placeHolderList.add(new ExampleData());
    }

    // Remove default dividers and set adapter
    listView.setDivider(null);
    listView.setDividerHeight(0);
    listView.setAdapter(new ListViewAdapter(placeHolderList));

    return inflater.inflate(R.layout.list_view_fragment, container, false);
}
 
Example #18
Source File: ScaleImageView.java    From ClockView with Apache License 2.0 6 votes vote down vote up
/** 
 * 初始化事件监听 
 */  
private void initListener() {  
    // 强制设置模式  
    setScaleType(ScaleType.MATRIX);
    // 添加观察者  
    getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {  
            // 移除观察者  
            getViewTreeObserver().removeGlobalOnLayoutListener(this);  
            // 获取控件大小  
            mWidth = getWidth();  
            mHeight = getHeight();  
  
            //通过getDrawable获得Src的图片  
            mDrawable = getDrawable();  
            if (mDrawable == null)  
                return;  
            mDrawableWidth = mDrawable.getIntrinsicWidth();  
            mDrawableHeight = mDrawable.getIntrinsicHeight();  
            initImageViewSize();  
            moveToCenter();  
        }  
    });  
}
 
Example #19
Source File: MainActivity.java    From mr-mantou-android with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onActivityReenter(int resultCode, Intent data) {
    super.onActivityReenter(resultCode, data);

    supportPostponeEnterTransition();

    reenterState = new Bundle(data.getExtras());

    binding.content.scrollToPosition(reenterState.getInt("index", 0));
    binding.content.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            binding.content.getViewTreeObserver().removeOnPreDrawListener(this);
            supportStartPostponedEnterTransition();
            return true;
        }
    });
}
 
Example #20
Source File: TabsPagerTitleStrip.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
public void notifyDataSetChanged()
{
    if (pager.getAdapter() == null)
        return;

    tabsContainer.removeAllViews();
    tabCount = pager.getAdapter().getCount();
    if (tabCount < 2)
        return;

    for (int i = 0; i < tabCount; i++)
        addIconTabWithCounter(i, ((IconTabProvider) pager.getAdapter()).getPageIconResId(i));

    updateTabStyles();
    getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
    {
        @Override
        public void onGlobalLayout()
        {
            getViewTreeObserver().removeOnGlobalLayoutListener(this);
            currentPosition = pager.getCurrentItem();
            currentPage = currentPosition;
            scrollToChild(currentPosition, 0);
        }
    });
}
 
Example #21
Source File: VideoGridViewAdapter.java    From SimpleVideoEdit with Apache License 2.0 6 votes vote down vote up
@Override
public VideoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final LayoutInflater inflater = LayoutInflater.from(parent.getContext());

    final View view = inflater.inflate(R.layout.video_select_gridview_item, null);

    if (actualDimensions == null) {
        view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                if (actualDimensions == null) {
                    actualDimensions = new int[] { UIUtil.getScreenWidth(view.getContext()) / 4, UIUtil.getScreenWidth(view.getContext()) / 4 };
                }
                view.getViewTreeObserver().removeOnPreDrawListener(this);
                return true;
            }
        });
    }

    return new VideoViewHolder(view);
}
 
Example #22
Source File: MaterialSheet.java    From FABtransitions with MIT License 6 votes vote down vote up
public  MaterialSheet(final View view){
    FrameLayout.LayoutParams params = (FrameLayout.LayoutParams)view.getLayoutParams();
    params.gravity = (Gravity.RIGHT);
    params.setMargins(Margins[0],Margins[1],Margins[2],Margins[3]);
    view.setLayoutParams(params);
    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {

            revealX  = (view.getLeft() + view.getRight()) / 2;
            revealY  = (view.getTop() + view.getBottom()) / 2;
            revealRadius = Math.max(view.getWidth(), view.getHeight());
            fabX = (fab.getLeft() +fab.getRight()) / 2;
            fabY = (fab.getTop() + fab.getBottom()) / 2;


        }
    });

}
 
Example #23
Source File: BaseFragment.java    From SmartFlasher with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver
            .OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            if (getActivity() == null) return;
            onViewFinished();
        }
    });
}
 
Example #24
Source File: FloatingActionButtonImpl.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@NonNull
private ViewTreeObserver.OnPreDrawListener getOrCreatePreDrawListener() {
  if (preDrawListener == null) {
    preDrawListener =
        new ViewTreeObserver.OnPreDrawListener() {
          @Override
          public boolean onPreDraw() {
            FloatingActionButtonImpl.this.onPreDraw();
            return true;
          }
        };
  }

  return preDrawListener;
}
 
Example #25
Source File: CoordinatorLayout.java    From ticdesign with Apache License 2.0 5 votes vote down vote up
/**
 * Remove the pre-draw listener if we're attached to a window and mark that we currently
 * do not need it when attached.
 */
void removePreDrawListener() {
    if (mIsAttachedToWindow) {
        if (mOnPreDrawListener != null) {
            final ViewTreeObserver vto = getViewTreeObserver();
            vto.removeOnPreDrawListener(mOnPreDrawListener);
        }
    }
    mNeedsPreDrawListener = false;
}
 
Example #26
Source File: FullScreenKeyboradBug.java    From MaterialQQLite with Apache License 2.0 5 votes vote down vote up
private FullScreenKeyboradBug(Activity activity) {
    FrameLayout content = (FrameLayout) activity.findViewById(android.R.id.content);
    mChildOfContent = content.getChildAt(0);
    mChildOfContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        public void onGlobalLayout() {
            possiblyResizeChildOfContent();
        }
    });
    frameLayoutParams = (FrameLayout.LayoutParams) mChildOfContent.getLayoutParams();
}
 
Example #27
Source File: PhotoPickerActivity.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void fixLayout() {
    if (listView != null) {
        ViewTreeObserver obs = listView.getViewTreeObserver();
        obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                fixLayoutInternal();
                if (listView != null) {
                    listView.getViewTreeObserver().removeOnPreDrawListener(this);
                }
                return true;
            }
        });
    }
}
 
Example #28
Source File: AdWebView.java    From mobile-sdk-android with Apache License 2.0 5 votes vote down vote up
private void removeViewTreeObserverListeners() {
    ViewTreeObserver treeObserver = getViewTreeObserver();
    if (treeObserver.isAlive()) {
        treeObserver.removeOnScrollChangedListener(this);
        treeObserver.removeGlobalOnLayoutListener(this);
    }
}
 
Example #29
Source File: ActionBarPopupWindow.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private void init() {
    if (superListenerField != null) {
        try {
            mSuperScrollListener = (ViewTreeObserver.OnScrollChangedListener) superListenerField.get(this);
            superListenerField.set(this, NOP);
        } catch (Exception e) {
            mSuperScrollListener = null;
        }
    }
}
 
Example #30
Source File: ViewTreeObserverCompat.java    From android-signaturepad with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
    }
}