Example usage for android.view ViewTreeObserver addOnGlobalLayoutListener

List of usage examples for android.view ViewTreeObserver addOnGlobalLayoutListener

Introduction

In this page you can find the example usage for android.view ViewTreeObserver addOnGlobalLayoutListener.

Prototype

public void addOnGlobalLayoutListener(OnGlobalLayoutListener listener) 

Source Link

Document

Register a callback to be invoked when the global layout state or the visibility of views within the view tree changes

Usage

From source file:org.i_chera.wolfensteineditor.fragments.LevelFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, final Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_level, container, false);
    mDrawerLayout = (DrawerLayout) v;//from  ww  w .  ja  v  a2 s .c  o m

    mGridLayout = (RelativeLayout) v.findViewById(R.id.grid_layout);
    mVerticalScroll = (VXScrollView) v.findViewById(R.id.vertical_scroll);
    mHorizontalScroll = (HXScrollView) v.findViewById(R.id.horizontal_scroll);
    mWallList = (ListView) v.findViewById(R.id.wall_list);
    mLeftDrawer = (LinearLayout) v.findViewById(R.id.left_drawer);
    mScrollLockCheck = (CheckBox) v.findViewById(R.id.scroll_lock_check);
    mCentralContent = (RelativeLayout) v.findViewById(R.id.central_content);

    mHorizontalScroll.setScrollingEnabled(false);
    mHorizontalScroll.setOnTouchListener(this);
    mVerticalScroll.setOnTouchListener(this);
    mVerticalScroll.setAlwaysInterceptMove(true);
    mCentralContent.setOnTouchListener(this);
    //        mVerticalScroll.setClickable(true);
    //        mHorizontalScroll.setOnClickListener(this);
    mHorizontalScroll.setScrollViewListener(this);
    mVerticalScroll.setScrollViewListener(this);
    mScrollLockCheck.setOnClickListener(this);
    mScrollLockCheck.setOnCheckedChangeListener(this);

    int tColor = MainActivity.FLOOR_COLOUR;
    tColor = Color.argb(200, Color.red(tColor), Color.green(tColor), Color.blue(tColor));
    mScrollLockCheck.setBackgroundColor(tColor);

    mLeftDrawer.setBackgroundColor(MainActivity.FLOOR_COLOUR);
    mWallList.setAdapter(new WallListAdapter());
    mWallList.setOnItemClickListener(this);

    mGridLayout.getLayoutParams().width = mGridLayout.getLayoutParams().height = LevelContainer.MAPSIZE
            * mTileSize;

    ViewTreeObserver observer = mVerticalScroll.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        // Can't get rid of warning otherwise. Can't use the recommended
        // function here
        @SuppressWarnings("deprecation")
        @Override
        public void onGlobalLayout() {
            mVerticalScroll.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            mViewportSize = new Point(mVerticalScroll.getMeasuredWidth(), mVerticalScroll.getMeasuredHeight());
            Bundle b = getActualState(savedInstanceState);
            if (b != null) {
                mHorizontalScroll.scrollTo(b.getInt(EXTRA_SCROLL_X), 0);
                mVerticalScroll.scrollTo(0, b.getInt(EXTRA_SCROLL_Y));
                mCurrentWallChoice = b.getInt(EXTRA_CURRENT_WALL_CHOICE);
                boolean checked = b.getBoolean(EXTRA_SCROLL_LOCK);
                mScrollLockCheck.setChecked(checked);
            }
            updateData();
        }
    });

    return v;
}

From source file:com.cachirulop.moneybox.fragment.MoneyboxFragment.java

/**
 * Register the event OnGlobalLayoutListener to fill the moneybox when the
 * layout is created./*from w  w  w .  j av  a2 s .c o m*/
 */
private void registerLayoutListener() {
    final View v;
    final ViewTreeObserver vto;
    Activity parent;

    parent = getActivity();

    v = parent.findViewById(R.id.moneyDropLayout);
    vto = v.getViewTreeObserver();
    if (vto.isAlive()) {
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            public void onGlobalLayout() {
                initMoneybox();

                v.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        });
    }
}

From source file:com.example.google.walkway.MainActivity.java

@SuppressLint("InlinedApi")
@Override/* w ww .  ja  va2s  .co m*/
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    supportRequestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    setContentView(R.layout.activity_main);

    mSelectedPlaceTypes.addAll(Arrays.asList(NAV_PLACE_TYPES));
    mPlaces = retrievePlaces();

    setupMapIfNeeded();

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);

    mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.drawable.ic_drawer, R.string.app_name,
            R.string.app_name) {

        public void onDrawerClosed(View view) {
            // getActionBar().setTitle(mTitle);

            if (view.equals(mNavListView)) {
                // XXX no need to recreate the markers if the place
                // types haven't changed.
                mMap.clear();
                mPlaces = retrievePlaces();

                if (mPlaceViewPager != null) {
                    mPlaceViewPager.getAdapter().notifyDataSetChanged();
                    mPlaceViewPager.requestLayout();
                }

                addPlacesToMap();
                setSelectedPlace(0);
                showPlacesOnMap(true); // overrides animation from
                                       // setSelectedPlace
                setupPlaceList();
            }
        }

        public void onDrawerOpened(View drawerView) {
            // getActionBar().setTitle(mDrawerTitle);
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

    setupPlaceViewPager();
    setupPlaceList();
    setupNavList();

    // Set the map padding once the UI views have dimensions.
    ViewTreeObserver observer = mDrawerLayout.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            removeListener();

            // Pad the top of the map to accommodate the transparent action
            // bar.
            TypedValue tv = new TypedValue();
            int actionbarHeight = 0;
            if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
                actionbarHeight = TypedValue.complexToDimensionPixelSize(tv.data,
                        getResources().getDisplayMetrics());
            }

            // Pad the bottom of the map to accommodate the place viewpager.
            int viewPagerHeight = 0;
            if (mPlaceViewPager != null) {
                viewPagerHeight = mPlaceViewPager.getHeight();
            }

            // If the place list isn't in a DrawerLayout, pad the map for it.
            int placeListWidth = 0;
            // Use if listview overlays map.
            //                if (mPlaceListView.getVisibility() == View.VISIBLE) {
            //                    placeListWidth = mPlaceListView.getWidth();
            //                }

            mMap.setPadding(0, actionbarHeight, placeListWidth, viewPagerHeight);

            // Once the map is ready, add the markers to it.
            addPlacesToMap();
            showPlacesOnMap(false);
            setSelectedPlace(0);
        }

        @SuppressWarnings("deprecation")
        @SuppressLint("NewApi")
        private void removeListener() {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                mDrawerLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            } else {
                mDrawerLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
            }
        }
    });
}

From source file:io.github.sin3hz.fastjumper.sample.PhotoListFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    mSwipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override//from w  ww .  j av a 2s  . c  o  m
        public void onRefresh() {
            getLoaderManager().restartLoader(0, null, mLoaderCallbacks);
        }
    });
    mAdapter = new PhotoListAdapter();
    final ViewTreeObserver vto = mRecyclerView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int width = mRecyclerView.getWidth() - mRecyclerView.getPaddingLeft()
                    - mRecyclerView.getPaddingRight();
            if (width <= 0) {
                return;
            }
            mRecyclerView.setAdapter(mAdapter);
            mRecyclerView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            setupLayoutManager();
        }
    });
    setupFastJumper();
}

From source file:com.conferenceengineer.android.iosched.ui.SessionDetailFragment.java

private void setupCustomScrolling(View rootView) {
    mAddSchedulePlaceholderView = rootView.findViewById(R.id.add_to_schedule_button_placeholder);
    if (mAddSchedulePlaceholderView == null) {
        if (mType != null && !ScheduleContract.Sessions.SESSION_TYPE_KEYNOTE.equals(mType)) {
            mAddScheduleButton.setVisibility(View.VISIBLE);
        }//from  www  .ja  v  a2s. c  om
        return;
    }

    mScrollView = (ObservableScrollView) rootView.findViewById(R.id.scroll_view);
    mScrollView.setCallbacks(this);
    ViewTreeObserver vto = mScrollView.getViewTreeObserver();
    if (vto.isAlive()) {
        vto.addOnGlobalLayoutListener(mGlobalLayoutListener);
    }
}

From source file:com.limewoodmedia.nsdroid.activities.Region.java

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.region);/*from  w  ww .  j  a v  a 2  s .  com*/

    // Fetch flag
    LoadingHelper.loadHomeFlag(this);
    imageLoader = Utils.getImageLoader(this);
    this.options = Utils.getImageLoaderDisplayOptions();

    ActionBar actionBar = getSupportActionBar();
    actionBar.setHomeButtonEnabled(true);

    Utils.setupNavigationDrawer(this);

    viewPager = (ViewPager) findViewById(R.id.pager);

    // WFE
    wfe = (ViewGroup) getLayoutInflater().inflate(R.layout.region_wfe, viewPager, false);
    regionName = (TextView) wfe.findViewById(R.id.region_name);
    flag = (ImageView) wfe.findViewById(R.id.region_flag);
    delegate = (TextView) wfe.findViewById(R.id.region_delegate);
    founder = (TextView) wfe.findViewById(R.id.region_founder);
    text = (TextView) wfe.findViewById(R.id.wfe);
    delegate.setMovementMethod(LinkMovementMethod.getInstance());
    founder.setMovementMethod(LinkMovementMethod.getInstance());
    text.setMovementMethod(LinkMovementMethod.getInstance());
    fieldset = (ViewGroup) wfe.findViewById(R.id.fieldset);
    header = (TextView) wfe.findViewById(R.id.wfe_header);
    ViewTreeObserver observer = header.getViewTreeObserver();
    observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            fieldset.setPadding(fieldset.getPaddingLeft(),
                    header.getHeight() - (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 15,
                            getResources().getDisplayMetrics()),
                    fieldset.getPaddingRight(), fieldset.getPaddingBottom());
        }
    });
    layout = (ViewGroup) wfe.findViewById(R.id.region_layout);

    // RMB
    rmb = (ViewGroup) getLayoutInflater().inflate(R.layout.rmb_single, viewPager, false);
    rmbInner = (RMBFragment) getSupportFragmentManager().findFragmentById(R.id.rmb);
    rmbRegion = (TextView) rmb.findViewById(R.id.rmb_region_name);
    // Page navigation
    previous = (ImageView) rmb.findViewById(R.id.rmb_previous);
    previous.setOnClickListener(rmbClickListener);
    page = (ImageView) rmb.findViewById(R.id.rmb_page);
    page.setOnClickListener(rmbClickListener);
    next = (ImageView) rmb.findViewById(R.id.rmb_next);
    next.setOnClickListener(rmbClickListener);

    // Officers
    officers = (ViewGroup) getLayoutInflater().inflate(R.layout.officers_single, viewPager, false);
    officersTitle = (TextView) officers.findViewById(R.id.officers_title);
    officersInner = (OfficersFragment) getSupportFragmentManager().findFragmentById(R.id.officers);

    // Embassies
    embassies = (ViewGroup) getLayoutInflater().inflate(R.layout.embassies_single, viewPager, false);
    embassiesTitle = (TextView) embassies.findViewById(R.id.embassies_title);
    embassiesInner = (EmbassiesFragment) getSupportFragmentManager().findFragmentById(R.id.embassies);

    // Set up view pager
    viewPager.setAdapter(new RegionPagerAdapter());
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            // Update context menu
            supportInvalidateOptionsMenu();
        }

        @Override
        public void onPageScrollStateChanged(int state) {
        }
    });

    // Check if going straight to RMB / Embassies
    if (getIntent().hasExtra("page")) {
        viewPager.setCurrentItem(getIntent().getIntExtra("page", 0), false);
    }

    Log.d(TAG, "DATA " + getIntent().getDataString());
    if (getIntent().getData() == null) {
        // Get user's region
        homeRegion = true;
        region = NationInfo.getInstance(this).getRegionId();
    } else {
        String dataStr = getIntent().getDataString();
        if (dataStr.startsWith("com.limewoodMedia.nsdroid.region.rmb://")) {
            region = dataStr.replace("com.limewoodMedia.nsdroid.region.rmb://", "");
            viewPager.setCurrentItem(1, false);
        } else if (dataStr.startsWith("com.limewoodMedia.nsdroid.region.embassies://")) {
            region = dataStr.replace("com.limewoodMedia.nsdroid.region.embassies://", "");
            viewPager.setCurrentItem(2, false);
        } else if (dataStr.startsWith("com.limewoodMedia.nsdroid.region://")) {
            region = dataStr.replace("com.limewoodMedia.nsdroid.region://", "");
        }
    }
    rmbInner.setRegionName(region);
    officersInner.setRegionName(region);
    embassiesInner.setRegionName(region);

    //        if(savedInstanceState == null) {
    loadRegion();
    //        } else {
    //           // Restore state
    //           data = savedInstanceState.getParcelable("region_data");
    //           doSetup();
    //        }
}

From source file:ca.zadrox.dota2esportticker.ui.LiveContentView.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_live_stats);

    mMaxHeaderElevation = getResources().getDimensionPixelSize(R.dimen.session_detail_max_header_elevation);

    mScrollView = (ObservableScrollView) findViewById(R.id.game_scroll_view);
    mScrollView.addCallbacks(this);
    ViewTreeObserver vto = mScrollView.getViewTreeObserver();
    if (vto.isAlive()) {
        vto.addOnGlobalLayoutListener(mGlobalLayoutListener);
    }/*from  w w w.j  av a 2s  .c o m*/

    mScrollViewChild = findViewById(R.id.game_scroll_view_child);

    mDetailsContainer = findViewById(R.id.game_details_container);

    mDetailsContainer.setAlpha(0);
    mDetailsContainer.setY(1200);

    mHeaderBox = findViewById(R.id.header_game);
    mGameViewContainer = findViewById(R.id.game_photo_container);

    mGameImageView = (ImageView) findViewById(R.id.game_photo);
    mGameTimeView = (TextView) findViewById(R.id.game_time_view);
    mGameScoreViewLeft = (TextView) findViewById(R.id.game_score_view_left);
    mGameScoreViewRight = (TextView) findViewById(R.id.game_score_view_right);
    tickerLayout = (LinearLayout) findViewById(R.id.sidedrawer);
    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mTitle = (TextView) findViewById(R.id.header_title);
    mSubtitle = (TextView) findViewById(R.id.header_subtitle);
    mUpdateProgressBar = (ProgressBar) findViewById(R.id.header_progressbar);

    mapView = (ImageView) findViewById(R.id.game_map_view);
    mHasPhoto = true;
    mGameImageView.setColorFilter(Color.rgb(123, 123, 123), android.graphics.PorterDuff.Mode.MULTIPLY);

    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();

    Picasso.with(this).load(R.drawable.drawable_dota_bg_dire_ancient).config(Bitmap.Config.ARGB_8888)
            .resize(Math.round(displayMetrics.widthPixels * PHOTO_ASPECT_RATIO * 2 / 3),
                    displayMetrics.widthPixels * 2 / 3)
            .transform(new CropImageTransform(displayMetrics.heightPixels, displayMetrics.widthPixels))
            .into(mGameImageView);

    setBundleContents();
    initMapView();

    mPresenter = new LiveStatsPresenter(this);
    mLiveTickerAdapter = new LiveTickerAdapter(this, R.layout.sidedrawer_list_item);

    if (savedInstanceState != null) {
        mLiveTickerAdapter.onRestoreInstanceState(savedInstanceState);
        gameCompleted = savedInstanceState.getBoolean(KEY_GAME_COMPLETE, false);
    }

    mDrawerListView = (ListView) findViewById(R.id.sidedrawer_items_list);
    mDrawerListView.setAdapter(mLiveTickerAdapter);

    mUpdateProgressAnim = new ProgressBarAnimation(mUpdateProgressBar, 2000, 0);
    mUpdateProgressAnim.setDuration(GAME_UPDATE_INTERVAL);
    mUpdateProgressAnim.setInterpolator(new LinearInterpolator());

    if (!PrefUtils.hasLiveDrawerBeenShown(this)) {
        mDrawerLayout.openDrawer(Gravity.END);
        Toast.makeText(this, "Match events are shown in the right drawer", Toast.LENGTH_SHORT).show();
        PrefUtils.setLiveDrawerShown(this);
    }
}

From source file:com.gdgdevfest.android.apps.devfestbcn.ui.SessionDetailFragment.java

private void setupCustomScrolling(View rootView) {
    mAddSchedulePlaceholderView = rootView.findViewById(R.id.add_to_schedule_button_placeholder);
    if (mAddSchedulePlaceholderView == null) {
        mAddScheduleButton.setVisibility(View.VISIBLE);
        return;//from   www .  j  a va  2  s.c om
    }

    mScrollView = (ObservableScrollView) rootView.findViewById(R.id.scroll_view);
    mScrollView.setCallbacks(this);
    ViewTreeObserver vto = mScrollView.getViewTreeObserver();
    if (vto.isAlive()) {
        vto.addOnGlobalLayoutListener(mGlobalLayoutListener);
    }
}

From source file:com.spoiledmilk.ibikecph.navigation.SMRouteNavigationMapFragment.java

public void zoomToBoundingBox() {
    final BoundingBoxE6 boundingBox = BoundingBoxE6.fromLocations(route.getWaypoints(), COORDINATE_PADDING);
    ViewTreeObserver vto = mapView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        boolean ran = false; // in case removeGlobalOnLayoutListener()
                             // doesn't work (happens on some systems)

        @SuppressLint("NewApi")
        @SuppressWarnings("deprecation")
        @Override/*from w  ww .  j  av a  2 s .c  o m*/
        public void onGlobalLayout() {
            if (!ran) {
                if (Build.VERSION.SDK_INT < 16) {
                    mapView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                } else {
                    mapView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                }
                mapView.zoomToBoundingBox(boundingBox);
            }
            ran = true;
        }
    });
}

From source file:koma.movieapp.ui.MovieDetailActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    //UIUtils.tryTranslateHttpIntent(this);

    boolean shouldBeFloatingWindow = shouldBeFloatingWindow();
    if (shouldBeFloatingWindow) {
        setupFloatingWindow();/*w ww. j  av  a 2  s. c  om*/
    }

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_movie_detail);

    final Toolbar toolbar = getActionBarToolbar();
    toolbar.setNavigationIcon(shouldBeFloatingWindow ? R.drawable.ic_ab_close : R.drawable.ic_up);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finishAfterTransition();
        }
    });
    mHandler.post(new Runnable() {
        @Override
        public void run() {
            toolbar.setTitle("");
        }
    });

    if (savedInstanceState == null) {
        Uri sessionUri = getIntent().getData();
    }

    mArguments = getIntent().getExtras();

    //
    //        if (extras == null) {
    //            return;
    //        }
    //
    //        mMovieId = Integer.parseInt(extras.getString("movieId"));

    mFABElevation = getResources().getDimensionPixelSize(R.dimen.fab_elevation);
    mMaxHeaderElevation = getResources().getDimensionPixelSize(R.dimen.session_detail_max_header_elevation);

    mTagColorDotSize = getResources().getDimensionPixelSize(R.dimen.tag_color_dot_size);

    mHandler = new Handler();

    if (mBackdropImageLoader == null) {
        mBackdropImageLoader = new ImageLoader(this, R.drawable.person_image_empty);
    }
    if (mNoPlaceholderImageLoader == null) {
        mNoPlaceholderImageLoader = new ImageLoader(this);
    }

    mScrollView = (ObservableScrollView) findViewById(R.id.scroll_view);
    mScrollView.addCallbacks(this);
    ViewTreeObserver vto = mScrollView.getViewTreeObserver();
    if (vto.isAlive()) {
        vto.addOnGlobalLayoutListener(mGlobalLayoutListener);
    }

    mScrollViewChild = findViewById(R.id.scroll_view_child);
    mScrollViewChild.setVisibility(View.INVISIBLE);

    mDetailsContainer = findViewById(R.id.details_container);
    mHeaderBox = findViewById(R.id.header_movie);
    mTitle = (TextView) findViewById(R.id.movie_title);
    mMovieRating = (TextView) findViewById(R.id.movie_rating);
    mMovieRuntime = (TextView) findViewById(R.id.movie_runtime);
    mPhotoViewContainer = findViewById(R.id.movie_photo_container);
    mPhotoView = (ImageView) findViewById(R.id.movie_backdrop);

    mMovieOverview = (TextView) findViewById(R.id.movie_overview);
    mMovieDirectors = (LinearLayout) findViewById(R.id.movie_directors_container);
    //mMovieWriters = (LinearLayout) findViewById(R.id.movie_writers_container);
    //mMovieProducers = (LinearLayout) findViewById(R.id.movie_producers_container);
    mMovieCast = (LinearLayout) findViewById(R.id.movie_cast_container);
    mMovieGenres = (LinearLayout) findViewById(R.id.movie_genre);
    mMovieGenresContainer = (ViewGroup) findViewById(R.id.movie_genres_container);

    mAddScheduleButton = (CheckableFrameLayout) findViewById(R.id.add_schedule_button);

    //ViewCompat.setTransitionName(mPhotoView, TRANSITION_NAME_PHOTO);

    LoaderManager manager = getLoaderManager();
    manager.initLoader(0, mArguments, this);
}