I am building a custom adapter for a listview - I would like this adapter to give the listview alternating background colours.
boolean alternate = false;
@
You can use this code:
if (position % 2 == 0) {
holder.image.setBackgroundResource(R.drawable.image1);
} else {
holder.image.setBackgroundResource(R.drawable.image2);
}
These are the following steps to do show. Step1.1) Use two selector for odd and even postion list item
artists_list_backgroundcolor.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="@color/grey" />
<item android:state_pressed="true"
android:drawable="@color/itemselected" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@color/itemselected" />
</selector>
Step 1.2) artists_list_background_alternate.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="false"
android:state_pressed="false"
android:drawable="@color/sign_out_color" />
<item android:state_pressed="true"
android:drawable="@color/login_hover" />
<item android:state_selected="true"
android:state_pressed="false"
android:drawable="@color/login_hover" />
</selector>
Step2) colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="survey_toplist_item">#EFEDEC</color>
<color name="survey_alternate_color">#EBE7E6</color>
<color name="grey">#ffffff</color>
<color name="itemselected">#EDEDED</color>
<color name="login_hover">#E5F5FA</color>
<color name="sign_out_color">#e84040</color>
</resources>
Step 3) In Arrayadapter:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = lInflater.inflate(R.layout.listitem, parent, false);
}
if (position % 2 == 0) {
view.setBackgroundResource(R.drawable.artists_list_backgroundcolor);
} else {
view.setBackgroundResource(R.drawable.artists_list_background_alternate);
}
((TextView) view.findViewById(R.id.heading)).setText(data.get(position));
return view;
}
For more details go through belog link
http://amitandroid.blogspot.in/2013/03/android-listview-with-alternate-list.html
private int[] colors = new int[] { 0xffD3D3D3, 0xffA9A9A9 };
inside getView refer the id
holder._linear_text_active_release_date = (LinearLayout) convertView.findViewById(R.id.relative_text_active_release_date);
holder._linear_text_active_release_status = (LinearLayout) convertView.findViewById(R.id.linear_text_active_release_status);
add these lines in the getView to set the colour for layout row
holder._linear_text_active_release_status.setBackgroundColor(Color.LTGRA;
holder._linear_text_active_release_pass.setBackgroundColor(ContextCompat.getColor(context,R.color.amber));
Please correct me if I am wrong but I do it this way:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = ((LayoutInflater) this._ctx
.getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(this._resource, parent, false);
}
switch(position % 3){
case 0: convertView.setBackgroun....
break;
.... (case 1; case 2;)
}
return convertView;
}
You're not going in the right direction as if the views are re-used you might get unexpected results in that some recycled views will have a different colors, while others not.
Instead of above, set the background based on position. Something like:
if(position % 2 == 0) {
// set some color
} else {
// set the other color
}
You can do in your adapter
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(resourceId, parent, false);
}
Person user = getItem(position);
if(user.isCharged)
convertView.setBackgroundColor(Color.BLUE);
}
that will be applied in all your items