繁体   English   中英

android listview备用背景颜色

[英]android listview alternate background color

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

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

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

也许您缺少的是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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM