Reputation: 89
I want to add gradient for each row. What I've tried to do is on listview label put this attribut, android:background="@drawable/mygradient". But All I get is this gradient displayed all along the list. What I want is this gradient to be displayed for each item.
Thanks in advance
Upvotes: 1
Views: 1472
Reputation: 57316
You can provide a custom adapter for your list view and set the gradient as the background in its getView
method. Something like this:
public class MyAdapter extends BaseAdapter {
List<MyDataType> myData;
Context context;
public MyAdaptor(Context ctx, List<MyDataType> myData) {
this.myData = myData;
this.context = ctx;
}
public int getCount() { return myData.size() };
public Object getItem(int pos) { return myData.get(pos); }
public long getItemId(int pos) { return 0L; }
public View getView(int pos, View convertView, ViewGroup parent) {
//this is where you can customise the display of an individual
//list item by setting its background, etc, etc.
...
//and return the view for the list item at the end
return <List item view>;
}
}
Then you can set this adapter as the adapter for your list:
ListView myList = <initialise it here>
myList.setAdapter(new MyAdapter(getContext(), listData);
Now whenever a list item needs to be displayed, getView
method will be called, where you'll perform all the necessary display customisation, including setting the background.
Upvotes: 1
Reputation: 29199
Instead of setting background property set Selector of list, as below:
android:listSelector="@drawable/list_selector_background"
Upvotes: 0