如何更改自定义列表视图排颜色变化或者?(How to change Custom Listview

2019-10-29 13:26发布

我已创建自定义的LISTVIEW用于显示搜索结果列表中要显示与背景颜色各行我尝试了一些代码,但不工作按我的要求,请帮助我

这里是我的代码

public class ListCustomBaseAdapter extends BaseAdapter {
private static ArrayList<SearchResult> searchArrayList;

private LayoutInflater mInflater;

public ListCustomBaseAdapter(Context context,
        ArrayList<SearchResult> results) {
    searchArrayList = results;
    mInflater = LayoutInflater.from(context);
}

public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.custom_row_view, null);
        holder = new ViewHolder();
        holder.txtName = (TextView) convertView.findViewById(R.id.custNm);
        holder.txtProsNm = (TextView) convertView.findViewById(R.id.prosNm);
        holder.txtFrmPort = (TextView) convertView
                .findViewById(R.id.frmPort);
        holder.txtToPort = (TextView) convertView.findViewById(R.id.ToPort);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    if (position % 2 == 0) {
        holder.txtName.setBackgroundColor(Color.WHITE);
        holder.txtProsNm.setBackgroundColor(Color.WHITE);<----- change bgcolor of textview not whole row of View
        holder.txtFrmPort.setBackgroundColor(Color.WHITE);
        holder.txtToPort.setBackgroundColor(Color.WHITE);
    } else {
        holder.txtName.setBackgroundColor(Color.DKGRAY);
        holder.txtProsNm.setBackgroundColor(Color.DKGRAY);
        holder.txtFrmPort.setBackgroundColor(Color.DKGRAY);
        holder.txtToPort.setBackgroundColor(Color.DKGRAY);
    }
    holder.txtName.setText(searchArrayList.get(position).getCustNm());
    holder.txtProsNm.setText(searchArrayList.get(position).getProsNm());
    holder.txtFrmPort.setText(searchArrayList.get(position).getFrmPort());
    holder.txtToPort.setText(searchArrayList.get(position).getToPort());

    return convertView;
}

static class ViewHolder {
    TextView txtName;
    TextView txtFrmPort;
    TextView txtToPort;
    TextView txtProsNm;
}

}

呼叫转接器

 public void onCreate(Bundle savedInstanceState) throws SQLException {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Connection object for Spinner
    con = new DatabaseConnection(this);


    try {
        // Get Filtermap from Parent Activity
        // this Bundle contains HashMap
        Bundle wrapper = getIntent().getBundleExtra("salesActList");
        salesLstObj = (Map<String, Object>) wrapper.getSerializable("salesActCriteriaList");
        salesLst = con.searchSalesActivity(salesLstObj);

        ArrayList<SearchResult> searchResults = getSearchResults();

        lv = (ListView) findViewById(R.id.srListView);
        lv.setAdapter(new ListCustomBaseAdapter(this, searchResults));


        });
    } catch (SQLException se) {         
        se.getStackTrace();
        String flag = "fail";
        dialogBox(flag);

}

这里是输出IMG

要显示蓝色区域为灰色交替如何做呢?

Custome行视图代码

  <TableLayout
    style="@style/TableLayoutStyle"
    android:orientation="vertical"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" >

    <TableRow
        android:id="@+id/tblRwCust"
        style="@style/TableRowStyle" >

        <TextView
            android:id="@+id/custNm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#FF7F24"
            android:textSize="18sp"
            android:textStyle="bold" />
    </TableRow>

    <TableRow
        android:id="@+id/tblRwPros"
        style="@style/TableRowStyle" >

        <TextView
            android:id="@+id/prosNm"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#000000" />
    </TableRow>

    <TableRow
        android:id="@+id/tblRwPort"
        style="@style/TableRowStyle" >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/frmPort"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="5dp"
                android:textColor="#808080" />

            <TextView
                android:id="@+id/ToPort"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#808080" />
        </LinearLayout>
    </TableRow>
</TableLayout>

Answer 1:

if(position%2 == 0 ){
            holder.convertView.setBackgroundColor(Color.CYAN);
        }else{
            holder.convertView.setBackgroundColor(Color.YELLOW);
        }


Answer 2:

这里是我的解决方案Table layout ,而不是Relative layout

<TableLayout
    style="@style/TableLayoutStyle"
    android:orientation="vertical"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp" 
    android:id="@+id/myTable"
    android:background="#FFFFFF">

在适配器

holder.myTable = (TableLayout) convertView.findViewById(R.id.myTable);

if (position % 2 == 0) {
        holder.myTable.setBackgroundColor(Color.WHITE);
    } else {
        holder.myTable.setBackgroundColor(Color.GRAY);
    }


文章来源: How to change Custom Listview row color change alternatively?