[英]android listview alternate background color
I have worked through the different answers on how to alternate the background color of a list view, but neither way works properly. 我已经研究了如何交替列表视图的背景颜色的不同答案,但两种方式都不能正常工作。
I extended the SimpleCursorAdapter
and set the background color depending on the line number: 我扩展了
SimpleCursorAdapter
并根据行号设置背景颜色 :
private class MySimpleCursorAdapter extends SimpleCursorAdapter {
private ViewHolder holder;
private int layoutRessource;
private LayoutInflater mInflater;
public MySimpleCursorAdapter(Context applicationContext, int layout, Cursor cursor,
String[] from, int[] to) {
super(applicationContext, layout, cursor, from, to, 0);
layoutRessource = layout;
mInflater = LayoutInflater.from(applicationContext);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
holder = (ViewHolder) view.getTag();
holder.getTextAbove().setText(cursor.getString(cursor.getColumnIndex("_id")));
holder.getTextBelow().setText(cursor.getString(cursor.getColumnIndex("ICDTxt")));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
Log.d(TAG, "entered newView");
View view = mInflater.inflate(layoutRessource, null);
holder = new ViewHolder();
holder.setTextAbove((TextView) view.findViewById(R.id.text_code));
holder.setTextBelow((TextView) view.findViewById(R.id.text_description));
holder.setLayout((LinearLayout) view.findViewById(R.id.rowLayout));
view.setTag(holder);
return view;
}
public View getView(int position, View convertView, ViewGroup parent) {
Log.d(TAG, "entered get View");
Log.d(TAG, "convertView == null: " + (convertView == null));
View rowView = super.getView(position, convertView, parent);
if ( position % 2 == 0 ) {
rowView.setBackgroundColor(Color.BLUE);
// rowView.setBackgroundResource(R.color.listBackgroundEven);
} else {
rowView.setBackgroundResource(R.color.listBackgroudOdd);
}
return rowView;
}
private class ViewHolder {
private TextView textAbove = null, textBelow = null;
public TextView getTextAbove() {
return textAbove;
}
public void setTextAbove(TextView textAbove) {
this.textAbove = textAbove;
}
public TextView getTextBelow() {
return textBelow;
}
public void setTextBelow(TextView textBelow) {
this.textBelow = textBelow;
}
}
The custom LayoutXML for the lines of the listview 列表视图行的自定义LayoutXML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rowLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_code"
android:textAppearance="@android:style/TextAppearance.Holo.Large"
android:textColor="@color/fontColor"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/text_description"
android:textAppearance="@android:style/TextAppearance.Holo.Medium"
android:textColor="@color/fontColor"/>
</LinearLayout>
This works but only changes the backgroundcolor of the text in each line. 这可以工作,但只更改每行中文本的背景颜色。 I would like to change the background color of the whole line.
我想改变整行的背景颜色。 Anything I am missing?
我错过了什么?
Perhaps all you are missing is to inflate() but I am not sure you want to set the color of the ListView when you have 2 child TextViews. 也许您缺少的是inflate()但是我不确定您是否想要在有2个子TextView时设置ListView的颜色。 There are 2 options that I know of.
我知道有两个选项。 You can set the background of the current view ( convertView , in this case ListView) or set it on the child UI element in ListView instead of the parent view, convertView .
您可以设置当前视图的背景( convertView ,在本例中为ListView)或将其设置在ListView中的子UI元素而不是父视图convertView上 。 If you set on the ListView, try this (I did not do this).
如果你在ListView上设置,试试这个(我没有这样做)。 Sample code in getView():
getView()中的示例代码:
if (convertView == null) {
// Inflate somehow
convertView = LayoutInflater.from(thisContext).inflate(<custom LayoutXML>, ...
...
}
convertView.setBackgroundColor(Color.BLUE);
If instead you set it on your TextView, and this is what I do. 如果你把它设置在TextView上,这就是我的工作。 Sample code:
示例代码:
if (convertView == null) {
// Inflate somehow
convertView = LayoutInflater.from(thisContext).inflate(<custom LayoutXML>, ...
...
}
TextView tvDescr = (TextView) convertView.findViewById(R.id.text_description);
tvDescr.setBackgroundResource(R.color.BLUE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.