Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/218.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 具有可检查实现的FloatingActionButton_Android_Floating Action Button - Fatal编程技术网

Android 具有可检查实现的FloatingActionButton

Android 具有可检查实现的FloatingActionButton,android,floating-action-button,Android,Floating Action Button,我用一个AppBar、FloatingActionButton和一个RecyclerView创建了一个活动。我实现自己的FAB,因为我希望它是可检查的。这是我的密码: public class CheckableFloatingActionButton extends FloatingActionButton implements Checkable { private static final int[] CHECKED_STATE_SET = {android.R.attr.sta

我用一个
AppBar
FloatingActionButton
和一个
RecyclerView
创建了一个
活动。我实现自己的FAB,因为我希望它是可检查的。这是我的密码:

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;
    }

}
它一直在工作,直到我上下滚动并隐藏/显示晶圆厂。正如您所看到的,FAB正在为可绘制图形设置动画,而不是立即设置。这就是为什么(我认为)这个错误会发生。有办法解决吗


如果这是一个floatingacions菜单,您可以在其展开或否时使用侦听器,为什么不在其展开时使用侦听器检查“checked”参数是否为真,以及它是否为可绘制的填充

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

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

Android Studio中的FloatingActionButton基本示例包括一个
可检查的
实现
FloatingActionButton
。使用
折叠工具栏布局
向上/向下滚动时效果良好。但我认为使用将是一个更优雅的解决方案