Java Code Examples for android.support.design.widget.FloatingActionButton#getLayoutParams()

The following examples show how to use android.support.design.widget.FloatingActionButton#getLayoutParams() . 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: ToolbarHideShowBehavior.java    From JReadHub with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
    if (dependency instanceof AppBarLayout) {
        //FAB的自身高度+据底边高度的和=要实现FAB隐藏需要向下移动的距离
        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
        int fabBottomMargin = lp.bottomMargin;
        int distanceToScroll = fab.getHeight() + fabBottomMargin;
        if (Build.VERSION.SDK_INT > Build.VERSION_CODES.GINGERBREAD_MR1) {
            //toolbar被移走的距离与本身高度的比值
            float ty = dependency.getY() - (float) statusBarHeight;
            float ratio = ty / (float) toolbarHeight;
            //toolbar被移走几分之几,fab就向下滑几分之几
            fab.setTranslationY(-distanceToScroll * ratio);
        }
    }
    return true;
}
 
Example 2
Source File: ScaleDownShowBehavior.java    From v9porn with MIT License 5 votes vote down vote up
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
    boolean returnValue = super.onDependentViewChanged(parent, child, dependency);
    if (dependency instanceof AppBarLayout) {
        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
        int fabBottomMargin = lp.bottomMargin;
        int distanceToScroll = child.getHeight() + fabBottomMargin;
        float ratio = (float) dependency.getY() / (float) toolbarHeight;
        child.setTranslationY(-distanceToScroll * ratio);
    }

    return returnValue;
}
 
Example 3
Source File: MainActivity.java    From v9porn with MIT License 5 votes vote down vote up
private void hideFloatingActionButton(FloatingActionButton fabSearch) {
    ViewGroup.LayoutParams layoutParams = fabSearch.getLayoutParams();
    if (layoutParams instanceof CoordinatorLayout.LayoutParams) {
        CoordinatorLayout.LayoutParams coLayoutParams = (CoordinatorLayout.LayoutParams) layoutParams;
        FloatingActionButton.Behavior behavior = new FloatingActionButton.Behavior();
        coLayoutParams.setBehavior(behavior);
    }
    fabSearch.hide();
}
 
Example 4
Source File: FAB_Float_on_Scroll.java    From Newslly with MIT License 5 votes vote down vote up
@Override
public void onNestedScroll(CoordinatorLayout coordinatorLayout, FloatingActionButton child, View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
    super.onNestedScroll(coordinatorLayout, child, target, dxConsumed, dyConsumed, dxUnconsumed, dyUnconsumed);

    //child -> Floating Action Button
    if (dyConsumed > 0) {
        CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
        int fab_bottomMargin = layoutParams.bottomMargin;
        child.animate().translationY(child.getHeight() + fab_bottomMargin).setInterpolator(new LinearInterpolator()).start();
    } else if (dyConsumed < 0) {
        child.animate().translationY(0).setInterpolator(new LinearInterpolator()).start();
    }
}
 
Example 5
Source File: ScaleDownShowBehavior.java    From v9porn with MIT License 5 votes vote down vote up
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton child, View dependency) {
    boolean returnValue = super.onDependentViewChanged(parent, child, dependency);
    if (dependency instanceof AppBarLayout) {
        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) child.getLayoutParams();
        int fabBottomMargin = lp.bottomMargin;
        int distanceToScroll = child.getHeight() + fabBottomMargin;
        float ratio = (float) dependency.getY() / (float) toolbarHeight;
        child.setTranslationY(-distanceToScroll * ratio);
    }

    return returnValue;
}
 
Example 6
Source File: MainActivity.java    From v9porn with MIT License 5 votes vote down vote up
private void hideFloatingActionButton(FloatingActionButton fabSearch) {
    ViewGroup.LayoutParams layoutParams = fabSearch.getLayoutParams();
    if (layoutParams instanceof CoordinatorLayout.LayoutParams) {
        CoordinatorLayout.LayoutParams coLayoutParams = (CoordinatorLayout.LayoutParams) layoutParams;
        FloatingActionButton.Behavior behavior = new FloatingActionButton.Behavior();
        coLayoutParams.setBehavior(behavior);
    }
    fabSearch.hide();
}
 
Example 7
Source File: BottomNavigationBar.java    From JD-Test with Apache License 2.0 5 votes vote down vote up
public void setFab(FloatingActionButton fab) {
    ViewGroup.LayoutParams layoutParams = fab.getLayoutParams();
    if (layoutParams != null && layoutParams instanceof CoordinatorLayout.LayoutParams) {
        CoordinatorLayout.LayoutParams coLayoutParams = (CoordinatorLayout.LayoutParams) layoutParams;
        BottomNavBarFabBehaviour bottomNavBarFabBehaviour = new BottomNavBarFabBehaviour();
        coLayoutParams.setBehavior(bottomNavBarFabBehaviour);
    }
}
 
Example 8
Source File: BottomNavigationBar.java    From JD-Test with Apache License 2.0 5 votes vote down vote up
private void setFab(FloatingActionButton fab, @FabBehaviour int fabBehaviour) {
    ViewGroup.LayoutParams layoutParams = fab.getLayoutParams();
    if (layoutParams != null && layoutParams instanceof CoordinatorLayout.LayoutParams) {
        CoordinatorLayout.LayoutParams coLayoutParams = (CoordinatorLayout.LayoutParams) layoutParams;
        BottomNavBarFabBehaviour bottomNavBarFabBehaviour = new BottomNavBarFabBehaviour();
        coLayoutParams.setBehavior(bottomNavBarFabBehaviour);
    }
}
 
Example 9
Source File: FloatingActionButtonBehavior.java    From mvvm-template with GNU General Public License v3.0 5 votes vote down vote up
@Override public boolean onDependentViewChanged(@NonNull
                                                final CoordinatorLayout parent, @NonNull
                                                final FloatingActionButton child, final View dependency) {
    log(TAG, INFO, "onDependentViewChanged: " + dependency);
    final List<View> list = parent.getDependencies(child);
    ViewGroup.MarginLayoutParams params = ((ViewGroup.MarginLayoutParams) child.getLayoutParams());
    int bottomMargin = (params.bottomMargin + params.rightMargin) - (params.topMargin + params.leftMargin);
    float t = 0;
    float t2 = 0;
    float t3 = 0;
    boolean result = false;
    for (View dep : list) {
        if (Snackbar.SnackbarLayout.class.isInstance(dep)) {
            t += dep.getTranslationY() - dep.getHeight();
            result = true;
        } else if (BottomNavigation.class.isInstance(dep)) {
            BottomNavigation navigation = (BottomNavigation) dep;
            t2 = navigation.getTranslationY() - navigation.getHeight() + bottomMargin;
            t += t2;
            result = true;

            if (navigationBarHeight > 0) {
                if (!navigation.isExpanded()) {
                    child.hide();
                } else {
                    child.show();
                }
            }
        }
    }

    if (navigationBarHeight > 0 && t2 < 0) {
        t = Math.min(t2, t + navigationBarHeight);
    }

    child.setTranslationY(t);
    return result;
}
 
Example 10
Source File: FloatingActionButtonBehavior.java    From island with Apache License 2.0 5 votes vote down vote up
@Override public boolean onDependentViewChanged(final CoordinatorLayout parent, final FloatingActionButton child, final View dependency) {
	// Block parent behavior for SnackBar if bottom sheet is visible
	if (dependency instanceof Snackbar.SnackbarLayout) {
		final ViewGroup.LayoutParams fab_general_params = child.getLayoutParams();
		if (fab_general_params instanceof CoordinatorLayout.LayoutParams) {
			final CoordinatorLayout.LayoutParams fab_params = ((CoordinatorLayout.LayoutParams) fab_general_params);
			final int anchor_id = fab_params.getAnchorId();
			if (anchor_id != 0) {
				final View anchor = parent.findViewById(anchor_id);
				if (anchor != null && anchor.getVisibility() == View.VISIBLE) return false;
			}
		}
	}
	return super.onDependentViewChanged(parent, child, dependency);
}
 
Example 11
Source File: ScrollingFABBehavior.java    From AwesomeSplash with MIT License 5 votes vote down vote up
@Override
public boolean onDependentViewChanged(CoordinatorLayout parent, FloatingActionButton fab, View dependency) {
    if (dependency instanceof AppBarLayout) {
        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
        int fabBottomMargin = lp.bottomMargin;
        int distanceToScroll = fab.getHeight() + fabBottomMargin;
        float ratio = (float) dependency.getY() / (float) toolbarHeight;
        fab.setTranslationY(-distanceToScroll * ratio);
    }
    return true;
}
 
Example 12
Source File: MainActivity.java    From explorer with Apache License 2.0 4 votes vote down vote up
private void initFloatingActionButton() {

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.floating_action_button);

        if (fab == null) return;

        fab.setOnClickListener(v -> actionCreate());

        if (name != null || type != null) {

            ViewGroup.LayoutParams layoutParams = fab.getLayoutParams();

            ((CoordinatorLayout.LayoutParams) layoutParams).setAnchorId(View.NO_ID);

            fab.setLayoutParams(layoutParams);

            fab.hide();
        }
    }