FloatingActionButton checkable

I created Activity

with AppBar

, FloatingActionButton

and a RecyclerView

. I am implementing my own FAB because I want it to be testable. Here is my code:

public class CheckableFloatingActionButton extends FloatingActionButton implements Checkable {

    private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};

    private boolean checked = false;
    private boolean broadcasting;

    private OnCheckedChangeListener onCheckedChangeListener;

    public interface OnCheckedChangeListener {

        void onCheckedChanged(CheckableFloatingActionButton fab, boolean isChecked);
    }

    public CheckableFloatingActionButton(Context context) {
        this(context, null);
    }

    public CheckableFloatingActionButton(Context context, AttributeSet attrs) {
        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CheckableFloatingActionButton);
        setChecked(a.getBoolean(R.styleable.CheckableFloatingActionButton_checked, false));
        a.recycle();

    }

    @Override
    public void setChecked(boolean checked) {
        if (this.checked != checked) {
            this.checked = checked;
            refreshDrawableState();
            if (broadcasting) {
                return;
            }

            broadcasting = true;
            if (onCheckedChangeListener != null) {
                onCheckedChangeListener.onCheckedChanged(this, this.checked);
            }

            broadcasting = false;
        }
    }

    public void toggle(boolean animate) {
        if (animate) {
            setChecked(checked);
            return;
        }
        checked = !checked;
        jumpDrawablesToCurrentState();

    }

    public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
        onCheckedChangeListener = listener;
    }

    @Override
    public boolean performClick() {
        toggle();
        return super.performClick();
    }

    @Override
    public boolean isChecked() {
        return checked;
    }

    @Override
    public void toggle() {
        setChecked(!checked);
    }

    @Override
    public int[] onCreateDrawableState(int extraSpace) {
        final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
        if (isChecked()) {
            mergeDrawableStates(drawableState, CHECKED_STATE_SET);
        }
        return drawableState;
    }

}

      

Works until I scroll up / down and hide / show the FAB. As you can see, the FAB animates the pull-out rather than immediately. This is why (I think) the error occurs. Is there a way to solve it?

link

+3


source to share


2 answers


The FloatingActionButtonBasic sample in Android Studio includes an implementation Checkable

FloatingActionButton

. It works well when you scroll up / down with CollapsingToolbarLayout

. But I think using https://developer.android.com/reference/android/widget/CompoundButton.html would be an even more elegant solution.



+1


source


If it is a FloatingAcionsMenu you can use a listener for when it expands or not, why don't you use a listener when it expands the check if the "checked" parameter is true and if it fills the drawable.



 fam.setOnFloatingActionsMenuUpdateListener(new FloatingActionsMenu.OnFloatingActionsMenuUpdateListener() {
        @Override
        public void onMenuExpanded() {
          if(checked){
             //do something
          }
        }

        @Override
        public void onMenuCollapsed() {
        }
    });

      

0


source







All Articles