ورود

View Full Version : مشکل در drag and drop



zahra.mf
چهارشنبه 29 بهمن 1393, 10:43 صبح
سلام

من یه drag and drop نوشتم که فقط دوتا scop دارم که یه imageview باید فقط توی اون دوتا محدوده قرار بگیره و هر جایی غیر از این دوتا محدوده بذاریم باید برگرده سر جای اولش

کدی که نوشتم یه کلاس دازم که با case نوشتم که برای دوتا محدوده کار میکنه ولی توی خارج از اون محدوده اصلا نمیره (رفت و برگشت نداره و سرجاش میمونه) یه جاهایی توی لبه های محدوده هم گاهی محو میشه!
package com.test.dragdrop1;





import android.app.Activity;
import android.os.Bundle;


import android.view.DragEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;


public class Dragdrop1Activity extends Activity implements OnTouchListener,OnDragListener{
//private static final String LOGCAT = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.imageView1).setOnTouchListener(t his);
findViewById(R.id.linearLayout1).setOnDragListener (this);
findViewById(R.id.linearLayout2).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;
}
}


@Override
public boolean onDrag(View layoutview, DragEvent dragevent) {
// TODO Auto-generated method stub
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:
View view1 = (View) dragevent.getLocalState();
/* ViewGroup owner1 = (ViewGroup) view1.getParent();
owner1.removeView(view1);
LinearLayout container1 = (LinearLayout) layoutview;
container1.addView(view1);*/
view1.setVisibility(View.VISIBLE);
break;
}
return true;
}



}


این هم کد xml

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


<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#FF8989"
android:layout_marginTop="50dp"
android:layout_marginLeft="50dp"
android:orientation="vertical" >


<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />


</LinearLayout>








<LinearLayout
android:id="@+id/linearLayout2"
android:layout_width="100dp"
android:layout_height="50dp"
android:background="#FFCC00"
android:layout_marginTop="100dp"
android:layout_marginLeft="10dp"
android:orientation="vertical" >
</LinearLayout>


</LinearLayout>