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.
Related
I just want to allow only scroll left in horizontal recyclerview for example. Please help me on that
Best way is to remove other directions item from adapter, which are getting hidden one by one. So when user scroll back ward it will not show up any thing.
I would not touch the data in your adapter (what does the data have to do with a UI business rule?). If you want, you could notify your Repository or ViewModel what views have been "scrolled" but that's a lot of events going up, and state coming down for everyone involved.
You have the tools at your disposal in the form of a combination of:
A GestureDetector (See (the documentation.)
A TouchListener (See (the documentation.)
Your favorite search engine to put these things together.
or perhaps benefit from the somewhat modular approach of RecyclerView:
Using OnItemTouchListener and a OnScrollListener to analyze the events and ignore the ones you don't want.
You could also create your own extended RecyclerView but that's more painful because you now need to change it all over the place you want this behavior.
In any case, your search engine of choice is also a good starting point. But think of this as:
You will need to intercept the touches and flings gestures (as well as other motion actions like "Dpads" and other Accessibility Events that may trigger a "scroll", evaluate in which direction this is headed, and determine if you still want that event to happen.
So, My original problem is that I have started working on a huge android application which has too many activities and layouts. It is totally undocumented/commented and I have to deliver results in days.
So, I want to match, the elements in layouts ->> to the code, so that I can understand the code faster.
If somehow I can define a global touch listener that can intercept every touch action and Log the associated view and it's parent layout, and after that hand the control over to the associated click listener.
Am I on the right path? is there any best practice in such scenarios?
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.
Doubtless this is caused by my decidedly unorthodox layout - I have buttons in a LinearLayout in an Activity which is placed by an ActivityGroup into a Gallery. The ActivityGroup is also the adapter implementation and the over-all effect is full-screen sliding, snapping panels.
This is working (a treat, actually) except that a touch event on the parent layout puts all the buttons into the pressed state (and any release removes the state). A touch on an individual button is only delivered to that button.
The buttons are not receiving any events, they're only changing state.
Have I done something obviously wrong? Is this a known bug and is there a work-around?
Any insights would be very much appreciated.
As obscure as this problem is its solution may be of use to someone else, so I'll answer my own question.
As mentioned, I'm (mis)using the Gallery to provide a slidey-panel à la iPhone. I do this by returning the top level window of an Activity when the Gallery asks its Adapter implementation for a view.
Typical use of a Gallery would result in small Views to which it's desirable for the press event to be applied - it's more like a button then it is a panel. Our use means that there are many buttons in a single view and we don't want the press event to ever be applied globally.
So the work-around was very easy. I extended Gallery and deliberately broke pointToPosition(int x, int y), returning INVALID_POSITION every time. The Gallery still does everything else expected of it but skips trying to apply the touch down event to any elements but itself (to prepare itself to scroll or fling).
I hope this is of value to someone.
I need to implement HorizontalScrollView which is scrolled to predefined positions (similar to Home behaviour). It works with slow gestures, but does not work with neither flings nor arrow key press.
I hooked to View.onScrollChanged() and it is called when scrolling happened, but I can't determine when scrolling animation ends.
In theory there should be a way to say that fling movement is over. Is there such API?
I did not find a way to track scroll animation, but it's possible to move scroll logic from HorizontalScrollView class to derived by you, where you can control every aspect of scrolling
I'm replying to a post a year old but this might be useful for future readers.
For implementing HOME-Like scroll, use Gallery instead of HorizontalScrollView.
Using 'Gallery' you need not worry about stopping scrolling at predefined positions and it gives you a handler for Fling-gesture too.
Add your code in onFling() or onScroll() as per your requirement.
More info # http://developer.android.com/reference/android/widget/Gallery.html