@Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); // получаем плавающую кнопку из активити FloatingActionButton fab = ButterKnife.findById(getActivity(), R.id.fab); // получаем параметры Layout fab приведенные к родителю CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); // выставляем привязку якоря к appBarLayout params.setAnchorId(R.id.appbar_layout); // выставляем anchorGravity params.anchorGravity = Gravity.BOTTOM | Gravity.RIGHT; fab.setLayoutParams(params); // меняем иконку плавающей кнопки fab.setImageResource(R.drawable.ic_mode_edit_24dp); /* fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //создаем и вешаем новый обработчик на fab //выбираем действие для fab в зависимости от текущего режима if (sCurrentFunctionality.equals(FUNCTIONALITY_PROFILE_VIEW)) { setupFuncionality(FUNCTIONALITY_PROFILE_EDIT); } else { setupFuncionality(FUNCTIONALITY_PROFILE_VIEW); } } });*/ }
private int getMarginBottom(FloatingActionButton button) { int marginBottom = 0; final ViewGroup.LayoutParams layoutParams = button.getLayoutParams(); if (layoutParams instanceof ViewGroup.MarginLayoutParams) { marginBottom = ((ViewGroup.MarginLayoutParams) layoutParams).bottomMargin; } return marginBottom; }
private void showFab() { final CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) fabPhoto.getLayoutParams(); layoutParams.setAnchorId(R.id.layout_app_bar); layoutParams.anchorGravity = Gravity.RIGHT | Gravity.END | Gravity.BOTTOM; fabPhoto.requestLayout(); fabPhoto.show(); }
/** * Applies a workaround to fix {@link android.support.design.widget.FloatingActionButton} margins * pre-lollipop. * * @param resources to get screen density * @param fab floating action button to fix */ public static void fixFloatingActionButtonMargins(Resources resources, FloatingActionButton fab) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { final int underLollipopMargin = 8; final float scale = resources.getDisplayMetrics().density; ViewGroup.MarginLayoutParams p = (ViewGroup.MarginLayoutParams) fab.getLayoutParams(); p.setMargins(0, 0, getPixelsFromDP(scale, underLollipopMargin), 0); fab.setLayoutParams(p); } }
private void hideViews() { RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) searchButton.getLayoutParams(); int fabBottomMargin = lp.bottomMargin; searchButton .animate() .translationY(searchButton.getHeight() + fabBottomMargin) .setInterpolator(new AccelerateInterpolator(2)) .start(); }
/** To hide fab, you need to remove its anchor */ private void hideFab() { // Ugly bug makes the view go to bottom|center of screen before hiding, seems like you need to // implement your own fab behavior... fabPhoto.setVisibility(View.GONE); final CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) fabPhoto.getLayoutParams(); layoutParams.setAnchorId(View.NO_ID); fabPhoto.requestLayout(); fabPhoto.hide(); }
@Override public boolean onDependentViewChanged( CoordinatorLayout parent, FloatingActionButton fab, View dependency) { boolean returnValue = super.onDependentViewChanged(parent, fab, 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 returnValue; }
@Override public void onClick(View view) { PreferencesManager mPreferencesManager = new PreferencesManager(MainActivity.this); DisplayMetrics displaymetrics = new DisplayMetrics(); this.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics); int screenWidth = displaymetrics.widthPixels; int screenHeight = displaymetrics.heightPixels; switch (view.getId()) { case R.id.switchAnimation: if (isRevealEnabled) { switchAnimation.setText("Switch to Reveal"); isRevealEnabled = false; } else { switchAnimation.setText("Switch to Fadein"); isRevealEnabled = true; } mPreferencesManager.resetAll(); break; case R.id.reset: mPreferencesManager.resetAll(); break; case R.id.resetAndPlay: mPreferencesManager.resetAll(); new Handler(Looper.getMainLooper()) .postDelayed( new Runnable() { @Override public void run() { showIntro(fab, INTRO_CARD); } }, 400); break; case R.id.changePosAndPlay: mPreferencesManager.resetAll(); Random r = new Random(); int right = r.nextInt((screenWidth - Utils.dpToPx(16)) - 16) + 16; int bottom = r.nextInt((screenHeight - Utils.dpToPx(16)) - 16) + 16; CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); params.setMargins(Utils.dpToPx(16), Utils.dpToPx(16), right, bottom); fab.setLayoutParams(params); break; } }
@Override public boolean onDependentViewChanged( CoordinatorLayout parent, FloatingActionButton fab, View dependency) { if (dependency instanceof AppBarLayout) { AppBarLayout appBarLayout = (AppBarLayout) dependency; CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) fab.getLayoutParams(); int bottomMargin = lp.bottomMargin; int distanceToScroll = fab.getHeight() + bottomMargin; // Log.e("@@!!", ViewCompat.getY(appBarLayout) + "\t" + // appBarLayout.getTotalScrollRange()); float ratio = ViewCompat.getY(appBarLayout) / (float) appBarLayout.getTotalScrollRange(); if (ratio < -0.9f) { ratio = -1f; } ViewCompat.setTranslationY(fab, -distanceToScroll * ratio); return true; } return false; }