2017-08-02 9 views
0

how to drag image to drop any of this imageview Je voudrais faire glisser et déposer une image Imageview dans l'un des imageviews donnée et correspondre le type. comment puis-je y parvenir sous Android? J'ai fait pour faire glisser un ImageView à un autre ImageView, mais je ne sais pas comment implémenter multi-gouttes ImageView.Glisser et déposer ImageView dans Android

+0

Après avoir traîné ce que vous voulez remplacer une image où il a traîné? –

+0

Oui, je dois remplacer. En fait, le tableau des images va charger un par un, et je dois le faire glisser à des images données .. –

Répondre

0

Démonstration du geste de glisser-déposer en déplaçant un TextView d'un LinearLayout à un autre.

public class MainActivity extends Activity implements OnTouchListener,OnDragListener{ 
     private static final String LOGCAT = null; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 
      findViewById(R.id.textView1).setOnTouchListener(this); 
      findViewById(R.id.pinkLayout).setOnDragListener(this); 
      findViewById(R.id.yellowLayout).setOnDragListener(this); 
     } 
     public boolean onTouch(View view, MotionEvent motionEvent) { 
       if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { 
        DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); 
        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(); 
       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"); 
       View view = (View) dragevent.getLocalState(); 
       ViewGroup owner = (ViewGroup) view.getParent(); 
       owner.removeView(view); 
       LinearLayout container = (LinearLayout) layoutview; 
       container.addView(view); 
       view.setVisibility(View.VISIBLE); 
       break; 
       case DragEvent.ACTION_DRAG_ENDED: 
         Log.d(LOGCAT, "Drag ended"); 
        break; 
       default: 
       break; 
       } 
       return true; 
     } 
    } 

activity_main.xml

<RelativeLayout xmlns:tools="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/center" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <LinearLayout 
     android:id="@+id/pinkLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="210dp" 
     android:background="#FF8989" 
     android:orientation="vertical" > 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/dragtext" /> 
    </LinearLayout> 

    <LinearLayout 
     android:id="@+id/yellowLayout" 
     android:layout_width="fill_parent" 
     android:layout_height="250dp" 
     android:layout_marginTop="210dp" 
     android:background="#FFCC00" 
     android:orientation="vertical" > 
    </LinearLayout> 

</RelativeLayout> 

enter image description here

+0

J'ai fait glisser et déposer avec un ImageView à un autre ImageView, maintenant le problème est, comment faire tomber l'un d'ImageView de donné multi drop options ImageView. –