Lazily populating Android ViewFlipper children - android

I have a ViewFlipper in which I display a large number of images, loaded from the sqlite database. If I initialise each ViewFlipper child on startup, there's an unreasonable delay whilst all the images are loaded from the db. Instead I want to populate the views one at a time, 'lazily', just before the user transitions from one child to the next. I need to populate the next view before setDisplayedChild(next) is called as I have animations applied to the transition. However ViewFlipper doesn't seem to have a getChildAt(int i) method which would enable me to do this - you only seem to be able to getDisplayedChild(). Any ideas how I might achieve this ?

Fixed it by keeping a list of views outside the view flipper - doh

Related

how to prevent creating same layout everytime while using fragments and viewpager?

I have a viewpager that holds 3 different fragments and all fragments has tablelayout which loads dynamicaly. (while saying dynamically, I never try to reach any dataset, I take dataset from bundle already, what I do is just iterate dataset and during this process creating tablerows, textviews and inserting them into tablelayout by one by, so the issue is only creating view as dynamicaly)
My problem is while swiping between fragments it takes a while. I noticed it does that filling operation I mentioned above in oncreateview everytime.
What I want to do is it should create tablelayout just once at the begining and return it everytime as view if fragment would be selected.
When I try that fragments view appears only once and after swiping nothing appears.
I couldnt sleep and I am writing these via my cellphone, so I cant share what I did at the moment but I hope youll get what I want to do and maybe direct me in a good way.
You can use:
myViewPager.setOffscreenPageLimit(3);
which will keep a fragment around once it has been loaded.
Also, you really should use a ListView instead of building out TableRows. This will scroll much smoother and load faster.

Showing animation of Views created from a custom adapter

I'm trying to show an animation with all Views that I've created from an adapter. When I scroll down, it shows the animation correctly, but when I scroll up, I see these Views recreate themselves and show the animation again. Then, when I scroll down, it happens again.
My assumption is that the mechanism of creating a View from an adapter is to load the View into memory; just the group of Views which are on screen right now (but above and below views are not loaded into memory). These will be loaded again when I scroll to these views, right?
Is there any way to fix this problem?
PS: Sorry for my English, I hope you understand my problem.
My assumption is that the mechanism of creating a View from an adapter
is to load the View into memory; just the group of Views which are on
screen right now (but above and below views are not loaded into
memory)
That's somewhat correct: a ListView will not try to visualize any data that isn't (at least partially) visible. It also 'recycles' views, meaning that any view that isn't currently used to present data to the user and is of the same 'type' as the next data item, may get reused.
Hence you shouldn't rely on persisting data with or make any assumptions about the existence of particular views. In stead, use something that's separate from the views; e.g. the dataset you're visualizing.
Quite often, you'll supply a list of POJOs to a BaseAdapter or ArrayAdapter. You could simply add a boolean to the POJO indicating whether it should animate or not, and change that whenever the animation for that particular item finishes. Alternatively, you could keep track of these values in a separate collection (which is probably the more straightforward approach if you're dealing with a Cursor as data source rather than POJOs).

How to get all children (visible and invisible) from a ListView?

My problem is similar to ListView getChildAt returning null for visible children, but despite searching I cannot find a solution.
I have a ListView with a Scroll. The ListView has 10 items, 7 of which are visible and 3 are hidden by scroll. I also have an external method (out of adapter) that must get all of the children from this ListView (e.g. using getChildAt()).
I need all 10 of the items, but the last 3 are null objects. I've tried code like the following:
getListView().smoothScrollToPosition();
But this doesn't work.
I think that I don't need to post the rest of my code, as the description says everything?
As you have already seen you can't get all the child row views from a ListView simply because a ListView holds only the views for the visible rows(plus some recycled rows but you can't reach those). The correct way to do what you want is to store whatever data in the adapter's data and retrieve it from there.
But the ListView doesn't keep the current values from RadioGroup in
running time.
I've seen that you have some problems with this so I've adapted some old code to build a basic example, code that you can find here.
I don't think so you need to add scroll view for a listView. Scroll automatically works on ListView. Try your application without adding scroll view and I'm sure it'll work as you needed.
The reason those children are null it's because they really do not exist and they will never exist, if only 7 children are on the screen at one time, the system will only create 7 and re-use by passing the convertView back to the adapter getView() method.
If you want to grab information regarding your whole dataset you should search on the dataset itself, instead of the views on the screen. E.g. if it's an ArrayAdapter, loop the array; if it's a CursorAdapter, loop the cursor; etc.
The non-visible children of a listView don't actually exist. When they become visible, one of the redundant views is recycled or a new view is generated. So you can't actually access all the views. Why do you want to? Whatever changes you want to make should be made to the data that populates the views rather than the views themselves.
There are a few point that you need to take care of:
1. List view provides inbuilt scroll functionality, So don't use Scroll view. It will only mess up things.
2. List view doesn't contain ALL the children. When you scroll it, it creates only visible items on run time.
3. If you want to get all the children altogether, Better keep an ArrayList of the child objects that your list has. You can add or remove children to this ArrayList as per requirement.

Force ListView/BaseAdapter to inflate all needed views at the begining in android

I have a rather expensive Layout that i inflate to a listview's childrens.
If the listview shows 5 childrens eatch time getView() will inflate only 5 (maybe one-two more) of my expensive Layouts. And then reuse it.
This process might take some time but it's ok.(It's at the same time i download data from the Internet so doesn't ruin the UX).
The problem is that if i scroll , at the beggining of the scrolling one convertView is null so the listview needs to inflate one more time. This is happening only the first time i scroll.
Is there any way to avoid that? Notice that i don't resize any of the children or the Listview. Is there a way to inflate that extra view at the beggining so i avoid the Scrolling Lag?
UPDATE
Also to be specific:
My listview shows 3and a half rows .. Meaning 4.The adapter inflates 5. What the adapter whats to inflate another one? Maybe there is a more elegant solution to that
Why not just create a cache of views when your activity is started, or in your adapter's constructor? getView() could return views from this cache if any are available, or create its own as it normally would if no cached views are available.
(I would not recommend creating more than n+1 views, though, where n is the number of views you expect to have on the screen at any one time. And I strongly recommend against ever trying to recycle old views back into the cache!)

Should I use multiple ListViews?

I have a RelativeLayout with different elements. I was planning to have two ListViews on it, but I have noticed there are some problems with scrolling. Since each ListView only shows a maximum of 5 rows should I try to make some kind of custom adapter to merge those ListViews? Or is it better to replace the ListView with a LinearLayout/RelativeLayout and add the rows as I get them manually? (like the first answer in here: android listview display all available items without scroll with static header ).
Which should be the proper way on doing this? or is there another way? Also, each row will have an OnClickListener.
There's two solutions if you'd like to keep your list... list-y, without having to prerender all the row Views like the above solution suggests (which can be slow to render, eats RAM and doesn't scale nicely to more than a screen or two of Views, but is a fine quick solution for smaller lists, though I'd just use a bunch of Views in a LinearLayout in a ScrollView rather than a ListView in that case).
Write a custom ListAdapter, overriding getItemViewType, getViewTypeCount and GetView to inflate the proper kind of view and recycle appropriately for your two types of views. You'll also either need to override getItem to contain custom logic for figuring out which set of source data to look in and to map the data accordingly, or mush the data down into one list of Objects (if you're using an arrayadapter) and cast in the getView method (probably a bit slower than handling it in the getItem without casting).
Just use cwac-merge, a view-and-adapter wrapping adapter. You can put two ListAdapters into a MergeAdapter and set that as your single ListView's adapter.
I had problems with scrolling. I never figured out how to have the ListView share vertical space with a different View, and have a single scrollbar for them both.
I worked around it by having everything that needs to scroll on the layout a row in the ListView.
Adding views as rows to a LinearLayout may have problems scaling up, but I think you'll be OK if you only have 10 rows in total. On 1st gen Android devices it'll probably start to get sluggish around 20 items (depends on Layout complexity obviously). ListView scales up by only inflating views as they come on screen.
So in answer to your question either of the two alternatives you suggest will be OK, but the LinearLayout option will be the easiest to code.

Categories

Resources