Showing lots of views in a ViewFlipper - android

I have a ViewFlipper that shows a lot of views. The flipper "moves" only in one direction, meaning once a view got hidden, it will never be shown again. Here's what I call to move to the next view:
mViewFlipper.addView(myNewView);
mViewFlipper.showNext();
Although this works fine I'm not sure if it's actually clean and memory friendly. Would I need to remove any of the previous views? Or can I just keep pushing new views in?
If old views had to be removed - where would I do that? With some sort of callback, that gets fired once a view becomes replaced/hidden?
EDIT: here's the complete method:
private void OnBtnNextPressed()
{
mViewFlipper.addView(new TestView());
mViewFlipper.showNext();
}

Related

GridView eats my onTouch events and starts "scrolling"

I am creating an android app in which I need to track the pointer on a couple of elements that are inside a GridView. The problem is that if the pointer moves a little bit up or down, the GridView enters some kind of scrolling mode (even if it fits easily inside the screen), so the events do not reach the child views anymore. Is there anything I can do to prevent this behaviour and keep tracking the pointer inside those children?
So to clarify: the pointer tracking works neatly, until the vertical difference between starting and current position becomes to large. At that moment, no touch event reaches my child elements anymore, and the gridView starts scrolling if it does not fit inside the screen.
I found a solution at another StackOverflow post. Simply put, the solution is to create a class that extends the GridView and overrides the onInterceptTouchEvent method, which must return false when scrolling (or any other form of interception) has to be prohibited.

What does setScrollingCacheEnabled(); do?

I initially used this method: myListView.setScrollingCacheEnabled(false); to enhance my list view's scrolling performance. However, I feel like it is causing a small bug in my list view. And I am not sure what it actually does.
1 out of every 20 times I navigate to the activity or fragment that contains the list view. The list view is not visible, then when I touch the screen, it immediately appears.
What is the difference between myListView.setScrollingCacheEnabled(false); and myListView.setScrollingCacheEnabled(true); ?

ListView with footer attached, smoothScrollToPosition not functioning correctly

I'm working with a rather large layout that includes a ListView and a LinearLayout beneath it as a footer. I first tried to wrap this in a ScrollView to make the whole layout scrollable, but ran into problems due to both the ListView and the ScrollView being incompatible with each other since they both have scrollable features. So, a workaround was to include the LinearLayout as a footer to the ListView.
Now, in the LinearLayout, I have buttons at various places that the user can click to return to the top of the page. The behaviour I am getting from this is odd, to say the least.
If I have not scrolled down too far, the buttons function normally. However, if I scroll down a bit too far, then clicking the button (even the same buttons that previously worked) will result in the layout scrolling up to roughly half of the way up the listview instead of the top.
Here is the method that does the scrolling, it's rather simple:
public void backToTop(View view) {
lv = (ListView)findViewById(R.id.listview);
lv.smoothScrollToPosition(0);
}
This method is triggered when any of the buttons are clicked.
I have also tried to use the "scrollTo(0, 0)" function, but that failed to do anything.
Any help would be appreciated!
**edit: After testing some more, it appears as though the point where scrolling does not seem to function properly anymore is when the listview is no longer visible on the page. As soon as I scroll past it, the buttons begin to function incorrectly.
Edit 2: SOLVED. My solution: I changed the line
lv.smoothScrollToPosition(0);
to:
lv.setSelected(0);
This seems to give the correct behaviour for all my buttons at any position that the user has placed their screen. Using setSelected does not seem to have the side-effect that I was expecting of automatically triggering the click-event. Hooray!
My solution: I changed the second line of my backToTop method:
lv.smoothScrollToPosition(0);
to:
lv.setSelected(0);
This seems to give the correct behaviour for all my buttons at any position that the user has placed their screen. Using setSelected does not seem to have the side-effect that I was expecting of automatically triggering the click-event. Hooray!

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.

Android custom view is not drawn properly

I have a custom widget based on the Android Gallery view. It is slightly customized to show the elements from the left of the view without a gap and pass required events to my listener.
On the very first view layout everything works perfect. However when I pause and resume the activity the view freezes. It is visible, I can see OnTouch() event handlers working, view positions adjusted, but the picture is not changing. The OnDraw() method for the Gallery widget is not called at all!
I have another widget in this layout (a sliding drawer) and as soon as I barely move the drawer everything starts working back again as it should.
I am completely out of ideas how to fix this. Tried different ways to force the OnDraw() method but it is just not working.
Any ideas?
Thanks
Make sure you override onDraw(android.graphics.Canvas) and not OnDraw() (both the Canvas argument and the lower case initial 'o').
If it's not that, then please post some code to look at it.

Categories

Resources