列表视图中的交替颜色,但需要有一个起始颜色



我有一个如下所示的列表视图。

| Header |
----------
| Data 1 |
----------
| Data 2 |
----------
| Data 3 |
----------
| Header |
----------
| Data 1 |
----------
| Header |
----------
| Data 1 |
----------
| Data 2 |
----------
| Data 3 |
----------
| Data 4 |
----------

这是我在交替的行上添加一些颜色时的代码

\My view
public View getView(int position, View convertView, ViewGroup parent) {
/* Alternating Colors*/
LinearLayout line_others = v.findViewById(R.id.line_others);
if (position % 2 == 0) {
line_others.setBackgroundResource(R.color.red);
} else {                         
line_others.setBackgroundResource(R.color.alt_gray);
}
}

这是输出

| Header |
----------
| Data 1 | GRAY
----------
| Data 2 | RED
----------
| Data 3 | GRAY
----------
| Header |
----------
| Data 1 | RED
----------
| Header |
----------
| Data 1 | RED
----------
| Data 2 | GRAY
----------
| Data 3 | RED
----------
| Data 4 | GRAY
----------

事实上,这是有效的,但我需要取得一些成就。我需要从小组第一排的红色开始,比如这样。

| Header |
----------
| Data 1 | RED
----------
| Data 2 | GRAY
----------
| Data 3 | RED
----------
| Header |
----------
| Data 1 | RED
----------
| Header |
----------
| Data 1 | RED
----------
| Data 2 | GRAY
----------
| Data 3 | RED
----------
| Data 4 | GRAY
----------

我的问题是我如何才能做到这一点?感谢

更新

ItemModel.java

public class ItemModel implements Comparable<ItemModel> {
private boolean isSectionHeader;
private String cusname;
private String date;
}
public String getCusname() {
return cusname;
}
public void setCusname(String cusname) {
this.cusname = cusname;
}
public boolean isSectionHeader() {
return isSectionHeader;
}
@Override
public int compareTo(ItemModel itemModel) {
return this.date.compareTo(itemModel.date);
}
public void setToSectionHeader() {
isSectionHeader = true;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getRemarks() {
return remarks;
}

public ItemModel(String cusname, String remarks, String date) {
this.isSectionHeader = isSectionHeader;
this.cusname = cusname;
this.remarks = remarks;
this.date = date;
}

在这里,我将数据从sqllite传输到阵列

private ArrayList<ItemModel> getItems() {
Cursor data = myDb.get_plan(pattern_email);
ArrayList<ItemModel> items = new ArrayList<>();
while (data.moveToNext()) {
String cusname = data.getString(0);
String remarks = data.getString(2);
String date = data.getString(3);
items.add(new ItemModel(cusname, remarks, date));
}
return items;
}

这是分拣机和列表视图中的显示

private ArrayList sortAndAddSections(ArrayList<ItemModel> itemList) {
ArrayList<ItemModel> tempList = new ArrayList<>();
ArrayList<Integer> tmpHeaderPositions = new ArrayList<>();
Collections.sort(itemList);
ItemModel sectionCell;
String header = "";
int addedRow = 0;
for (int i = 0; i < itemList.size(); i++) {
if (!(header.equals(itemList.get(i).getDate()))) {
String cusname = itemList.get(i).getCusname();
String remarks = itemList.get(i).getRemarks();
sectionCell = new ItemModel(cusname, remarks, date);
sectionCell.setToSectionHeader();
tmpHeaderPositions.add(i + addedRow);
addedRow++;
tempList.add(sectionCell);
header = itemList.get(i).getDate();
}
tempList.add(itemList.get(i));
}
tmpHeaderPositions.add(tempList.size());
for (int i = 0; i < tmpHeaderPositions.size() - 1; i++) {
sectionCell = tempList.get(tmpHeaderPositions.get(i));
sectionCell.setDate(sectionCell.getDate() + " (" +
(tmpHeaderPositions.get(i + 1) - tmpHeaderPositions.get(i) - 1) + ")");
}
return tempList;
}

这是我的观点

public View getView(int position, View convertView, ViewGroup parent) {
/* Alternating Colors*/
LinearLayout line_others = v.findViewById(R.id.line_others);
if (position % 2 == 0) {
line_others.setBackgroundResource(R.color.red);
} else {                         
line_others.setBackgroundResource(R.color.alt_gray);
}
}

简单地说,当你构建数据时,添加一个新的属性,比如"order",然后像这样构建:

假设你的班级模型:

DataEntry {
int order;
... 
}

addHeader 1
addDataEntry(order: 1)
addDataEntry(order: 2)
addDataEntry(order: 3)
addDataEntry(order: ... n)
addHeader 2
addDataEntry(order: 1)
addDataEntry(order: 2)
addDataEntry(order: 3)
addDataEntry(order: ... n)
...
public View getView(int position, View convertView, ViewGroup parent) {
/* Alternating Colors*/
DataEntry entry = list.get(position);
LinearLayout line_others = v.findViewById(R.id.line_others);
if (entry.position % 2 == 0) {
line_others.setBackgroundResource(R.color.red);
} else {
line_others.setBackgroundResource(R.color.alt_gray);
}
}

相关内容

最新更新