3

在這裏將一個浮動動作按鈕,我具有由其中所有都包含在一個TABLayout多個片段共享如下所示如何通過圖尋呼機

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
xmlns:app="http://schemas.android.com/apk/res-auto" 
android:orientation="vertical" 
xmlns:android="http://schemas.android.com/apk/res/android"> 

    <android.support.v7.widget.Toolbar 
    android:id="@+id/toolbar" 
    android:layout_width="match_parent" 
    android:layout_height="?attr/actionBarSize" 
    android:background="#006699" 
    android:titleTextColor="#FFFFFF" 
    app:popupTheme="@style/AppTheme.PopupOverlay" /> 

    <android.support.design.widget.TabLayout 
    android:id="@+id/tab_layout" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/toolbar" 
    android:background="#006699" 
    android:elevation="6dp" 
    android:minHeight="?attr/actionBarSize" 
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"/> 

<android.support.v4.view.ViewPager 
    android:id="@+id/pager" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:layout_below="@id/tab_layout"/> 

<android.support.design.widget.FloatingActionButton 
android:id="@+id/order_icon" 
android:layout_width="59dp" 
android:layout_height="59dp" 
android:visibility="visible" 
android:layout_gravity="right|bottom" 
app:layout_anchor="@id/pager" 
app:layout_anchorGravity="bottom|right|end" 
android:src="@drawable/order_icon" 
android:layout_alignBottom="@+id/pager" 
android:layout_alignParentRight="true" 
android:layout_alignParentEnd="true" /> 

</RelativeLayout> 

的FAB由所有共享的圖尋呼機碎片。我想在FAB上添加一個onLongClicklistener,通過它我可以在佈局上拖動按鈕(包括viewPager)。 我已經使用onTouchListener和MotionEvent對象完成了上述任務。 但是,我無法使用拖放API(釋放拖動的按鈕後,該按鈕消失)。

下面是代碼:

orderIcon = (FloatingActionButton)findViewById(R.id.order_icon); 
    orderIcon.setTag(IMAGEVIEW_TAG); 
    // Sets a long click listener for the ImageView 
    orderIcon.setOnLongClickListener(new View.OnLongClickListener() { 
    @Override 
    public boolean onLongClick(View v) { 
     ClipData.Item item = new ClipData.Item((CharSequence) v 
       .getTag()); 
     String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN }; 
     ClipData clipData = ClipData.newPlainText("", ""); 
     View.DragShadowBuilder myShadow = new View.DragShadowBuilder(view);      

     // Starts the drag 
     v.startDrag(clipData,myShadow,null,0); 
     return true; 
     } 
     }); 
     orderIcon.setOnDragListener(new View.OnDragListener() { 
     @SuppressLint("NewApi") 
     @Override 
     public boolean onDrag(View v, DragEvent event) { 
     switch (event.getAction()) { 
      case DragEvent.ACTION_DRAG_STARTED: 
       owner = (ViewGroup) v.getParent(); 
       owner.removeView(v); 

       Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED"); 
       break; 
      case DragEvent.ACTION_DRAG_ENTERED: 
       Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED"); 
       float x = (int) event.getX(); 
       float y = (int) event.getY(); 
       break; 
      case DragEvent.ACTION_DRAG_EXITED: 
        Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED"); 
        x = (float) event.getX(); 
        y = (float) event.getY(); 
        v.setX(x); 
        v.setY(y); 
        owner.addView(v); 
        v.setVisibility(View.VISIBLE); 
        break; 
      case DragEvent.ACTION_DRAG_LOCATION: 
       Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION"); 

       break; 
      case DragEvent.ACTION_DRAG_ENDED: 
       Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENDED"); 


       break; 
      case DragEvent.ACTION_DROP: 
       Log.d(msg, "ACTION_DROP event"); 
       break; 
      default: 
       break; 
      } 
     return true; 
     } 
    }); 

任何響應將高度讚賞。先謝謝你。

回答

9

試試這個,

1)聲明變量。

float dX; 
    float dY; 
    int lastAction; 

2)獲取Java類的觀點,

fab = (FloatingActionButton) findViewById(R.id.floatingActionButton); 

3)然後寫你的看法下面的代碼,

fab.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View view, MotionEvent event) { 
       switch (event.getActionMasked()) { 
        case MotionEvent.ACTION_DOWN: 
         dX = view.getX() - event.getRawX(); 
         dY = view.getY() - event.getRawY(); 
         lastAction = MotionEvent.ACTION_DOWN; 
         break; 
        case MotionEvent.ACTION_MOVE: 
         view.setY(event.getRawY() + dY); 
         view.setX(event.getRawX() + dX); 
         lastAction = MotionEvent.ACTION_MOVE; 
         break; 
        case MotionEvent.ACTION_UP: 
         if (lastAction == MotionEvent.ACTION_DOWN) 
          Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show(); 
         break;  
        default: 
         return false; 
       } 
       return true; 
      } 
     }); 
+0

這個工程,我不知道爲什麼它不是接受的答案,謝謝你 –

+1

它實際上工作,但變量「視圖」有錯誤。將其更改爲「v」將使其工作。 –

+0

@ChungXa謝謝,兄弟。 –

0

試試這個,我編輯的代碼以上回答

fab.setOnTouchListener(new View.OnTouchListener() { 
     float dX; 
     float dY; 
     float startX; 
     float startY; 
     int lastAction; 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      switch (event.getActionMasked()) { 
       case MotionEvent.ACTION_DOWN: 
        dX = v.getX() - event.getRawX(); 
        dY = v.getY() - event.getRawY(); 
        startX = event.getRawX(); 
        startY = event.getRawY(); 
        lastAction = MotionEvent.ACTION_DOWN; 
        break; 
       case MotionEvent.ACTION_MOVE: 
        v.setY(event.getRawY() + dY); 
        v.setX(event.getRawX() + dX); 
        lastAction = MotionEvent.ACTION_MOVE; 
        break; 
       case MotionEvent.ACTION_UP: 
        if (Math.abs(startX - event.getRawX()) < 10 && Math.abs(startY - event.getRawY()) < 10){ 
         Toast.makeText(v.getContext(), "Clicked!", Toast.LENGTH_SHORT).show(); 

        } 
        break; 
       default: 
        return false; 
      } 
      return true; 


     } 
    });