我有三种布局:顶部、左侧和右侧。在顶部布局上,我有 EditText 图标,当我将它拖到左侧布局时,将在左侧布局中创建一个 EditText。
在左侧布局或右侧布局中使用 EditText 放置,我可以将其从左到右布局(或从右到左,不能从左/右到顶部移动)
我的问题:
1.当我通过在左侧布局中拖动创建一个EditText,然后通过在左侧布局中拖动创建一个EditText,效果很好,我可以看到所有拖放日志动作。但是,如果我再次在左侧布局中创建另一个 EditText,则不会收到 ACTION_DRAG_STARTED 和 ACTION_DRAG_ENDED。
http://i1057.photobucket.com/albums/t394/tdtrinhsiho/ScreenShot2013-07-28at60954PM_zps14a2d784.png
2.当我创建一个EditText太接近布局中已经创建的EditText时,没有收到ACTION_DROP
http://i1057.photobucket.com/albums/t394/tdtrinhsiho/ScreenShot2013-07-28at61336PM_zps98ab85f1.png
我的代码:XML:
<LinearLayout
android:id="@+id/top"
android:layout_width="fill_parent"
android:layout_height="100dip"
android:background="@color/Azure">
<ImageView
android:id="@+id/sticker"
android:padding="10dip"
android:layout_width="50dip"
android:layout_height="50dip"
android:background="@drawable/note"
>
</ImageView>
</LinearLayout>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="horizontal">
<AbsoluteLayout
android:id="@+id/left"
android:layout_width="500dip"
android:layout_height="fill_parent"
android:background="@color/OldLace">
</AbsoluteLayout>
<AbsoluteLayout
android:id="@+id/right"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="@color/BurlyWood">
</AbsoluteLayout>
</LinearLayout>
</LinearLayout>
活动:包com.tranductrinh.mydraganddrop;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.content.ClipData;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.DragEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewGroup;
import android.widget.AbsoluteLayout;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
public class Main extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
findViewById(R.id.sticker).setOnTouchListener(new MyTouchListener());
findViewById(R.id.left).setOnDragListener(new MyDragListener());
findViewById(R.id.right).setOnDragListener(new MyDragListener());
}
private class MyTouchListener implements View.OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
//view.setVisibility(View.INVISIBLE);
return true;
} else
return false;
}
}
private class MyLongTouchListener implements View.OnLongClickListener {
public boolean onLongClick(View view) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
//view.setVisibility(View.INVISIBLE);
return true;
}
}
class MyDragListener implements View.OnDragListener {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Log.i("INFORMATION ", "START");
return true;
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackgroundColor(getResources().getColor(R.color.YellowGreen));
Log.i("INFORMATION ", "ENTER");
return true;
case DragEvent.ACTION_DRAG_EXITED:
if (v == findViewById(R.id.left))
v.setBackgroundColor(getResources().getColor(R.color.OldLace));
else
if (v == findViewById(R.id.right))
v.setBackgroundColor(getResources().getColor(R.color.BurlyWood));
Log.i("INFORMATION ", "EXIT");
return true;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
Log.i("INFORMATION ", "DROP");
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
if (owner.getId() != R.id.top) {
// remove Sticker in from layout
owner.removeView(view);
// add Sticker to new layout
AbsoluteLayout container = (AbsoluteLayout) v;
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(200, 200, (int)event.getX()-100, (int)event.getY()-100);
container.addView(view, params);
view.setVisibility(View.VISIBLE);
view.setFocusable(false);
// set onLongClickListener
view.setOnLongClickListener(new MyLongTouchListener());
Log.i("INFORMATION ", "Move operation");
}
else if (owner.getId() == R.id.top) {
//view.setVisibility(View.VISIBLE);
// new EditText object by code
EditText editText = new EditText(Main.this);
editText.setBackgroundDrawable(getResources().getDrawable(R.drawable.note));
// add Sticker to Container
AbsoluteLayout container = (AbsoluteLayout) v;
AbsoluteLayout.LayoutParams params = new AbsoluteLayout.LayoutParams(200, 200, (int)event.getX()-100, (int)event.getY()-100);
container.addView(editText, params);
// set focus on EditText
editText.setFocusable(false);
// set onLongClickListener
editText.setOnLongClickListener(new MyLongTouchListener());
Log.i("INFORMATION ", "Copy operation");
}
// set view parent to normal
if (v == findViewById(R.id.left)) {
v.setBackgroundColor(getResources().getColor(R.color.OldLace));
}
else
if (v == findViewById(R.id.right)) {
v.setBackgroundColor(getResources().getColor(R.color.BurlyWood));
}
return true;
case DragEvent.ACTION_DRAG_ENDED:
Log.i("INFORMATION ", "Drag Drop Result : " + event.getResult());
if (event.getResult() == false) {
Toast toast = Toast.makeText(Main.this, "Invalid operation drag and drop", Toast.LENGTH_SHORT);
toast.show();
// when drop action not handler
((View) event.getLocalState()).setVisibility(View.VISIBLE);
}
Log.i("INFORMATION ", "END *******************");
return true;
default:
break;
}
return false;
}
}
}