在這裏,我把我的代碼,當我將我的觀點(ImageView的),它是無形的,請幫我的Android在拖放,降不工作
public class MainActivity extends ActionBarActivity {
ImageView ivfirst;
private android.widget.RelativeLayout.LayoutParams layoutParams;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivfirst = (ImageView) findViewById(R.id.imgView);
ivfirst.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
ClipData.Item item = new ClipData.Item((CharSequence) v
.getTag());
String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
ClipData dragData = new ClipData(v.getTag().toString(),
mimeTypes, item);
View.DragShadowBuilder myShadow = new View.DragShadowBuilder(
ivfirst);
v.startDrag(dragData, myShadow, null, 0);
return true;
}
});
ivfirst.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
switch(event.getAction())
{
case DragEvent.ACTION_DRAG_STARTED:
layoutParams = (RelativeLayout.LayoutParams)v.getLayoutParams();
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
int x_cord = (int) event.getX();
int y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_EXITED :
x_cord = (int) event.getX();
y_cord = (int) event.getY();
layoutParams.leftMargin = x_cord;
layoutParams.topMargin = y_cord;
v.setLayoutParams(layoutParams);
break;
case DragEvent.ACTION_DRAG_LOCATION :
x_cord = (int) event.getX();
y_cord = (int) event.getY();
break;
case DragEvent.ACTION_DRAG_ENDED :
// Do nothing
break;
case DragEvent.ACTION_DROP:
// Do nothing
break;
default: break;
}
return true;
}
});
ivfirst.setOnTouchListener(new View.OnTouchListener() {
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
ivfirst);
ivfirst.startDrag(data, shadowBuilder, ivfirst, 0);
ivfirst.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
});
}
}
當我運行該程序的正常工作,但降不行, 請指點我,我錯了
Thankx艾米我得到了重點 –