In my application I have an activity which contains a viewflipper with 3 listviews inside. I have implemented the segmentedbutton widget to change between the listviews inside the viewflipper. Doing this way, I keep three listviews loaded in memory and display them according to the button selected.
Does android allow the developer to change the list adapter in runtime? I was thiking about removing the viewflipper and keep only one listview and when the user select one button, i only change the adapter from the listview.
Is this feseable? Wouldnt it be consuming more memory and cpu instead of having the three listviews inside the viewflipper?
Any answer that can help me is very appreciated.
Many thanks
T
You can certainly change the list adapter on a list at runtime, and this sounds like a much better alternative if what you are trying to accomplish is truly just changing the contents of the list.
To add to Micah's answer, you can also have 2 ListViews in your ViewFlipper in order to create a sliding effect when moving between the two lists.
Related
I am developing an activity with a ListView in which I need to change the current row by another layout by clicking on the row, and I'm not finding any way to do as much as I look (I take hours searching for possible solutions and I have not seen any reference to this problem). I do not know if this can be done in Android, but if anyone has an idea of how to do this would be appreciated.
Thanks in advance.
PS: The ListView control is normal and just want to replace a layout with a different layout. I'm using the API 15.
Use a ViewSwitcher
http://developer.android.com/reference/android/widget/ViewSwitcher.html
A ViewSwitcher is -
ViewAnimator that switches between two views, and has a factory from
which these views are created. You can either use the factory to
create the views, or add them yourself. A ViewSwitcher can only have
two child views, of which only one is shown at a time.
I suggest merging the two layouts in a single one and hide the second one. In your adapter data you should have a flag or something to indicate which layout to display. When you click a row, toggle that flag for the selected item and notifyDataSetChanged() on the adapter. This will make sure the changed layout remains even if you scroll up and down and the row goes off screen.
A more optimized solution is to have different item types in the adapter.
I am not sure how to go about doing this. This is my requirements for an Activity:
Header
ListView of 10 items
Header
ListsView of 10 items
The ListViewsare coming from two different data sources and may have two different row layouts. So I am thinking I need to make two Custom adapter classes (two getView()'s, etc).
Here's the kicker, I want to be able to pull-to-refresh the whole list and update both ListViews. If that is too much, I'll settle with a refresh button (that Google seems to prefer anyway); currently I use com.handmark.pulltorefresh.library.PullToRefreshListView
Is this possible? If so, what is best steps to make this work?
Very much possible.
Approach can be one ScrollView with one LinearLayout child(vertical orientation).
Now two ListView as child views for linearlayout.
As you said already,you will need two adaptors(with different row layout). Each list can have its own Header View.
For PulltoRefresh functionality,
you can have a look at pulltorefresh library
This allows you to make any View as pull to refresh. As i explained above you need ScrollView as root view, So you need to use PullToRefreshScrollView from above library.
MergeAdapter will help you to merge any number of headers and adapters into a single one. So basically in your activity only one ListView is needed and this ListView should be use a merge adapter with two headers and two custom adapters.
Merge Adapter
I want to build one activity with two listviews. One that scroll horizontally. One that scroll vertically. The two adapter attached are customized with image and text and load their data from two different sqlite tables.
i've googled for two/three months without result... there's no example that answer to my question
How can i do?
Sorry for my poor english
Gianni Maiorani
The Android SDK does not provide a horizontally scrolling ListView. You can use an open source HorizontalListView.
See the BaseAdapter and CursorAdapter for populating your ListViews with child views that contain image/text.
here is a good example for horizontal list view
http://www.dev-smart.com/archives/34
as for the vertical listview i think there will be no problem in it
I was wondering how to implement the listview like the android market.
in the right hand panel a listview is divided into the two rows. This is very useful because it saves lot of space and user can look at almost double items in the listview at a time. how can I implement this ? any suggestions?
you can use GridView instead of ListView of two columns..
1. GridView
2. GridViewExample
Sample design :
The Android Market also uses the ViewPager to scroll between the pages. Here is an example on how you could implement it.
There is another question that is related to this one.Check the folling link, it might help you:
How to display a two column ListView in Android?
Modify you *.xml file to get what you want.
Good luck!
If you want to do this using listView, then each item (row) in the list view should consist of two views. You can do this using a relative layout (or linear layout) with two items side by side.
You can also implement this using a gridview having two columns.
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.