如何在浮动操作按钮上创建自定义点击监听器

How to create a custom click listener on floating action button

如何在 FAB 上创建自定义点击侦听器或使用 return performClick()

我一直在尝试让 android 浮动按钮响应点击,但它对我的自定义点击事件不起作用,我还尝试在初始化 class 时传递自定义点击没有运气。请问我错过了什么?

import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class MovableFloatingActionButton extends FloatingActionButton implements View.OnTouchListener {

    private final static float CLICK_DRAG_TOLERANCE = 10; // Often, there will be a slight, unintentional, drag when the user taps the FAB, so we need to account for this.

    private float downRawX, downRawY;
    private float dX, dY;
    CustomClickListener customClickListener;

    public MovableFloatingActionButton(Context context) {
        super(context);
        init();
    }

    public MovableFloatingActionButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MovableFloatingActionButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        setOnTouchListener(this);
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent){

        ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)view.getLayoutParams();

        int action = motionEvent.getAction();
        if (action == MotionEvent.ACTION_DOWN) {

            downRawX = motionEvent.getRawX();
            downRawY = motionEvent.getRawY();
            dX = view.getX() - downRawX;
            dY = view.getY() - downRawY;

            return true; // Consumed

        }
        else if (action == MotionEvent.ACTION_MOVE) {

            int viewWidth = view.getWidth();
            int viewHeight = view.getHeight();

            View viewParent = (View)view.getParent();
            int parentWidth = viewParent.getWidth();
            int parentHeight = viewParent.getHeight();

            float newX = motionEvent.getRawX() + dX;
            newX = Math.max(layoutParams.leftMargin, newX); // Don't allow the FAB past the left hand side of the parent
            newX = Math.min(parentWidth - viewWidth - layoutParams.rightMargin, newX); // Don't allow the FAB past the right hand side of the parent

            float newY = motionEvent.getRawY() + dY;
            newY = Math.max(layoutParams.topMargin, newY); // Don't allow the FAB past the top of the parent
            newY = Math.min(parentHeight - viewHeight - layoutParams.bottomMargin, newY); // Don't allow the FAB past the bottom of the parent

            view.animate()
                    .x(newX)
                    .y(newY)
                    .setDuration(0)
                    .start();

            return true; // Consumed

        }
        else if (action == MotionEvent.ACTION_UP) {

            float upRawX = motionEvent.getRawX();
            float upRawY = motionEvent.getRawY();

            float upDX = upRawX - downRawX;
            float upDY = upRawY - downRawY;

            if (Math.abs(upDX) < CLICK_DRAG_TOLERANCE && Math.abs(upDY) < CLICK_DRAG_TOLERANCE) { // A click
                if (customClickListener != null) {
                    customClickListener.onClick(view);
                }
                return false;
                //return performClick();
            }
            else { // A drag
                return true; // Consumed
            }

        }
        else {
            return super.onTouchEvent(motionEvent);
        }

    }

    public void setCustomClickListener(CustomClickListener customClickListener) {
        this.customClickListener = customClickListener;
    }

    public interface CustomClickListener {
        void onClick(View view);
    }
}

这就是我在 activity

上使用 class 的方式
   MovableFloatingActionButton fl = new MovableFloatingActionButton(this);
    fl.setCustomClickListener(new MovableFloatingActionButton.CustomClickListener() {
        @Override
        public void onClick(View view) {
            Log.e("FAB", "Float Clicked");
        }
    });

执行点击在这种情况下不起作用,因为您已经创建了自定义点击侦听器,所以每当您使用 performClick() 时,都会执行 setOnClickListener 的回调。但是你正在使用 CustomClickListener.

因此,作为解决方案,您必须创建其他方法来执行 CustomClickListener 的点击。

在您的 class MovableFloatingActionButton 中添加此方法:

public void customClick() {
    if (customClickListener != null) {
        customClickListener.onClick(this);
    }
}

然后使用此方法执行代码。

要考虑的另一件事是,您有以下代码。

MovableFloatingActionButton fl = new MovableFloatingActionButton(this);

如果您正在动态使用此浮动按钮并将其动态添加到您的视图中,则无需进行任何更改。

但是如果您在 xml 文件中使用并在 Java 文件中访问,那么您必须像这样使用:

MovableFloatingActionButton fl= findViewById(R.id.mov);

以后您可以使用 fl.customClick() 来执行您的自定义点击侦听器。