One touch event puts all child views into pressed state - android

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.

Related

Leanback.DetailsFragment not scrolling as expected

I have a details fragment that is using a DetailsOverviewRow and FullWidthDetailsOverviewRowPresenter. When the page first loads the action buttons are selected. When I press down once, focus leaves the buttons and nothing else happens. When I press down a second time focus moves to a ListRow that is further down the page. I'm trying to figure out why the overview, or body as it's called in the presenter, section doesn't focus.
Should there not be a second 'state' between the two screenshots where the overview/body is readable?
Possibly of note. The DetailsFragment isn't declared in an activities xml, I'm changing fragments manually using FragmentTransactions. Also, the Fragment is instantiated using a static create method (source below).
Thanks in advance.
Activity xml
DetailsFragment java
I haven't tried this example in particular, but from my time spent with Leanback support library, I learned that even though a lot of helpful stuff is indeed provided, a lot is not :D
So I would try these things:
1) Make sure that something in the area you want visible is focusable. (Clickable elements should be focusable by default, but better check too) What I mean is that on the screenshot, there is just text, no buttons or editable content in that area. So when you press down, there is nothing to focus. What happens if you make the body TextView focusable?
(Yes, one would expect that the support library would take care of that, but that might not be the case.)
2) Find out what actually gets focused when you press down, since as you said, the focus leaves the buttons - but where does it go? (How to find out which view is focused?) You might have a "direction problem" somewhere. That is - the focus travels based on the view hierarchy tree, not based on what we see on the screen. In some cases, it is possible to skip some elements or get stuck somewhere by moving focus through an unexpected part of the view tree, that makes sense for the algorithm, but isn't logical from the human perspective.
The details presenter focus works this way :
1) First focus is given to Action buttons. Right/left nav press shifts the focus right/left between action buttons.
2) Down nav press from actions row shifts the focus from actions row to details row (individual details items itself are not focusable), this is achieved by shifting the Thumbnail anchoring position to further south.
3) Down nav press from details row shifts the focus from details row to related row.
So the details row gains focus by changing the anchoring position of Thumbnail image. Is your thumbnail image changing its anchoring position when pressing down from actions row?
Hate to answer my own question. This was due to my failure to RTFM. I was creating the fragment UI after a network request completes. For the FullWidthDetailsPresenter to work properly it and the ClassPresenterSelector() should be initialized in the fragments onCreate() method.

Change default navigation of gridview - android

I'm having some problems to navigate on a gridView using the keyboard (with up, down, left and right keys).
I'm new in Android and maybe this question has a simple answer. I looked for a solution and anything could help me.
The problem is that I would like to change the way of navigation among items of a grid view. I would like to do it due to two reasons.
The first reason is related to the fact that some itens have clickable subitens (and consequently the entire item is not clickable). When the focus is changed from a item i to the item i+1, for example, the item i+1 must be checked if has subitens. If yes, the focus is changed to one of the subitens depending on direction (ie. if left to right, the first subitem on the left is focused).
The second reason is that in my application, the up arrow should works in the same way as the left arrow and the down arrow in the same way of the right arrow. So, I just need see if the keyCode of a onKey event is UP or DOWN and convert it to LEFT and RIGHT, respectively.
I thought that would be a simple task: override the onKeyDown method and treat these specificities, but I observed that my implementation of onKeyDown was invoked just in two cases: (1) when other keys are pressed (with exception of the aforementioned arrows and (2) when the grid view lost the focus. For example, if the item C1 is the current item selected and the user click to the left or down, the onKey is not invoked. Otherwise, if the right or up are typed, my method is invoked.
Is there any way to cancel this internal mechanism of grid view? I tried different ways, as follows:
(1) Implementation of OnKeyListener directly on the gridview and on the item (on the adapter) as well.
(2) Implementation of different callbacks to see which one would be called before this internal mechanism (like OnFocusChangeListener, OnKeyListener, OnItemSelectedListener). I tried both for the grid view and each item on the adapter.
(3) Instantiation of the gridview with the selector setted to a transparent image. When the desired item is reached on my keyEvent, I added a visible selector. It worked on the first event but when the visible selector is setted it does not back to the invisible selector.
I tried to be clear and I hope to anyone of you could help =)
Thank you so much
How are you?
You could try use onKeyUp instead of onKeyDown, see the answer below:
https://stackoverflow.com/a/20171618/1408986
And Android documentation:
http://developer.android.com/training/keyboard-input/commands.html
Hope this help.. bye!
I solved this problem by implementing both listeners: onKey and OnItemSelected. The focus was everytime running of the grid used and jusing those listeners solved my problem. Thanks a lot.

Android Animation reset on BasicAdapter sort

This has been bugging me for a few days now. I have a FrameLayout and one of the elements within the layout moves to reveal a menu. I can paste the code if requested, it's a bit long since it's my play code and I haven't used any styles. I digress...
When the user presses a particular button it calls a startAnimation on a custom LinearLayout which is layered on top of another stock LinearLayout. Pushing the button again will put the custom LinearLayout back to it's original location, thus hiding the menu.
I had to create a custom LinearLayout to override onAnimationEnd so the layout would stop and stay at the final animated position (I found this based on some other questions asked here on StackOverflow).
The problem arises when the user actually presses one of the visible menu items. One of the items, for example, sorts or reverse sorts the displayed list. It appears that right after I call notifyDataSetChanged on my BasicAdapter the screen redraws itself and my menu is hidden. I have no code that closes the menu, it's almost like the entire Activity is re-created or reset when the list is told to redraw.
I should also point out that I'm extending an Activity not a ListActivity. I'm targeting API 10 (Gingerbread, 2.3) and up.
If any one has any pointers, I would greatly appreciate it. I've been wracking my brain on this for days now and it's driving me crazy. Please let me know if I can provide any more info.
EDIT:
Here's the SO post about overriding the onAnimationEnd method.
Android TranslateAnimation resets after animation
Do you record which item's menu has been opened yourself? If not, then it means that you let the UI system do the remembering for you, which would mean that this information would be lost or rendered useless (since you have changed the item ordering), so all the items reverts to their initial states.
The solution is to associate the menu opened/closed state with each data in the list, then when the adapter's getView method is called, you can rebuild the correct UI state.

Gallery overlapping items click

I have a Gallery based View with a negative spacing between elements, so that one zoomed out behind the previous, i.e. 'carousel'. Zooming and carousel effect is achieved by Camera transformation.
The problem arises when I try to click right side of foreground item and what I really get is click on item behind it (always to right of it)
Let me enumerate what I've already tried:
setChildrenDrawingOrderEnabled(true);
z coordinate of background item is obviously greater then foreground
one
getChildDrawingOrder was overridden as many ways as possible (and I
found out that it doesn't play any role at all - touch events don't
depend on visibility order of views)
Seems like next(right) child has greater index and therefore a priority of handling UI events, and my z-coordinate/drawing order manipulations don't change anything.
So question: how to make foreground item responsible for all click events on it?

ViewFlipper caching issue

The views are not cached in a ViewFlipper. Is there a way wherein we can get an image of the view and show it to user so that he sees the Ui as we see on Home scrren(when we swipe the previous view also moves along and when we lift our finger, only then the next view is shown completely.)
What I want to do is that when the user starts moving his finegr on screen, the view should also move along(create an image of view).
I am not getting to do this, as when we swipe the present view goes and next view comes, we do not get both visible when we r moving our finger on screen.
Please if anyone gets what I am trying to do, do help me.
Thanks,
Farha
It's tricky to get scroll and swipe tracking working on Android, while using ViewAnimator or its subclasses.
They allow you to set in and out animations and start them at a given moment, but they work with discrete, either-this-or-the-other-view animations. They are actually using FrameLayout and after in or out animation is executed, other views' visibility is set to View.GONE to hide them from showing up under/over your current View.
The Launcher and the Gallery application are actually doing the functionality you want, by using a different approach.
They track the user touch input (onTouchEvent()), on MotionEvent.ACTION_MOVE they perform animations manually and on MotionEvent.ACTION_UP snap to the appropriate view, just like in the iPhone.
Unfortunately, this approach is actually more complicated than it looks like.
With the manual handling, you have to ensure that you are taking care of everything related to the touch input. This includes a lot of flag-raising, value-checking, event-delegating, etc.
If you want to get better acquainted with this, take a look at these classes from Gallery3D or Launcher's source code.
One other way to get nice horizontal scrolling is to use HorizontalScrollView.
You have to figure out a way to recycle your views, like you would with a ListView and you have to add the snap-to-view logic, but if you have to take care of a small number of views it could be the easiest approach.
Hope that helps.

Categories

Resources