Reputation:
I need to add a switch (Android 4.0+) to my ListView , just a single switch with an ImageView Button , I have seen soo many tutorials that teach of adding several views in the list activities but what I need is just a single option of the list to have this switch . I know if I can put a button or textView I can put a switch in there , but I can't understand how is putting a button or any view there without repeating the whole same scenario on all the options , as for me , I am new to the ArrayAdapters and ListViews , but I need to learn these two so I can use them later on ! what I need is like this for example:
______________________
Image | Switch
______________________
Normal option
______________________
Button | Whatever view
_______________________
Normal option
_______________________
Checkable Option
_______________________
and so on ... So if you clearly , I want full control over every single option in there , thats what I need and want . Thanks for any help :)
Upvotes: 0
Views: 772
Reputation: 5636
if you're gonna do it by listview, then you can do it by being explicit about each position in the getView
method of the adapter. here's an example:
public View getView(int position, View convertView, ViewGroup parent) {
switch (position) {
case 0:
// do image switch option
// convertView = layout 0
break;
case 3:
// do button whatever option
// convertView = layout 3
break;
case 5:
// do button whatever option
// convertView = layout 5
break;
default:
// do normal option
// convertView = layout 1
break;
}
return convertView;
}
getView
is a method in many android adapters that returns a view. When hooked up to a listView
, it will be recognized as the method to return a view for each one of its rows. Each row in a listview that you see is the product of one executed instance of this method. You don't call it: it is called automatically whenever rows of a listview need to appear, or whenever a row position moves off-screen and comes back, or whenever the listView is being refreshed, in general whenever listview rows must be "made". It is there for you to override so that you can customize how ever for the specific look that you want.
convertView
is a view as well. It is the product of a concurrent mechanism running in the listview adapter called view-recycling. Imagine that you have a listview that needs to display 1000 rows for your game's shopping menu. a conventional approach would be that you simply make an inflate command for your in getView
for a vibrant xml and edit the components to show the item differences. So, that's inflating a 1000 times (and inflating is a expensive step, mind you), plus all that has to be in memory and is just clogging up your garbage collector. Plus, what if you need to refresh something to show changes? Get outta here! There are quad-core phones now, but there is a better way.
listviews done right don't actually make 1000 views at once but only the 7 or 8 that are on the screen at a time. When a listview row view is scrolled beyond the boundaries of the phone's screen, it isn't thrown out just yet. This concurrent mechanism runs and this view is saved and held under the title of convertView
and passed in as a parameter for the next getView
call. Not to (re-)use it is a terrible waste, because essentially you have a view that looks like the next row in the next getView call, perhaps with only the text in a textview changed or something like that, but you're gonna do another expensive inflation anyway. Then the unused convertView knows that it's second chance has been disregarded, into the GC, and it's business as usual.
The above code doesn't make use of convertView
, because frankly it wasn't asked in the question, but it's very simple to utilize.
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
// inflation step
// convertView = inflation
}
// rest of code involving components of inflated view
return convertView;
}
First you check if there's nothing there, which you can imagine is when a listview is first launched and nothing has had the chance to be on deck to be thrown out. And only then do your inflation then. Next time around, getView will have it's mouth full with used view and will plug it in (if it's being returned). This is the single most beneficial action for your listview performance-wise that you can do right off the bat.
But you should know this, about half the questions tagged android on this site are on this alone, there are no guarantees that convertView will plop in the position that you want, as in right below if you're scrolling up and right above if scrolling down. Just make sure that you're being explicit about what goes at what position and you'll be fine. For more information on this and other listview know-how watch this: http://www.youtube.com/watch?v=wDBM6wVEO70.
Upvotes: 2