1

我要當打開時,它會顯示加息的浮動按鈕與運動結合移動浮動按鈕同樣喜歡「WhatsApp的」應用。並且還在工作點擊事件。浮動按鈕setOnClickListener不被稱爲Android的

我使用默認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; 
       } 
      }); 
+0

你得到什麼錯誤 – Abhishek

+0

觸摸事件正在工作,我可以拖放浮動按鈕,但我想改變活動時,點擊它。它不工作。 – Krishna

+0

是你的應用程序崩潰還是什麼? – Abhishek

回答

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

檢測單抽頭希望這會幫助你。

+0

非常感謝這是工作。當我使用這段代碼 然後它移動只有右下方不是整個屏幕。 – Krishna

+0

@Krishnavrinsoft歡迎好友 –

+0

讓我清楚你想讓浮泡在屏幕上的任何位置移動嗎? –