我正在准备应用程序,其中包含两个listview,其中包含来自SD卡的图像,另一个listview包含带有默认图标的联系人,现在我想将第一个listview中的图像拖放到另一个联系人列表视图中。
答案 0 :(得分:0)
private DragListener mDragListener =
new DragListener() {
int backgroundColor =#f000;// add color of ur choice
int defaultBackgroundColor;
public void onDrag(int x, int y, ListView listView) {
// TODO Auto-generated method stub
}
public void onStartDrag(View itemView) {
itemView.setVisibility(View.INVISIBLE);
defaultBackgroundColor = itemView.getDrawingCacheBackgroundColor();
itemView.setBackgroundColor(backgroundColor);
ImageView iv = (ImageView)itemView.findViewById(R.id.imageView1);
if (iv != null) iv.setVisibility(View.INVISIBLE);
}
public void onStopDrag(View itemView) {
itemView.setVisibility(View.VISIBLE);
itemView.setBackgroundColor(defaultBackgroundColor);
ImageView iv = (ImageView)itemView.findViewById(R.id.imageView1);
if (iv != null) iv.setVisibility(View.VISIBLE);
}
};
创建一个接口并在你的ListActivity类中调用它的对象。同样明智的你可以删除和删除
public boolean onTouchEvent(MotionEvent ev) {
// TODO Auto-generated method stub
final int action = ev.getAction();
final int x = (int) ev.getX();
final int y = (int) ev.getY();
if (action == MotionEvent.ACTION_DOWN && x < this.getWidth()/4) {
mDragMode = true;
}
if (!mDragMode)
return super.onTouchEvent(ev);
switch (action) {
case MotionEvent.ACTION_DOWN:
mStartPosition = pointToPosition(x,y);
if (mStartPosition != INVALID_POSITION) {
int mItemPosition = mStartPosition - getFirstVisiblePosition();
mDragPointOffset = y - getChildAt(mItemPosition).getTop();
mDragPointOffset -= ((int)ev.getRawY()) - y;
startDrag(mItemPosition,y);
drag(0,y);// replace 0 with x if desired
}
break;
case MotionEvent.ACTION_MOVE:
drag(0,y);// replace 0 with x if desired
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
default:
mDragMode = false;
mEndPosition = pointToPosition(x,y);
stopDrag(mStartPosition - getFirstVisiblePosition());
if (mDropListener != null && mStartPosition != INVALID_POSITION && mEndPosition != INVALID_POSITION)
mDropListener.onDrop(mStartPosition, mEndPosition);
break;
}
return true;
}
从列表视图中获取子ID,然后使用触摸事件,然后从列表视图中编写Drag方法。