com.google.android.material.appbar.AppBarLayout Java Examples

The following examples show how to use com.google.android.material.appbar.AppBarLayout. 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: ViewSubredditDetailActivity.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.view_subreddit_detail_activity, menu);
    applyMenuItemTheme(menu);
    mMenu = menu;
    MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_view_subreddit_detail_activity);
    if (isInLazyMode) {
        lazyModeItem.setTitle(R.string.action_stop_lazy_mode);
        params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
        collapsingToolbarLayout.setLayoutParams(params);
    } else {
        lazyModeItem.setTitle(R.string.action_start_lazy_mode);
        params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS |
                AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED);
        collapsingToolbarLayout.setLayoutParams(params);
    }
    return true;
}
 
Example #2
Source File: PostActivity.java    From hipda with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_post);
    mRootView = findViewById(R.id.main_activity_root_view);
    mAppBarLayout = (AppBarLayout) findViewById(R.id.appbar_layout);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    if (getSupportActionBar() != null)
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    FragmentManager fragmentManager = getSupportFragmentManager();
    if (fragmentManager.findFragmentById(R.id.main_frame_container) == null) {
        Bundle arguments = getIntent().getExtras();
        PostFragment fragment = new PostFragment();
        fragment.setArguments(arguments);
        fragmentManager.beginTransaction()
                .add(R.id.main_frame_container, fragment).commit();
    }

    setSwipeBackEnable(false);
}
 
Example #3
Source File: NestedScrollAppBarLayout.java    From Mysplash with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child,
                                   View directTargetChild, View target, int nestedScrollAxes, int type) {
    if (super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes, type)
            && type == ViewCompat.TYPE_TOUCH) {
        bindAppBar(child);
        if (appBarLayout.nestedScrollingListener != null) {
            appBarLayout.nestedScrollingListener.onStartNestedScroll();
        }
        appBarLayout.stopScrollAnimator();
        appBarLayout.setStartY(child.getY());
        return true;
    } else {
        return false;
    }
}
 
Example #4
Source File: AccountSavedThingActivity.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.account_saved_thing_activity, menu);
    applyMenuItemTheme(menu);
    mMenu = menu;
    MenuItem lazyModeItem = mMenu.findItem(R.id.action_lazy_mode_account_saved_thing_activity);
    if (isInLazyMode) {
        lazyModeItem.setTitle(R.string.action_stop_lazy_mode);
        params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_NO_SCROLL);
        collapsingToolbarLayout.setLayoutParams(params);
    } else {
        lazyModeItem.setTitle(R.string.action_start_lazy_mode);
        params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);
        collapsingToolbarLayout.setLayoutParams(params);
    }

    return true;
}
 
Example #5
Source File: AppBarStateChangeListener.java    From aptoide-client-v8 with GNU General Public License v3.0 6 votes vote down vote up
@Override public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
  if (i == 0) {
    if (mCurrentState != State.EXPANDED) {
      onStateChanged(appBarLayout, State.EXPANDED);
    }
    mCurrentState = State.EXPANDED;
  } else if (Math.abs(i) - appBarLayout.getTotalScrollRange() == 0) {
    if (mCurrentState != State.COLLAPSED) {
      onStateChanged(appBarLayout, State.COLLAPSED);
    }
    mCurrentState = State.COLLAPSED;
  } else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
    if (mCurrentState != State.MOVING) {
      onStateChanged(appBarLayout, State.MOVING);
    }
    mCurrentState = State.MOVING;
  } else {
    if (mCurrentState != State.IDLE) {
      onStateChanged(appBarLayout, State.IDLE);
    }
    mCurrentState = State.IDLE;
  }
}
 
Example #6
Source File: AppUtilsTest.java    From materialistic with Apache License 2.0 6 votes vote down vote up
@Test
public void testNavigate() {
    AppBarLayout appBar = mock(AppBarLayout.class);
    when(appBar.getBottom()).thenReturn(1);
    Navigable navigable = mock(Navigable.class);
    AppUtils.navigate(Navigable.DIRECTION_DOWN, appBar, navigable);
    verify(appBar).setExpanded(eq(false), anyBoolean());
    verify(navigable, never()).onNavigate(anyInt());

    when(appBar.getBottom()).thenReturn(0);
    AppUtils.navigate(Navigable.DIRECTION_DOWN, appBar, navigable);
    verify(navigable).onNavigate(eq(Navigable.DIRECTION_DOWN));

    AppUtils.navigate(Navigable.DIRECTION_RIGHT, appBar, navigable);
    verify(navigable).onNavigate(eq(Navigable.DIRECTION_RIGHT));

    AppUtils.navigate(Navigable.DIRECTION_UP, appBar, navigable);
    verify(navigable).onNavigate(eq(Navigable.DIRECTION_UP));

    AppUtils.navigate(Navigable.DIRECTION_LEFT, appBar, navigable);
    verify(navigable).onNavigate(eq(Navigable.DIRECTION_LEFT));
}
 
Example #7
Source File: FloatingActionButton.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onLayoutChild(
    @NonNull CoordinatorLayout parent,
    @NonNull FloatingActionButton child,
    int layoutDirection) {
  // First, let's make sure that the visibility of the FAB is consistent
  final List<View> dependencies = parent.getDependencies(child);
  for (int i = 0, count = dependencies.size(); i < count; i++) {
    final View dependency = dependencies.get(i);
    if (dependency instanceof AppBarLayout) {
      if (updateFabVisibilityForAppBarLayout(parent, (AppBarLayout) dependency, child)) {
        break;
      }
    } else if (isBottomSheet(dependency)) {
      if (updateFabVisibilityForBottomSheet(dependency, child)) {
        break;
      }
    }
  }
  // Now let the CoordinatorLayout lay out the FAB
  parent.onLayoutChild(child, layoutDirection);
  // Now offset it if needed
  offsetIfNeeded(parent, child);
  return true;
}
 
Example #8
Source File: DetailsActivity.java    From PopularMovies with MIT License 6 votes vote down vote up
/**
 * sets the title on the toolbar only if the toolbar is collapsed
 */
private void handleCollapsedToolbarTitle() {
    mBinding.appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        boolean isShow = true;
        int scrollRange = -1;

        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            if (scrollRange == -1) {
                scrollRange = appBarLayout.getTotalScrollRange();
            }
            // verify if the toolbar is completely collapsed and set the movie name as the title
            if (scrollRange + verticalOffset == 0) {
                mBinding.collapsingToolbar.setTitle(
                        mViewModel.getResult().getValue().data.getMovie().getTitle());
                isShow = true;
            } else if (isShow) {
                // display an empty string when toolbar is expanded
                mBinding.collapsingToolbar.setTitle(" ");
                isShow = false;
            }
        }
    });
}
 
Example #9
Source File: FlexibleSpaceExampleActivity.java    From CoordinatorExamples with Apache License 2.0 6 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.activity_flexible_space);

	mFab = findViewById(R.id.flexible_example_fab);

	Toolbar toolbar = (Toolbar) findViewById(R.id.flexible_example_toolbar);
	toolbar.setNavigationOnClickListener(new View.OnClickListener() {
		@Override public void onClick(View v) {
			onBackPressed();
		}
	});

	AppBarLayout appbar = (AppBarLayout) findViewById(R.id.flexible_example_appbar);
	appbar.addOnOffsetChangedListener(this);
}
 
Example #10
Source File: FabOffsetter.java    From appbarsyncedfab with Apache License 2.0 6 votes vote down vote up
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
    // fab should scroll out down in sync with the appBarLayout scrolling out up.
    // let's see how far along the way the appBarLayout is
    // (if displacementFraction == 0.0f then no displacement, appBar is fully expanded;
    //  if displacementFraction == 1.0f then full displacement, appBar is totally collapsed)
    float displacementFraction = -verticalOffset / (float) appBarLayout.getTotalScrollRange();

    // top position, accounting for translation not coming from this behavior
    float topUntranslatedFromThis = fab.getTop() + fab.getTranslationY() - fabTranslationYByThis;

    // total length to displace by (from position uninfluenced by this behavior) for a full appBar collapse
    float fullDisplacement = parent.getBottom() - topUntranslatedFromThis;

    // calculate new value for displacement coming from this behavior
    float newTranslationYFromThis = fullDisplacement * displacementFraction;

    // update translation value by difference found in this step
    fab.setTranslationY(newTranslationYFromThis - fabTranslationYByThis + fab.getTranslationY());

    // store new value
    fabTranslationYByThis = newTranslationYFromThis;
}
 
Example #11
Source File: SmsActivity.java    From hipda with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_sms);
    mRootView = findViewById(R.id.main_activity_root_view);
    mAppBarLayout = (AppBarLayout) findViewById(R.id.appbar_layout);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);

    if (getSupportActionBar() != null)
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    showFragment();
}
 
Example #12
Source File: AppBarStateChangeListener.java    From maoni with MIT License 6 votes vote down vote up
@Override
public final void onOffsetChanged(AppBarLayout appBarLayout, int i) {
  if (i == 0) {
    if (mCurrentState != State.EXPANDED) {
      onStateChanged(appBarLayout, State.EXPANDED);
    }
    mCurrentState = State.EXPANDED;
  } else if (Math.abs(i) >= appBarLayout.getTotalScrollRange()) {
    if (mCurrentState != State.COLLAPSED) {
      onStateChanged(appBarLayout, State.COLLAPSED);
    }
    mCurrentState = State.COLLAPSED;
  } else {
    if (mCurrentState != State.IDLE) {
      onStateChanged(appBarLayout, State.IDLE);
    }
    mCurrentState = State.IDLE;
  }
}
 
Example #13
Source File: AppBarLayoutOverScrollViewBehavior.java    From CrazyDaily with Apache License 2.0 6 votes vote down vote up
private void recovery(final AppBarLayout abl) {
    if (mTotalDy > 0) {
        mTotalDy = 0;
        if (isAnimate) {
            ValueAnimator anim = ValueAnimator.ofFloat(mLastScale, 1f).setDuration(200);
            anim.addUpdateListener(animation -> {
                float value = (float) animation.getAnimatedValue();
                mTargetView.setScaleX(value);
                mTargetView.setScaleY(value);
                abl.setBottom((int) (mLastBottom - (mLastBottom - mParentHeight) * animation.getAnimatedFraction()));
            });
            anim.start();
        } else {
            mTargetView.setScaleX(1f);
            mTargetView.setScaleY(1f);
            abl.setBottom(mParentHeight);
        }
    }
}
 
Example #14
Source File: UserProfileFragment.java    From edx-app-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onChildScrollingPreferenceChanged() {
    final int position = viewHolder.profileSectionTabs.getSelectedTabPosition();
    @AppBarLayout.LayoutParams.ScrollFlags
    final int scrollFlags;
    final Fragment fragment = (((StaticFragmentPagerAdapter) viewHolder.profileSectionPager.getAdapter()).getFragment(position));
    if (fragment != null) {
        if (position >= 0 &&
                ((ScrollingPreferenceChild) fragment).prefersScrollingHeader()) {
            scrollFlags = AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL;
        } else {
            scrollFlags = 0;
        }
        final AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) viewHolder.profileHeader.getLayoutParams();
        params.setScrollFlags(scrollFlags);
        viewHolder.profileHeader.setLayoutParams(params);
    }
}
 
Example #15
Source File: MainActivity.java    From MultiTypeRecyclerViewAdapter with Apache License 2.0 6 votes vote down vote up
private void initBar() {
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
    }
    mToolbar.setNavigationIcon(R.mipmap.ic_scan);
    mToolbar.setNavigationOnClickListener(v -> {
        refreshLoading();
    });
    mNavigationIcon = mToolbar.getNavigationIcon();
    mAppBar.addOnOffsetChangedListener((AppBarLayout appBarLayout, int verticalOffset) -> {
        int totalScrollRange = appBarLayout.getTotalScrollRange();
        float percent = Math.abs(verticalOffset * 1.0f / totalScrollRange);
        if (mNavigationIcon != null) {
            mNavigationIcon.setColorFilter((int) mArgbEvaluator.evaluate(percent, Color.WHITE, Color.BLACK), PorterDuff.Mode.SRC_IN);
        }
        if (mSearchItemIcon != null) {
            mSearchItemIcon.setColorFilter((int) mArgbEvaluator.evaluate(percent, Color.WHITE, Color.BLACK), PorterDuff.Mode.SRC_IN);
        }
        mTitle.setTextColor((int) mArgbEvaluator.evaluate(percent, Color.WHITE, Color.BLACK));
    });
}
 
Example #16
Source File: NestedWebViewAppBarLayout.java    From NestedWebView with MIT License 6 votes vote down vote up
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child,
        View target, int dx, int dy, int[] consumed, int type) {
    if (this.toolbar == null) {
        this.toolbar = findToolbar(child);
        if (this.toolbar == null) {
            return;
        }
    }
    int[] toolbarLocation = new int[2];
    toolbar.getLocationOnScreen(toolbarLocation);
    int[] targetLocation = new int[2];
    target.getLocationOnScreen(targetLocation);
    int toolbarBottomY = toolbarLocation[1] + toolbar.getHeight();
    int[] appbarLocation = new int[2];
    int nestedScrollViewTopY = targetLocation[1];
    child.getLocationOnScreen(appbarLocation);
    NestedWebView nestedWebView = (NestedWebView) target;
    if (nestedScrollViewTopY <= toolbarBottomY) {
        nestedWebView.onChangeCollapseToolbar(true, dy);
    } else {
        nestedWebView.onChangeCollapseToolbar(false, dy);
    }
    super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
}
 
Example #17
Source File: CollapsingTitleBarLayout.java    From UIWidget with Apache License 2.0 6 votes vote down vote up
@Override
protected void onAttachedToWindow() {
    super.onAttachedToWindow();

    // Add an OnOffsetChangedListener if possible
    final ViewParent parent = getParent();
    if (parent instanceof AppBarLayout) {
        // Copy over from the ABL whether we should fit system windows
        ViewCompat.setFitsSystemWindows(this, ViewCompat.getFitsSystemWindows((View) parent));

        if (mOnOffsetChangedListener == null) {
            mOnOffsetChangedListener = new OffsetUpdateListener();
        }
        ((AppBarLayout) parent).addOnOffsetChangedListener(mOnOffsetChangedListener);

        // We're attached, so lets request an inset dispatch
        ViewCompat.requestApplyInsets(this);
    }
}
 
Example #18
Source File: AppBarLayoutBehavior.java    From smart-farmer-android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child,
                                   View directTargetChild, View target,
                                   int nestedScrollAxes, int type) {
    LogUtil.d(TAG, "onStartNestedScroll");
    stopAppbarLayoutFling(child);
    return super.onStartNestedScroll(parent, child, directTargetChild, target,
            nestedScrollAxes, type);
}
 
Example #19
Source File: AppBarLayoutOverScrollViewBehavior.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
private void initial(AppBarLayout abl) {
        abl.setClipChildren(false);
        mParentHeight = abl.getHeight();
        mTargetViewHeight = mTargetView.getHeight();

        Log.e(TAG, "initial: mParentHeight==" + mParentHeight);
        Log.e(TAG, "initial: mTargetViewHeight==" + mTargetViewHeight);
//        mMiddleHeight = middleLayout.getHeight();
    }
 
Example #20
Source File: SetupBigHeaderActivity.java    From revolution-irc with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (mCustomContentViewId != 0)
        setContentView(mCustomContentViewId);
    else
        setContentView(R.layout.activity_setup_big_header);

    AppBarLayout appBar = findViewById(R.id.appbar);

    CollapsingToolbarLayout toolbarLayout = findViewById(R.id.toolbar_layout);

    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mLayout = findViewById(R.id.layout);
    mLayout.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
        int height = mLayout.getHeight();
        CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams)
                appBar.getLayoutParams();
        params.height = height / 3;
        appBar.setLayoutParams(params);

        int childHeight = (mContentView != null ? mContentView.getHeight() : 0);
        if (mContentView instanceof NestedScrollView)
            childHeight = ((NestedScrollView) mContentView).getChildAt(0).getHeight();
        boolean needsScroll = (mContentView != null && childHeight > height - params.height);

        AppBarLayout.LayoutParams paramsToolbar = (AppBarLayout.LayoutParams) toolbarLayout.getLayoutParams();
        paramsToolbar.setScrollFlags(needsScroll
                ? (AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED)
                : 0);
        toolbarLayout.setLayoutParams(paramsToolbar);
    });
}
 
Example #21
Source File: DesignModuleAttributeHelper.java    From proteus with Apache License 2.0 5 votes vote down vote up
private static AppBarLayout.LayoutParams getLayoutParams(View v) {
  initialize();
  AppBarLayout.LayoutParams result = null;
  ViewGroup.LayoutParams layoutParams = v.getLayoutParams();
  if (null != layoutParams && layoutParams instanceof AppBarLayout.LayoutParams) {
    result = (AppBarLayout.LayoutParams) layoutParams;
  }
  return result;
}
 
Example #22
Source File: AppBarLayoutOverScrollViewBehavior.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed, int type) {
    Log.e(TAG, "onNestedPreScroll: dy==" + dy);
    Log.e(TAG, "onNestedPreScroll: child.getBottom()==" + child.getBottom());

    if (!isRecovering) {
        if (mTargetView != null && ((dy < 0 && child.getBottom() >= mParentHeight)
                || (dy > 0 && child.getBottom() > mParentHeight))) {
            scale(child, target, dy);
            return;
        }
    }
    super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
}
 
Example #23
Source File: AppBarLayoutOverScrollViewBehavior.java    From MultiTypeRecyclerViewAdapter with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onNestedFling(@NonNull CoordinatorLayout coordinatorLayout, @NonNull AppBarLayout child, @NonNull View target, float velocityX, float velocityY, boolean consumed) {
    if (velocityY > 100) {
        isAnimate = false;
    }
    return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
}
 
Example #24
Source File: AppBarLayoutLiftOnScrollHack.java    From AndroidFastScroll with Apache License 2.0 5 votes vote down vote up
public static void hack(@NonNull AppBarLayout appBarLayout, int liftOnScrollTargetViewId) {
    appBarLayout.getViewTreeObserver().addOnPreDrawListener(() -> {
        // Invalidate the cached view reference so that this works after replacing fragment.
        appBarLayout.setLiftOnScrollTargetViewId(liftOnScrollTargetViewId);
        // Call AppBarLayout.Behavior.onNestedPreScroll() with dy == 0 to update lifted state.
        CoordinatorLayout.Behavior behavior =
                ((CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams()).getBehavior();
        CoordinatorLayout coordinatorLayout = (CoordinatorLayout) appBarLayout.getParent();
        behavior.onNestedPreScroll(coordinatorLayout, appBarLayout, coordinatorLayout, 0, 0,
                null, 0);
        return true;
    });
}
 
Example #25
Source File: AppBarLayoutOverScrollViewBehavior.java    From CrazyDaily with Apache License 2.0 5 votes vote down vote up
private void scale(AppBarLayout abl, View target, int dy) {
    mTotalDy += -dy;
    mTotalDy = Math.min(mTotalDy, TARGET_HEIGHT);
    mLastScale = Math.max(1f, 1f + mTotalDy / TARGET_HEIGHT);
    mTargetView.setScaleX(mLastScale);
    mTargetView.setScaleY(mLastScale);
    mLastBottom = mParentHeight + (int) (mTargetViewHeight / 2 * (mLastScale - 1));
    abl.setBottom(mLastBottom);
    target.setScrollY(0);
}
 
Example #26
Source File: FloatingActionButtonBehavior.java    From focus-android with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
    if (!enabled) {
        return;
    }

    if (verticalOffset == 0 && !visible) {
        showButton();
    } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange() && visible) {
        hideButton();
    }
}
 
Example #27
Source File: CollapsingToolbarLayoutActivity.java    From Android-skin-support with MIT License 5 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_collapsing_toolbar_layout);
    mIvPlaceholder = (ImageView) findViewById(R.id.main_iv_placeholder);
    mLlTitleContainer = (LinearLayout) findViewById(R.id.main_ll_title_container);
    mFlTitleContainer = (FrameLayout) findViewById(R.id.main_fl_title);
    mAblAppBar = (AppBarLayout) findViewById(R.id.main_abl_app_bar);
    mTvToolbarTitle = (TextView) findViewById(R.id.main_tv_toolbar_title);
    mTbToolbar = (Toolbar) findViewById(R.id.main_tb_toolbar);
    mTbToolbar.setTitle("");

    // AppBar的监听
    mAblAppBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            int maxScroll = appBarLayout.getTotalScrollRange();
            float percentage = (float) Math.abs(verticalOffset) / (float) maxScroll;
            handleAlphaOnTitle(percentage);
            handleToolbarTitleVisibility(percentage);
        }
    });

    findViewById(R.id.small_photo).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            startActivity(new Intent(CollapsingToolbarLayoutActivity.this, SettingsActivity.class));
        }
    });

    initParallaxValues(); // 自动滑动效果
}
 
Example #28
Source File: CollapsingTitleBarLayout.java    From UIWidget with Apache License 2.0 5 votes vote down vote up
@Override
protected void onDetachedFromWindow() {
    // Remove our OnOffsetChangedListener if possible and it exists
    final ViewParent parent = getParent();
    if (mOnOffsetChangedListener != null && parent instanceof AppBarLayout) {
        ((AppBarLayout) parent).removeOnOffsetChangedListener(mOnOffsetChangedListener);
    }

    super.onDetachedFromWindow();
}
 
Example #29
Source File: ScreenStackFragment.java    From react-native-screens with MIT License 5 votes vote down vote up
@Override
public View onCreateView(LayoutInflater inflater,
                         @Nullable ViewGroup container,
                         @Nullable Bundle savedInstanceState) {
  CoordinatorLayout view = new NotifyingCoordinatorLayout(getContext(), this);
  CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(
          LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
  params.setBehavior(new AppBarLayout.ScrollingViewBehavior());
  mScreenView.setLayoutParams(params);
  view.addView(recycleView(mScreenView));

  mAppBarLayout = new AppBarLayout(getContext());
  // By default AppBarLayout will have a background color set but since we cover the whole layout
  // with toolbar (that can be semi-transparent) the bar layout background color does not pay a
  // role. On top of that it breaks screens animations when alfa offscreen compositing is off
  // (which is the default)
  mAppBarLayout.setBackgroundColor(Color.TRANSPARENT);
  mAppBarLayout.setLayoutParams(new AppBarLayout.LayoutParams(
          AppBarLayout.LayoutParams.MATCH_PARENT, AppBarLayout.LayoutParams.WRAP_CONTENT));
  view.addView(mAppBarLayout);

  if (mShadowHidden) {
    mAppBarLayout.setTargetElevation(0);
  }

  if (mToolbar != null) {
    mAppBarLayout.addView(recycleView(mToolbar));
  }

  return view;
}
 
Example #30
Source File: AppBarLayoutOverScrollViewBehavior.java    From MultiTypeRecyclerViewAdapter with Apache License 2.0 5 votes vote down vote up
private void scale(AppBarLayout abl, View target, int dy) {
    mTotalDy += -dy;
    mTotalDy = Math.min(mTotalDy, TARGET_HEIGHT);
    mLastScale = Math.max(1f, 1f + mTotalDy / TARGET_HEIGHT);
    mTargetView.setScaleX(mLastScale);
    mTargetView.setScaleY(mLastScale);
    mLastBottom = mParentHeight + (int) (mTargetViewHeight / 2 * (mLastScale - 1));
    abl.setBottom(mLastBottom);
    target.setScrollY(0);
}