【问题标题】:Alternating colors in listview but needs to have a starting color在列表视图中交替颜色,但需要有一个起始颜色
【发布时间】:2018-08-05 23:52:03
【问题描述】:

我有一个看起来像这样的列表视图。

| 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);
    }
}

【问题讨论】:

    标签: android listview colors


    【解决方案1】:

    很简单,当您构建数据时,添加一个新属性,例如“订单”,然后像这样构建:

    假设你的班级模型:

            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);
                            }
        }
    

    【讨论】:

    • 对不起,我不知道DateEntry 来自哪里。我希望你给我一个必须简化的代码
    • 我有 gettersetter 的课程
    • 是的,这可能是您的数据输入订单属性
    • 也许只是对你如何创建类的一点解释,直到我在我的颜色中使用它。我所需要的只是一种交替的颜色,但它需要在每组中以红色开始
    • 您没有向我们展示您是如何构建数据的,所以我们不知道
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-06
    • 1970-01-01
    • 2010-11-26
    • 2010-09-20
    • 1970-01-01
    相关资源
    最近更新 更多