在列表视图中交替显示颜色,但需要具有起始颜色 [英] Alternating colors in listview but needs to have a starting color

查看:83
本文介绍了在列表视图中交替显示颜色,但需要具有起始颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a listview that looks like this.

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

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

and here is my code when im putting some colors on alternating rows

\\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
----------

实际上它正在工作,但是我需要取得一些成就.我需要从一组这样的组中的第一行开始使用红色.

Actually it is working but i need to achieve something. I need to start with color red for every first row in a group something like this.

| 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
----------

我的问题是我该如何实现?谢谢

My question is how can I achieve that? thanks

已更新

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传输到数组的地方

This where I transfer my data from sqllite to array

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

这里是排序器,并显示在列表视图中

Here is the sorter and display in listview

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"),然后像这样进行构建:

Easy, when you build your data , add a new attribute like 'order', then build like this:

假设您的课程模型是

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

这篇关于在列表视图中交替显示颜色,但需要具有起始颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆