I want to simulate the iOS 3D Touch. I have a recyclerview, where i register a OnLongClickListener to every item. When a longClick is detected, a AlertDialog opens.
For checking if the longClick ends, i have implemented the OnTouchlistener.
In short, i implemented it like it was described here: https://stackoverflow.com/a/10746549/4907047
It works like a charm, as long as i don't move the finger after longClick. If i move the finger, the listview under the dialog is still moving with my finger. Besides that the dialog will not close after stop touching the screen.
I think i have to cancel the events of the underlying listview. Does anyone know, how to handle this?
You should consider implementing a contextMenu since it can do what you want. Read this.
Related
I am trying to drag a view from one place to another and also have a single click/tap to do a completely different action without dragging the view.
Right now what I have a long click listener to start my drag like this
view.setOnLongClickListener(..
....
view.startDrag(...
And for my single click I use a click listener like this
view.setOnClickListener(...
// My completely different action
How can I have it so that I don't have to use long click to start drag, but can start drag by allowing user to just simply move his/her finger from my view to the target.
Use view.setOnTouchListener(..
the function will be called as soon as you touch the item. In this function, you should call your view.startDrag(... function and you are good to go.
Have fun and Take Care.
use a delay thread inside on touch listner
because on click works when you remove your finger dragging cant be done after removing finger
I have a view with a long click listener. This triggers the show of a fullscreen dialog. I need to capture touch events on said dialog (particularly ACTION_UP) but since the ACTION_DOWN was captured by the first view and was never released I don't get any events on the dialog.
Besides a listener on the view that tells the dialog when ACTION_UP happens, do you know another way of detecting ACTION_UP in this case from the dialog perspective?
You could try returning a false on the OnLongClickListener so the event won't be consumed by the first view, and that might let your second dialog see the Click action.
I have an app that inside onItemLongClick opens a dialog. I want the user to have a better experience so I want the possibility to choose and item in the dialog without having to raise his finger but I can't understand how to make a onTouch listener that will get the MOVE MotionEvent. can anyone tell me how to do this?
Thanks,
Benny.
Any onTouchListener will get move events, unless the moves are stolen from it. Given you're in an onItemLongClick, are you part of a ListView? If so, you'll get ACTION_CANCEL whenever the listview decides to take the touch events away from its children to scroll with, and you won't receive further moves (or even an up).
I have implemented custom view with some children. The view can be scrolled using standard drag gesture. Also every child can be clicked. The problem is, that when I start dragging the view, one of children gets 'down' event and it changes its state to 'pressed' for a second. I would prefer standard listview behavior - the child goes into pressed state when the user keeps pressing this child with his/her finger for like 50ms. It would reduce blinking caused by misread press event.
I know, that I need at least 2 events to detect if the user is tapping or dragging the view. For now I'm using TimerTask to shedule 'down' event. When I get 'move' event before my 'down' event is executed, I know that the user is dragging and I can cancel the sheduled event.
I know it's quite hacky. I also tried gesturedetector to detect drag and tap events, but it needs some additional work to properly implement changing view state from pressed to default when the user moves finger and starts to drag the view.
My question is - how this is implemented in android listview? I tried to copy their solution from listview implementation, but it's so huge I can't handle it. Simply I don't see the code responsible for handling such situation.
I managed to understand a gesture detection logic in ListView and, in general, in android views. I wrote my own gesture detector, which is somewhat better than the original one. It reports more gestures (multiple taps, dragging) and has some configurables (timeouts, move epsilon). You can find it open-sourced here: Better Gesture Detector on code.google
The library uses Handler class and postDelayed()/removeCallbacks() method combination to detect, handle and cancel motion events and gestures. It's quite simple and one should be able to get the idea by just reading the code.
This repository also contains a simple demo. Please note that this code is provided 'as is', contains some useless comments, logs and should be cleaned up a bit.
In case my title wasn't clear enough, I'll explain it in detail:
Say we have a screen filled with multiple buttons (10+), and we press down on one, activating onTouch/onClick. If we now move the finger, without lifting it, I want it to activate any other button it slides over. In this particular case, I want sound to be played when you slide over a virtual piano.
I know about the onTouchListener-solution where you register every ACTION_MOVE and find some boundaries that activates new events, but that's far from optimal if you have multiple buttons and want to allow smooth sliding without delay.
I also read this thread which suggested that we combine the touchListener of the View with a gesturelistener from the activity, but once again, this does not feel at all optimal for my situation.
I have not tried combining touchlistener with gesturelistener yet, but I will go ahead and do so if someone tells me they have no other way of doing this.
In my opinion, the proper way of doing this is to forget about buttons, and create a custom view which draws the entire keyboard. In this view you can handle touch events the way you like. You do not even need the gesture detector, just analyze the action and coordinates of motion events, it's very easy.
I don't understand what you mean about ACTION_MOVE and delays. To minimize delay, react on ACTION_DOWN, and then on ACTION_MOVE if it hovers other keys while in down state. It can't be any faster than that. With buttons there is an important delay because the onClick() event is triggered when the user lift the finger, on ACTION_UP.
Buttons are just not meant to work as you describe. The idea is that if a user taps on a button and then move his finger away at the same time it does not trigger onClick events on other views around. This prevents bogus clicks.
I actually took the "easy" way out and used buttons with an onTouch-method using ACTION_DOWN and ACTION_MOVE with coordinate calculations that combined with event.getX() and event.getY() allows me to detect which button is currently hovered. It's lag free with 13 buttons.