我试图在我的 gridview 上阻止 Android拖放。我为网格适配器中的每个视图设置了一个 dragListener。当网格不够长而不能滚动时,或者只要网格中的第一个视图可见,它就可以滚动。滚动时,它会将动作向下移动一个位置,因为许多视图在顶部不可见,而不是在正确的位置。
GridView 在适配器内部设置 DragListener:
ImageView i = new ImageView;
i.setImageResource(ImageList.get(position));
i.setOnDragListener(new MyDragImageListener(position));
return i;
MyDragListener:
public class MyDragImageListener implements OnDragListener{
int imagePosition;
public MyDragImageListener(int position) {
imagePosition = position;
}
@Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Log.i("DRAG Started", "" + imagePosition);
break;
case DragEvent.ACTION_DRAG_ENTERED:
Log.i("DRAG Entered", "" + imagePosition);
ImageView imageEnter = (ImageView) Grid.getChildAt(imagePosition);
imageEnter.setBackgroundColor(Color.argb(155, 100, 200, 255));
break;
case DragEvent.ACTION_DRAG_EXITED:
Log.i("DRAG Exited", "" + imagePosition);
ImageView imageExit = (ImageView) Grid.getChildAt(imagePosition);
imageExit.setBackgroundColor(Color.argb(0, 0, 0, 0));
break;
case DragEvent.ACTION_DROP:
Log.i("DRAG Dropped", "" + imagePosition);
break;
case DragEvent.ACTION_DRAG_ENDED:
Log.i("DRAG Ended", "" + imagePosition);
default:
break;
}
return true;
}
}