I have a grid-view and i set an adapter to that that contain array-list of image-views. I want to drag a single item from grid-view and dropping it into another layout. The problem I'm facing is how would i set a touch listener to a single item of grid-view so that i can drop it in target. If i am setting ontouchlistener in grid-view, it is picking whole grid view rather than a single item. Please help me............... I've tried this code:
gridview.setOnItemClickListener(new MyOnItemClickListener());
gridview.setOnDragListener(new MyOnDragListener());
private final class MyTouchListener implements OnTouchListener {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
return true;
} else {
return false;
}
}
final class MyOnDragListener implements OnDragListener{
Drawable enterShape = getResources().getDrawable(R.drawable.shape_droptarget);
Drawable historyDrawer;
@Override
public boolean onDrag(View v, DragEvent event) {
LocalLogger.LOGGER.info("Under on drag listener");
switch(event.getAction()){
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_ENTERED:
historyDrawer=v.getBackground();
v.setBackground(enterShape);
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackground(historyDrawer);
break;
case DragEvent.ACTION_DROP:
break;
case DragEvent.ACTION_DRAG_ENDED:
break;
}
return false;
}
}
使用OnItemLongClickListener查找长按的子项。
Add it in grid onclick like this -
gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View view, int position, long arg3) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, view, 0);
return true;
}
});
Since you are only using ACTION_DOWN
on your touch listener, you can use onItemClickListener or onItemLongClickListener for your dragging.
grid.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View v,
int pos, long id) {
ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
v);
// start dragging the item touched
v.startDrag(data, shadowBuilder, v, 0);
return false;
}
});
I resolved it next way:
gridView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
float currentXPosition = motionEvent.getX();
float currentYPosition = motionEvent.getY();
int position = gridView.pointToPosition((int) currentXPosition, (int) currentYPosition);
View v = gridView.getChildAt(position);
ClipData.Item item = new ClipData.Item("" + position); // here you send any data you want, I sent position
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_HTML};
ClipData data = new ClipData("" + position, mimeTypes, item);
View.DragShadowBuilder shadow = new View.DragShadowBuilder(v);
view.startDrag(data, shadow, null, 0);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.