2013-10-29 118 views
0

我想在拖動佈局的同時動態更改佈局的背景。如何在Android中拖動佈局時動態更改佈局背景?

我用下面的代碼來實現觸摸監聽器。

RelativeLayout rlImages.setOnDragListener(new MyDragListener()); 
    RelativeLayout half_left.setOnTouchListener(new MyTouchListener()); 


private final class MyTouchListener implements OnTouchListener { 
    Drawable normalShape = getResources().getDrawable(R.drawable.pinkcircle); 

    @SuppressWarnings("deprecation") 
    public boolean onTouch(View view, MotionEvent motionEvent) { 
     view.setBackgroundResource(R.drawable.pinkcircle); 

     if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
      ClipData data = ClipData.newPlainText("", ""); 
      DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
      view.startDrag(data, shadowBuilder, view, 0); 
      view.setVisibility(View.INVISIBLE); 
      return true; 
     } else { 

      return false; 
     } 
    } 
} 

我dragListener類:

class MyDragListener implements OnDragListener { 
    Drawable enterShape = getResources().getDrawable(R.drawable.halfcircle_left); 
    Drawable normalShape = getResources().getDrawable(R.drawable.pinkcircle); 

    @SuppressWarnings("deprecation") 
    @Override 
    public boolean onDrag(View v, DragEvent event) { 
     int dragAction = event.getAction(); 
     final View dragView = (View) event.getLocalState(); 

     dragView.setBackgroundDrawable(normalShape); 
     switch (event.getAction()) { 
      case DragEvent.ACTION_DRAG_STARTED: 
       // dragView.setBackgroundDrawable(normalShape); 
       dragView.setBackgroundDrawable(normalShape); 
       // Do nothing 
       break; 
      case DragEvent.ACTION_DRAG_ENTERED: 
       dragView.setBackgroundDrawable(normalShape); 
       // v.setBackgroundDrawable(enterShape); 
       break; 
      case DragEvent.ACTION_DRAG_EXITED: 
       // v.setBackgroundDrawable(normalShape); 
       break; 
      case DragEvent.ACTION_DROP: 
       // Dropped, reassign View to ViewGroup 
       dragView.setBackgroundDrawable(normalShape); 
       break; 
      case DragEvent.ACTION_DRAG_ENDED: 
      default: 
       break; 
     } 
     return true; 
    } 
} 
+0

什麼問題? –

+0

我想在拖動它的同時更改佈局的背景,但無法在任何DragEvents中做到這一點 – user1891910

+0

您可以顯示代碼將「MyTouchListener」和「MyDragListener」設置爲View的代碼? –

回答

0

在您需要在開始拖動之前,因爲一旦你撥打view.startDrag(),視圖被複制到改變背景的onTouchListener。然後,當拖動結束並且再次變爲可見時,您需要將背景重置爲開始時的背景(或任何其他您想要的背景)。

下面顯示瞭如何工作的:

public class MainActivity extends Activity implements OnTouchListener, OnDragListener { 
    private static final String LOGCAT = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // doing view stuff... 
    } 

    public boolean onTouch(View view, MotionEvent motionEvent) { 
     if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
      DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
      // set background of dragged view to red 
      view.setBackgroundResource(android.R.color.holo_red_dark); 
      view.startDrag(null, shadowBuilder, view, 0); 
      view.setVisibility(View.INVISIBLE); 
      return true; 
     } else { 
      return false; 
     } 
    } 

    public boolean onDrag(View layoutview, DragEvent dragevent) { 
     int action = dragevent.getAction(); 
     View dragView = (View) dragevent.getLocalState(); 
     switch (action) { 
      case DragEvent.ACTION_DRAG_STARTED: 
       Log.d(LOGCAT, "Drag event started"); 
       break; 
      case DragEvent.ACTION_DRAG_ENTERED: 
       Log.d(LOGCAT, "Drag event entered into " + layoutview.toString()); 
       break; 
      case DragEvent.ACTION_DRAG_EXITED: 
       Log.d(LOGCAT, "Drag event exited from " + layoutview.toString()); 
       break; 
      case DragEvent.ACTION_DROP: 
       Log.d(LOGCAT, "Dropped"); 
       // reset background of dragged view 
       dragView.setBackground(null); 
       ViewGroup owner = (ViewGroup) dragView.getParent(); 
       owner.removeView(dragView); 
       LinearLayout container = (LinearLayout) layoutview; 
       container.addView(dragView); 
       dragView.setVisibility(View.VISIBLE); 
       break; 
      case DragEvent.ACTION_DRAG_ENDED: 
       Log.d(LOGCAT, "Drag ended"); 
       break; 
      default: 
       break; 
     } 
     return true; 
    } 
}