我有一个看起来像这样的列表视图。
| 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);
}
}
答案 0 :(得分:1)
轻松,在构建数据时,添加一个新属性(例如“ 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);
}
}