列表视图中的交替颜色
Alternating colors in Listview
在我的列表视图中 getView
我有这样的代码
if (position % 2 == 0) {
v.setBackgroundColor(Color.BLUE);
}
这是
的输出
但是如果数据看起来像这样怎么办
我试过上面的代码,但它看起来像这样
但我需要这样。
我不知道怎么了,但是每组颜色图案都交换了。我怎样才能根据最后的格式修复它。总是以蓝色开头
已更新
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);
}
}
有一个名为 counter 之类的 int 变量,并在代码的开头以及每当有新的 header 时将其设置为 0。
然后每行将计数器加一,如果计数器是偶数,则将背景设为蓝色。
即
counter = 0;
for each item in the list view {
if (isHeader) {
counter = 0;
} else {
if (counter % 2 = 0) {
background = blue;
}
else {
background = white;
}
counter++;
}
}
以下内容基于此处的回答:Alternating colors in listview but needs to have a starting color
- 在 ItemModel 中添加一个新的 int 字段 bgColor 并创建 getter 和 setter 方法。
变化:
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;
int bgColor = R.color.red; //Added
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();
bgColor = R.color.red; //Added
}
sectionCell = itemList.get(i); //Added
sectionCell.setBgColor(bgColor); //Added
tempList.add(sectionCell); //Changed
if (bgColor == R.color.red) bgColor = R.color.alt_gray; //Added
else bgColor = R.color.red; //Added
}
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;
}
更改适配器 getView()。背景颜色不用再计算,直接用数据项的bgColor设置即可。
希望对您有所帮助!
在我的列表视图中 getView
我有这样的代码
if (position % 2 == 0) {
v.setBackgroundColor(Color.BLUE);
}
这是
的输出但是如果数据看起来像这样怎么办
我试过上面的代码,但它看起来像这样
但我需要这样。
我不知道怎么了,但是每组颜色图案都交换了。我怎样才能根据最后的格式修复它。总是以蓝色开头
已更新
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);
}
}
有一个名为 counter 之类的 int 变量,并在代码的开头以及每当有新的 header 时将其设置为 0。
然后每行将计数器加一,如果计数器是偶数,则将背景设为蓝色。
即
counter = 0;
for each item in the list view {
if (isHeader) {
counter = 0;
} else {
if (counter % 2 = 0) {
background = blue;
}
else {
background = white;
}
counter++;
}
}
以下内容基于此处的回答:Alternating colors in listview but needs to have a starting color
- 在 ItemModel 中添加一个新的 int 字段 bgColor 并创建 getter 和 setter 方法。
变化:
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; int bgColor = R.color.red; //Added 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(); bgColor = R.color.red; //Added } sectionCell = itemList.get(i); //Added sectionCell.setBgColor(bgColor); //Added tempList.add(sectionCell); //Changed if (bgColor == R.color.red) bgColor = R.color.alt_gray; //Added else bgColor = R.color.red; //Added } 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; }
更改适配器 getView()。背景颜色不用再计算,直接用数据项的bgColor设置即可。
希望对您有所帮助!