FloatingActionButton doesn't hide

后端 未结 17 924
星月不相逢
星月不相逢 2020-11-30 03:42

I am trying to hide my FloatingActionButton fabLocation programmatically with :

fabLocation.setVisibility(View.GONE)

but it do

相关标签:
17条回答
  • 2020-11-30 04:25

    If you want to hide the fab, you can try this: fab.hide();

    else try fab.show();

    enjoy.

    0 讨论(0)
  • 2020-11-30 04:25

    To hide the floating button

    fab = (FloatingActionButton) findViewById(R.id.fab);
    CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
    p.setAnchorId(View.NO_ID);
    fab.setLayoutParams(p);
    fab.hide;
    

    and to re-show

    fab = (FloatingActionButton) findViewById(R.id.fab);
    CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
    p.setAnchorId(View.NO_ID);
    fab.setLayoutParams(p);
    fab.show;
    
    0 讨论(0)
  • 2020-11-30 04:26

    None of these other solutions helped me 100%. I needed repeated, animated show and hide (like FAB.show() and hide()) all while anchored to the app bar when visible.

    I ended up creating the FAB new every time I showed it, manually inserting and anchoring it, and animating it based on the support library implementation. It's gross, but it works perfectly.

    private void hidePlayButton(final FloatingActionButton fab) {
        // Cancel any animation from the default behavior
        fab.animate().cancel();
        fab.animate()
                   .scaleX(0f)
                   .scaleY(0f)
                   .alpha(0f)
                   .setDuration(200)
                   .setInterpolator(new FastOutLinearInInterpolator())
                   .setListener(new Animator.AnimatorListener() {
                       @Override public void onAnimationStart(Animator animation) {}
    
                       @Override public void onAnimationEnd(Animator animation) {
                          coordinatorLayout.removeView(fab);
                       }
    
                       @Override public void onAnimationCancel(Animator animation) {}
    
                       @Override public void onAnimationRepeat(Animator animation) {}
                   });
    }
    
    private void showPlayButton() {
        int fabSize = getResources().getDimensionPixelSize(R.dimen.fab_size);
        int margin = getResources().getDimensionPixelSize(R.dimen.fab_margin);
        final FloatingActionButton fab = new FloatingActionButton(getActivity());
        fab.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(getActivity(), R.color.tint)));
        fab.setImageDrawable(ContextCompat.getDrawable(getActivity(), R.drawable.play));
        CoordinatorLayout.LayoutParams p = new CoordinatorLayout.LayoutParams(fabSize, fabSize);
        p.rightMargin = p.leftMargin = p.bottomMargin = p.topMargin = margin;
        p.anchorGravity = Gravity.BOTTOM | Gravity.END;
        p.setAnchorId(R.id.appbar);
        fab.setLayoutParams(p);
        // Start from 1 pixel
        fab.setAlpha(0f);
        fab.setScaleX(0f);
        fab.setScaleY(0f);
        binding.coordinatorLayout.addView(fab);
        fab.animate()
           .alpha(1f)
           .scaleX(1f)
           .scaleY(1f)
           .setDuration(200)
           .setInterpolator(new FastOutLinearInInterpolator());
    
        fab.setOnClickListener(new View.OnClickListener() {
            @Override public void onClick(View v) {
                hidePlayButton(fab);
                // do action
            }
        });
    }
    
    0 讨论(0)
  • 2020-11-30 04:26

    Simple code :

       public static void setVisibilityFab(FloatingActionButton fab, int visibility)
      {
    
            CoordinatorLayout.LayoutParams p = (CoordinatorLayout.LayoutParams) fab.getLayoutParams();
            p.setAnchorId(View.NO_ID);
            fab.setLayoutParams(p);
            if(visibility==View.GONE || visibility==View.INVISIBLE)
                fab.setVisibility(View.GONE);
            else
                fab.setVisibility(View.VISIBLE);
        }
    
    0 讨论(0)
  • 2020-11-30 04:29

    here is simple solution

    private var fabAnchorId: Int = View.NO_ID
    private val fabParams: CoordinatorLayout.LayoutParams
            get() = (fab.layoutParams as CoordinatorLayout.LayoutParams)
    
    fun showFab() {
        fabParams.anchorId = fabAnchorId
        fabParams.gravity = Gravity.NO_GRAVITY
        fab.show()
    }
    
    fun hideFab() {
        fabParams.anchorId = View.NO_ID
        fabParams.gravity = Gravity.END.or(Gravity.BOTTOM)
        fab.hide()
    }
    

    before show/hide we have to change anchor and gravity

    0 讨论(0)
提交回复
热议问题