在Android中不会调用浮动按钮setOnClickListener

时间:2016-01-21 08:37:28

标签: java android onclicklistener ontouchlistener floating-action-button

我想整合可移动的浮动按钮,如“WhatsApp”应用程序打开时,它会显示带有动作的远足浮动按钮。还有工作点击事件。

我使用默认的OnTouch事件,但是我无法在浮动按钮上应用Click事件。如何同时应用点击并触摸事件

提前致谢!

home.xml

 <RelativeLayout
        android:id="@+id/rl_parent_floating"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible">

        <RelativeLayout xmlns:fab="http://schemas.android.com/apk/res-auto"
            android:id="@+id/rl_floating_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentBottom="true"
            android:layout_margin="10dp">

            <TextView
                android:id="@+id/txtCartCount"
                android:layout_width="30dp"
                android:layout_height="30dp"
                android:layout_alignRight="@+id/fab"
                android:layout_alignTop="@+id/fab"
                android:background="@drawable/cart_gold_circle"
                android:gravity="center"
                android:singleLine="true"
                android:text="2"
                android:textColor="@android:color/white"
                android:textSize="8sp" />

            <com.melnykov.fab.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="3dp"
                android:src="@drawable/ic_floating_cart"
                fab:fab_colorNormal="@color/header_color_red"
                fab:fab_colorPressed="@color/colorPrimaryDark"
                fab:fab_colorRipple="@color/gold_color" />
        </RelativeLayout>
    </RelativeLayout>

Home.java

     mFloatingActionButton = (FloatingActionButton) findViewById(R.id.fab);
            mRrootLayout = (ViewGroup) findViewById(R.id.rl_floating_button);
            rl_floating_button = (RelativeLayout) findViewById(R.id.rl_floating_button);
            rl_parent_floating = (RelativeLayout) findViewById(R.id.rl_parent_floating);
mFloatingActionButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent(mActivity, Cart.class);
                startActivity(mIntent);
            }
        });

            mFloatingActionButton.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View view, MotionEvent event) {

                    final int X = (int) event.getRawX();
                    final int Y = (int) event.getRawY();

                    switch (event.getAction() & MotionEvent.ACTION_MASK) {
                        case MotionEvent.ACTION_DOWN:
                            RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
                            view.getLayoutParams();

                            _xDelta = X - lParams.leftMargin;
                            _yDelta = Y - lParams.topMargin;
                            break;
                        case MotionEvent.ACTION_UP:
                            break;
                        case MotionEvent.ACTION_POINTER_DOWN:
                            break;
                        case MotionEvent.ACTION_POINTER_UP:
                            break;
                        case MotionEvent.ACTION_MOVE:
                            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view
                                    .getLayoutParams();
                            layoutParams.leftMargin = X - _xDelta;
                            layoutParams.topMargin = Y - _yDelta;
                            layoutParams.rightMargin = 0;
                            layoutParams.bottomMargin = 0;
                            view.setLayoutParams(layoutParams);
                            break;
                    }
                    mRrootLayout.invalidate();
                    return true;
                }
            });

1 个答案:

答案 0 :(得分:2)

使用SimpleOnGestureListener & GestureDetector类来点击屏幕,而不是使用界面setOnclickListner,因为它可能不适用于ontouchListner

private class SingleTapDetector extends SimpleOnGestureListener{
    @Override
    public boolean onSingleTapUp(MotionEvent e) {

        return true;
    }

}

然后在onCreate方法中初始化GestureDetector,就像这样

gestureDetector=new GestureDetector(this,new SingleTapDetector());

现在,如果在浮动按钮上检测到单击,请检查浮动气泡的OnTouchListener

代码

mFloatingActionButton.setOnTouchListener(new View.OnTouchListener() {
 @Override
 public boolean onTouch(View v, MotionEvent event) {
 if (gestureDetector.onTouchEvent(event)) {
  // code for single tap or onclick

  // pass your intent here
  } else {

  switch(event.getAction()){
  //  code for move and drag

  // handle your MotionEvents here
  }
     return true;
            }
        });

希望这会对你有所帮助。