Java Code Examples for android.widget.TextView#getViewTreeObserver()

The following examples show how to use android.widget.TextView#getViewTreeObserver() . 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: MainActivity.java    From ObservableScrollView with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //设置透明状态栏
    StatusbarUtils.enableTranslucentStatusbar(this);
    setContentView(R.layout.activity_main);

    //初始化控件
    mObservableScrollView = (ObservableScrollView) findViewById(R.id.sv_main_content);
    mTextView = (TextView) findViewById(R.id.tv_main_topContent);
    mHeaderContent = (LinearLayout) findViewById(R.id.ll_header_content);

    //获取标题栏高度
    ViewTreeObserver viewTreeObserver = mTextView.getViewTreeObserver();
    viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            mTextView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            mHeight = mTextView.getHeight() - mHeaderContent.getHeight();//这里取的高度应该为图片的高度-标题栏
            //注册滑动监听
            mObservableScrollView.setOnObservableScrollViewListener(MainActivity.this);
        }
    });
}
 
Example 2
Source File: ViewUtils.java    From PowerRecyclerView with Apache License 2.0 6 votes vote down vote up
public static void calculateTag1(TextView first, TextView second, final String text) {
    ViewTreeObserver observer = first.getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            Layout layout = first.getLayout();
            int lineEnd = layout.getLineEnd(0);
            String substring = text.substring(0, lineEnd);
            String substring1 = text.substring(lineEnd, text.length());
            Log.i("TAG", "onGlobalLayout:" + "+end:" + lineEnd);
            Log.i("TAG", "onGlobalLayout: 第一行的内容::" + substring);
            Log.i("TAG", "onGlobalLayout: 第二行的内容::" + substring1);
            if (TextUtils.isEmpty(substring1)) {
                second.setVisibility(View.GONE);
                second.setText(null);
            } else {
                second.setVisibility(View.VISIBLE);
                second.setText(substring1);
            }
            first.getViewTreeObserver().removeOnPreDrawListener(
                    this);
            return false;
        }
    });

}
 
Example 3
Source File: ViewUtils.java    From PowerRecyclerView with Apache License 2.0 6 votes vote down vote up
public static void calculateTag2(TextView tag, TextView title, final String text) {
    ViewTreeObserver observer = tag.getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            SpannableString spannableString = new SpannableString(text);
            LeadingMarginSpan.Standard what = new LeadingMarginSpan.Standard(tag.getWidth() + dip2px(tag.getContext(), 10), 0);
            spannableString.setSpan(what, 0, spannableString.length(), SpannableString.SPAN_INCLUSIVE_INCLUSIVE);
            title.setText(spannableString);
            tag.getViewTreeObserver().removeOnPreDrawListener(
                    this);
            return false;
        }
    });

}
 
Example 4
Source File: FlexibleSpaceToolbarScrollViewActivity.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.observable_scroll_view_activity_flexiblespacetoolbarscrollview);

    setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    mFlexibleSpaceView = findViewById(R.id.flexible_space);
    mTitleView = (TextView) findViewById(R.id.title);
    mTitleView.setText(getTitle());
    setTitle(null);
    mToolbarView = findViewById(R.id.toolbar);

    final ObservableScrollView scrollView = (ObservableScrollView) findViewById(R.id.scroll);
    scrollView.setScrollViewCallbacks(this);

    mFlexibleSpaceHeight = getResources().getDimensionPixelSize(R.dimen.flexible_space_height);
    int flexibleSpaceAndToolbarHeight = mFlexibleSpaceHeight + getActionBarSize();

    findViewById(R.id.body).setPadding(0, flexibleSpaceAndToolbarHeight, 0, 0);
    mFlexibleSpaceView.getLayoutParams().height = flexibleSpaceAndToolbarHeight;

    ViewTreeObserver vto = mTitleView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
                mTitleView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            } else {
                mTitleView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
            updateFlexibleSpaceText(scrollView.getCurrentScrollY());
        }
    });
}