簡體   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