我希望用户能够在 a 中交换项目(aImageView
中的一个RelativeLayout
)GridView
,但我也希望他们能够在GridView
将图像拖动到相应的顶部或底部时向上或向下滚动GridView
。
如果我在 backing 中实现拖动侦听ArrayAdapter
器,则无法进行滚动。如果我GridView
按如下所示实现它,我将无法获得我放置的数组项的位置。
有没有办法做到这一点?请注意我遇到问题的评论。谢谢!
我有一个GridView
实现OnDragListener
,但我不知道如何获得我想要交换的位置。
这是长按监听器:
gv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> grv, View v,
int position, long row) {
mVibrator.vibrate(VIBRATE_DURATION);
ClipData cd = ClipData.newPlainText("position", String.valueOf(position));
v.startDrag(cd, new View.DragShadowBuilder(v), v, 0);
return true;
}
});
这是我的拖动监听器:
gv.setOnDragListener(new OnDragListener() {
Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
Drawable normalShape = getResources().getDrawable(R.drawable.shape);
private static final int THRESHHOLD = 200;
@Override
public boolean onDrag(View v, DragEvent event) {
int height = v.getMeasuredHeight();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
v.setBackground(enterShape);
break;
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackground(enterShape);
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackground(normalShape);
break;
case DragEvent.ACTION_DRAG_LOCATION:
float y = event.getY();
if (height - y < THRESHHOLD) {
gv.smoothScrollBy(200, 300);
Log.i(TAG, "Scrolling DOWN");
} else if (height - y > height - THRESHHOLD) {
gv.smoothScrollBy(-200, 300);
Log.i(TAG, "Scrolling UP");
}
break;
case DragEvent.ACTION_DROP:
//Gridview position to swap with.
// HOW DO I GET THIS?
int i = position;
int j = Integer.parseInt(event.getClipData().getItemAt(0).getText().toString());
Collections.swap(mUriList, i, j);
Log.i(TAG, "Swapped " + i+ " with " + j);
break;
case DragEvent.ACTION_DRAG_ENDED:
v.setBackground(null);
mAdapter.notifyDataSetChanged();
default:
break;
}
return true;
}
});