2016-05-31 25 views
0

Ich testen Drag & Drop-Funktion in meiner App Ich habe ein relatives Layout, das zwei Textansichten enthält Diese beiden Textansichten können von ihrer ursprünglichen Position an beliebiger Stelle gezogen werden im LayoutDrag & Drop-Ansicht nicht sichtbar nach Drag & Drop abgeschlossen

wenn ich ziehen sie die Textansichten sie in der Lage sind überall auf dem Bildschirm gezogen werden, aber wenn ich die Ansicht, lassen sie es fallen überall sind sie nicht mehr sichtbar

ich habe OnTouchListener() und OnDragListener verwendet() auf den textviews

mir helfen, dieses Problem zu geeigneten Koordinaten auf ACTION_DROP Ereignis

public class MainActivity extends Activity 
{ 
private TextView tv1, tv2; 
LayoutParams lParams; 
private static final String msg = "DRAG EVENT TESTING"; 


@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    // TODO: Implement this method 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    tv1 = (TextView) findViewById(R.id.bigTV); 
    tv2 = (TextView) findViewById(R.id.smallTv); 

    tv1.setOnTouchListener(new MyTouchListener()); 
    tv2.setOnTouchListener(new MyTouchListener()); 

    tv1.setOnDragListener(new MyDragListener()); 
    tv2.setOnDragListener(new MyDragListener()); 

} 

private class MyTouchListener implements OnTouchListener 
{ 

    @Override 
    public boolean onTouch(View v, MotionEvent event) 
    { 


     ClipData dragData = ClipData.newPlainText("",""); 

     View.DragShadowBuilder shdwbldr = new View.DragShadowBuilder(v); 
     v.startDrag(dragData, shdwbldr, v, 0); 
     v.setVisibility(View.INVISIBLE); 
     return true; 
    } 


} 

private class MyDragListener implements OnDragListener 
{ 

    @Override 
    public boolean onDrag(View v, DragEvent event) 
    { 
     switch(event.getAction()){ 
      case DragEvent.ACTION_DRAG_STARTED: 
       lParams = (RelativeLayout.LayoutParams)v.getLayoutParams(); 
       Log.d(msg, "Action is DragEvent.ACTION_DRAG_STARTED"); 


       break; 

      case DragEvent.ACTION_DRAG_ENTERED: 
       Log.d(msg, "Action is DragEvent.ACTION_DRAG_ENTERED"); 
       int x_cord = (int) event.getX(); 
       int y_cord = (int) event.getY(); 
       break; 

      case DragEvent.ACTION_DRAG_EXITED : 
       Log.d(msg, "Action is DragEvent.ACTION_DRAG_EXITED"); 
       x_cord = (int) event.getX(); 
       y_cord = (int) event.getY(); 
       lParams.leftMargin = x_cord; 
       lParams.topMargin = y_cord; 
       v.setLayoutParams(lParams); 
       break; 

      case DragEvent.ACTION_DRAG_LOCATION : 
       Log.d(msg, "Action is DragEvent.ACTION_DRAG_LOCATION"); 
       x_cord = (int) event.getX(); 
       y_cord = (int) event.getY(); 
       break; 

      case DragEvent.ACTION_DROP: 
       Log.d(msg, "ACTION_DROP event"); 


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

       // Do nothing 
       break; 


      default: break; 
     } 
     return true; 
    } 


} 

     } 

mein Layout

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_height="match_parent" 
android:layout_width="match_parent"> 

<TextView 
    android:id="@+id/bigTV" 
    android:layout_height="wrap_content" 
    android:text="Drag Me" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:layout_width="wrap_content"/> 

<TextView 
    android:id="@+id/smallTv" 
    android:layout_height="wrap_content" 
    android:text="Drag me too but slowly!" 
    android:textAppearance="?android:attr/textAppearanceMedium" 
    android:layout_width="wrap_content" 
    android:layout_below="@id/bigTV"/> 

     </RelativeLayout> 

Antwort

0

bewegen Textansichten

meine Tätigkeit zu lösen.