How can I listen the move events after the LongPress is caled in my GestureDetector?
When the user LongClick he starts the selection mode, and can drag a square into the screen. But I noticed that the onScroll is not called after LongPress is consumed.
Tried to do fight this for a while, and for now the solution is:
Disable the longpress using setIsLongpressEnabled(isLongpressEnabled) on your gestureDetector
Here is my OnTouch method of my View:
public boolean onTouchEvent(MotionEvent event) {
if (mGestureDetector.onTouchEvent(event)== true)
{
//Fling or other gesture detected (not logpress because it is disabled)
}
else
{
//Manually handle the event.
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
//Remember the time and press position
Log.e("test","Action down");
}
if (event.getAction() == MotionEvent.ACTION_MOVE)
{
//Check if user is actually longpressing, not slow-moving
// if current position differs much then press positon then discard whole thing
// If position change is minimal then after 0.5s that is a longpress. You can now process your other gestures
Log.e("test","Action move");
}
if (event.getAction() == MotionEvent.ACTION_UP)
{
//Get the time and position and check what that was :)
Log.e("test","Action down");
}
}
return true;
}
My device returned ACTION_MOVE whenever I hold finger on the screen. If your doesnt, just check the state of some pressed flag after 0.5s using a timer or thread.
Hope that helps!
I have done this task by using the following concepts:
Suppose I have the Image View and on long press on it, image inside this image View would be drag-able and placed inside the another view(e.g. Relative Layout)
Set MyClickListner on Image View's setOnLongClickListener() method.
private final class MyClickListener implements View.OnLongClickListener {
// called when the item is long-clicked
#Override
public boolean onLongClick(View view) {
// TODO Auto-generated method stub
// create it from the object's tag
ClipData.Item item = new ClipData.Item((CharSequence)view.getTag());
String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag( data, //data to be dragged
shadowBuilder, //drag shadow
view, //local data about the drag and drop operation
0 //no needed flags
);
// view.setVisibility(View.INVISIBLE);
return true;
}
}
Then set MyDragListner on Relative Layout(e.g. bigImageRelativeLayoutVw.setOnDragListener(new MyDragListener());)
class MyDragListener implements View.OnDragListener {
#Override
public boolean onDrag(View v, DragEvent event) {
int X=(int)event.getX();
int Y=(int)event.getY();
int touchX = 0,touchY=0;
// Handles each of the expected events
switch (event.getAction()) {
//signal for the start of a drag and drop operation.
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
//the drag point has entered the bounding box of the View
case DragEvent.ACTION_DRAG_ENTERED:
break;
//the user has moved the drag shadow outside the bounding box of the View
case DragEvent.ACTION_DRAG_EXITED:
// v.setBackground(normalShape); //change the shape of the view back to normal
break;
//drag shadow has been released,the drag point is within the bounding box of the View
case DragEvent.ACTION_DROP:
// if the view is the bottomlinear, we accept the drag item
if(v == bigImageRelativeLayoutVw) {
View view = (View) event.getLocalState();
touchX=X-viewCoords[0]-20;
touchY=Y-viewCoords[1]-20;
View view1=new View(getActivity());
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(30,30);
layoutParams.leftMargin =touchX;
layoutParams.topMargin = touchY;
view1.setBackgroundResource(R.drawable.heavy_damage);
view1.setLayoutParams(layoutParams);
RelativeLayout containView = (RelativeLayout) v;
containView.addView(view1);
view.setVisibility(View.VISIBLE);
} else {
View view = (View) event.getLocalState();
view.setVisibility(View.VISIBLE);
break;
}
break;
//the drag and drop operation has concluded.
case DragEvent.ACTION_DRAG_ENDED:
// v.setBackground(normalShape); //go back to normal shape
default:
break;
}
return true;
}
}
Related
I have two view pager, one is nested in the other. To get the correct behaviour (swiping the inner view pager without changing the outer) I had to override the inner view pagers onTouchListener and put all my onTouch/onClick logic into it (got the idea from here).
Works all fine, but since I don't have a onClickListener anymore I lost my selector effect. When I put android:clickable="true" on the layout element I get my selector effect, but the view pagers behaviour is wrong again.
Is there any way to achieve the selector effect out of the onTouchListener?
innerPager.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
Log.d(DEBUG_LOG, "Single tap.");
return true;
} else if(event.getAction() == MotionEvent.ACTION_DOWN && v instanceof ViewGroup) {
((ViewGroup) v).requestDisallowInterceptTouchEvent(true);
}
return false;
}
});
This solved it for me. Just added the following code to my OnTouchListener and replaced the card view with my inner view pagers current item:
// Since the host view actually supports clicks, you can return false
// from your touch listener and continue to receive events.
myTextView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent e) {
// Convert to card view coordinates. Assumes the host view is
// a direct child and the card view is not scrollable.
float x = e.getX() + v.getLeft();
float y = e.getY() + v.getTop();
// Simulate motion on the card view.
myCardView.drawableHotspotChanged(x, y);
// Simulate pressed state on the card view.
switch (e.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
myCardView.setPressed(true);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
myCardView.setPressed(false);
break;
}
// Pass all events through to the host view.
return false;
}
});
I have a project which i'm required to develop Android game that display a 5*5 table and and image for each player which each one of them can move the image in place inside the 5*5 table.
ex:
Note : I need to know the exact coordinates (or anything relative) so i can save the position in array and move the image to that new place (eg : re-draw it on the new position).
Any ideas ?
Thanks in advance.
Just use a RelativeLayout with 5x5 ImageViews (set to not visible).
And on the images that the user can move to the place, use Drag and Drop.
In onDrag you set the visibility of the image to Gone.
For all ImageView (5x5 Table), you set the onDragListener.
After that, in the overwritten method OnDrop, you can receive the view that is dropped and can determine which drawable to show.
edit:
Oh well, in this case I would use GridView as said in the comments. And make usage of drag and drop. You don't need to attach a DragListener to every image then. You can simply let the GridView listen to the drop events and determine by the x and y where to drop.
Little example (just as a hint)
gameStoneView.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
DragShadowBuilder shadowBuilder = new DragShadowBuilder(gameStoneView);
gameStoneView.startDrag(null, shadowBuilder, gameStoneView, 0);
if (dragListener != null) {
dragListener.onDragStart();
}
break;
default:
LOG.v("Motion event reorderIcon: DEFAULT - not action down");
}
return true;
}
});
gridView.setOnDragListener(new OnDragListener() {
#Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_LOCATION:
currentY = event.getY();
currentX = event.getX();
break;
case DragEvent.ACTION_DROP:
final Point position = getPositionOfItem(currentX, currentY);
dropItemAt(position);
break;
case DragEvent.ACTION_DRAG_EXITED:
//BORDER DRAGEVENT: ACTION_DRAG_EXITED
viewDraggedOutSide = true;
break;
case DragEvent.ACTION_DRAG_ENDED:
//BORDER DRAGEVENT: ACTION_DRAG_ENDED
if (viewDraggedOutSideList) {
reinsertDraggedItem();
update();
}
viewDraggedOutSideList = false;
return true;
default:
return true;
}
}
});
Im using view.startDrag() to start dragging operation. While dragging, events are received in OnDragListener. Also im using DragShadowBuilder to show the last image of View B while dragging.
Is there a way to stop or cancel or abort dragging from some external events or operation? for example, there are 2 view, View A and View B, i'm dragging View B over View A. While dragging, due to some external event or operation, i want to cancel dragging operations or cancel OnDragListener (without removing my finger from the view B).
Code Snippet for DragShadowBuilder:
DragShadowBuilder shadowBuilder = new DragShadowBuilder(view) {
#Override
public void onDrawShadow(Canvas canvas) {
canvas.drawBitmap(b, 0, 0, new Paint());
super.onDrawShadow(canvas);
}
};
boolean dragSuccess = false;
dragSuccess = view.startDrag(null, shadowBuilder, view, 0);
Code Snippet for OnDragListener:
private final class ViewDragListener implements OnDragListener {
#Override
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
log("ACTION_DRAG_STARTED");
break;
case DragEvent.ACTION_DRAG_ENTERED:
log("ACTION_DRAG_ENTERED");
break;
case DragEvent.ACTION_DRAG_EXITED:
log("ACTION_DRAG_EXITED");
case DragEvent.ACTION_DROP:
log("ACTION_DROP");
mX = (int) event.getX();
mY = (int)event.getY();
break;
case DragEvent.ACTION_DRAG_ENDED:
log("ACTION_DRAG_ENDED");
//Doing some cleanup operations.
break;
case DragEvent.ACTION_DRAG_LOCATION:
log("ACTION_DRAG_LOCATION");
break;
default:
break;
}
return true;
}
}
I couldn't find any trick for this. I tried sending a motion event of ACTION_UP and it did nothing. As long as the user's finger is held down, there's no interrupting the event hierarchy, it seems. I found nothing to help with a google search.
I just set a global and waited until the drag ended, then proceeded.
I am making an game application where i need to use onDraglistener for drag and drop here what I am doing on a class which implements onDraglistener.
#Override
public boolean onDrag(View v, DragEvent event) {
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
Log.v("here","drag started");
//no action necessary
break;
case DragEvent.ACTION_DRAG_ENTERED:
//no action necessary
Log.v("here","drag enteres");
break;
case DragEvent.ACTION_DRAG_LOCATION:
int mCurX = (int) event.getX();
int mCurY = (int) event.getY();
Log.v("Cur(X, Y) : " ,"here ::" + mCurX + ", " + mCurY );
break;
case DragEvent.ACTION_DRAG_EXITED:
//no action necessary
Log.v("here","drag exits");
break;
case DragEvent.ACTION_DROP:
.................do what ever when dropped...
}
here what I am doing on touchlistener of a view to be dragged and dropped:
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
/*
* Drag details: we only need default behavior
* - clip data could be set to pass data as part of drag
* - shadow can be tailored
*/
view.setTag("option"+index);
ClipData data = ClipData.newPlainText("tag", "option"+index);
shadowBuilder = new View.DragShadowBuilder(view);
//start dragging the item touched
view.startDrag(data, shadowBuilder, view, 0);
offsetX = (int)view.getX();//(int)motionEvent.getX();
offsetY = (int)view.getY();//motionEvent.getY();
Log.v("here","it is ::" + (int)motionEvent.getX() + " , "+(int)motionEvent.getY());
return false;
}
Now it builds the shadow builder which gest dragged to anywhere you drag finger.
How could I animate the shadow builder or the view going back to its position when not dropped to target?
I tried public boolean onInterceptTouchEvent(MotionEvent event) and moving to parent view and taking x,y position and animating it to go back to its original position but it gets action cancelled.
Is there any way to get x,y coordinates to a place the view dropped other then the target. Then I would be able to animate it between the positions.
You need to handle the drop:
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_ENTERED:
//break here was removed in purpose so you can see the shadow and the element is still at its initial place.
case DragEvent.ACTION_DROP:
View view = (View) event.getLocalState();
if (prioritiesList.contains(v)) {
ViewGroup viewgroup = (ViewGroup) view.getParent();
viewgroup.removeView(view);
LinearLayout containView = (LinearLayout) v;
containView.addView(view);
view.setVisibility(View.VISIBLE);
break;
}
I have a custom ListView in which I have set an OnTouchListener for each list item. When the user touches the item, I use TransitionDrawable to change the background color of the item during MotionEvent.ACTION_DOWN and bring it back to normal during MotionEvent.ACTION_UP.
Now when I scroll this list, the item which is touched at the start of the scroll action changes background color. I want to avoid this behavior. So while scrolling I want to disable any change in background color. Is there a way to do this? Please see the code below:
public class ListItemOnTouchListener implements OnTouchListener {
#Override
public boolean onTouch(View v, MotionEvent event) {
Context mContext = getActivity().getApplicationContext();
Resources res = mContext.getResources();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
TransitionDrawable transitionDown = (TransitionDrawable) res
.getDrawable(R.drawable.row_background_transition_down);
v.setBackgroundDrawable(transitionDown);
transitionDown.startTransition(350);
Log.d(TAG, "ACTION_DOWN");
return true;
case MotionEvent.ACTION_UP:
TransitionDrawable transitionUp = (TransitionDrawable) res
.getDrawable(R.drawable.row_background_transition_up);
v.setBackgroundDrawable(transitionUp);
transitionUp.startTransition(1000);
// Get list view
ListView listView = getListView();
int position = listView.getPositionForView((LinearLayout) v.getParent());
listView.performItemClick(v, position, 0);
Log.d(TAG, "ACTION_UP");
return true;
case MotionEvent.ACTION_MOVE:
Log.d(TAG, "ACTION_MOVE");
break;
case MotionEvent.ACTION_CANCEL:
Log.d(TAG, "ACTION_CANCEL");
v.setBackgroundResource(R.drawable.row_background_normal);
break;
}
return false;
}
}
When the user starts scrolling you should get an ACTION_CANCEL. Catch this and change the color back.
Instead of implementing on touch, u can implement onListItemClick(will work same as on touch) and then on that method u can write the code to change the color of the list item touched/clicked.