繁体   English   中英

Android ListView有不同的颜色

[英]Android ListView with different colors

我在ListView使用不同的颜色有一点问题。

我尝试了几件事似乎没什么用

私有类GameRowAdapter扩展ArrayAdapter {ArrayList对象;

    public GameRowAdapter(Context context, int textViewResourceId, ArrayList<RowModel> objects) {
        super(context, textViewResourceId, objects);
        this.objects = objects;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        RowModelViews model;
        if (row == null) {
            LayoutInflater inflater = LayoutInflater.from(GamePlayerActivity.this);
            row = inflater.inflate(R.layout.game_line_layout, null);
            model = new RowModelViews();
            model.entry = (TextView) row.findViewById(R.id.gameLineEntry);
            model.score = (TextView) row.findViewById(R.id.gameLineEntryScore);
            row.setTag(model);
        } else {
            model = (RowModelViews) row.getTag();
        }

        if (position == 6 || position == 7) {
            row.setBackgroundColor(R.color.black);
        } else {
            row.setBackgroundColor(R.color.light_transparent);
        }

        model.entry.setText(objects.get(position).entry);
        model.score.setText(objects.get(position).score);

        return row;
    }
}

static class RowModelViews {
    TextView entry;
    TextView score;

}

static class RowModel {
    String entry;
    String score;

    public RowModel(String entry, String score) {
        this.entry = entry;
        this.score = score;
    }
}

谁能告诉我,我做错了什么?

谢谢。

UPDATE

这是inflatet行的xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:orientation="horizontal">
    <TextView android:id="@+id/gameLineEntry"
              android:layout_height="fill_parent"
              android:layout_width="fill_parent"
              android:layout_marginLeft="20dp"
              android:textSize="20dp"
              android:gravity="center_vertical"/>
    <TextView android:id="@+id/gameLineEntryScore"
              android:layout_height="fill_parent"
              android:layout_width="65dp"
              android:layout_marginRight="20dp"
              android:gravity="center"
              style="@style/Button"                  
              android:layout_gravity="right"/>
</LinearLayout>

您只是在创建视图时定义颜色。 但是对于循环视图,您需要再次明确设置颜色。 无法保证6和7将是新视图。 因此,每次调用getView时总是设置视图的颜色

编辑:运行一个小的本地测试后,我认为问题可能是每行中的TextViews都在ListView行视图的前面。 尝试设置model.entry和model.score背景颜色。 /编辑

我认为它来自convertViews被重用。 试试这个:

if (position == 6 || position == 7) {
    row.setBackgroundColor(R.color.black);
}

在if(row == null)块之外,并将其更改为:

if (position == 6 || position == 7) {
    model.entry.setBackgroundColor(R.color.black);
    model.score.setBackgroundColor(R.color.black);

} else {
    model.entry.setBackgroundColor(/*Whatever your default background color is*/);
    model.score.setBackgroundColor(/*Whatever your default background color is*/);
}

所以你的完整getView方法将是:

 @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        RowModelViews model;
        if (row == null) {
            LayoutInflater inflater = LayoutInflater.from(GamePlayerActivity.this);
            row = inflater.inflate(R.layout.game_line_layout, null);
            model = new RowModelViews(row);
            model.entry = (TextView) row.findViewById(R.id.gameLineEntry);
            model.score = (TextView) row.findViewById(R.id.gameLineEntryScore);
            row.setTag(model);
        } else {
            model = (RowModelViews) row.getTag();
        }

        if (position == 6 || position == 7) {
            model.entry.setBackgroundColor(R.color.black);
            model.score.setBackgroundColor(R.color.black);
        } else {
            model.entry.setBackgroundColor(/*Whatever your default background color is*/);
            model.score.setBackgroundColor(/*Whatever your default background color is*/);
        }

        model.entry.setText(objects.get(position).entry);
        model.score.setText(objects.get(position).score);

        return row;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM