I am seeing many people interested in writing their own adapters for various reasons. the primary reason being their need to implement their custom layouts in the rows.
- You create two layouts:
- One for Even rows
- One for Odd rows
- These layouts are designed keeping in mind that they'll be used as layouts of a row.
- Your code has an array containing the data that will be displayed in the list view.
- Inside your code, write a simple class extending BaseAdapter.
- In this class we implement following methods of Android.Widget.Adapter super class, in order to get things working:
- getCount() - returns the total number of elements in your data array
- getIterm() - returns the data item associated with the specified position in the data set. Not implemented at this point.
- getItemId() - returns the row id associated with the specified position in the list. Not implemented at this point.
- getView() - returns the row that has to be drawn. This is the method we're interested in the most. It will be called every time the ListView draws a new row.
Here, you can control what gets drawn in a particular row, by selecting a layout and setting data into it.
In our example, we are checking whether the position of the row is even or odd, and according to that, we are setting the row's layout.
- I have also set a background to the layout which holds the list.
- A list has a transparent background in normal conditions which makes you see the image that you set as the layout background, but when the List is scrolled, it doesn't remain transparent any more.
- If you want you background image to be visible while scrolling too, set cacheColorHint of your ListView.