android listview备用背景颜色

时间:2015-03-06 09:08:46

标签: android listview

我已经研究了如何交替列表视图的背景颜色的不同答案,但两种方法都无法正常工作。

我扩展了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;
                }
            }

列表视图行的自定义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>

这可以工作,但只会更改每行中文本的背景颜色。我想改变整行的背景颜色。我缺少什么?

1 个答案:

答案 0 :(得分:0)

也许您缺少的是inflate(),但是当您有2个子TextView时,我不确定您是否要设置ListView的颜色。我知道有两个选项。您可以设置当前视图的背景( convertView ,在本例中为ListView),或者在ListView中的子UI元素上设置它而不是父视图 convertView 。如果你在ListView上设置,试试这个(我没有这样做)。 getView()中的示例代码:

if (convertView == null) {
       // Inflate somehow
       convertView = LayoutInflater.from(thisContext).inflate(<custom LayoutXML>, ...
    ...
    }
convertView.setBackgroundColor(Color.BLUE);

如果您将其设置在TextView上,这就是我的工作。示例代码:

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);