Android ListView 具有不同的颜色

发布于 2024-12-10 05:49:04 字数 2622 浏览 1 评论 0原文

我在 ListView 中使用不同颜色时遇到了一些问题。

我已经尝试了几件事,但似乎没有任何效果

private class GameRowAdapter extends 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;
    }
}

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

谢谢。

更新

这是充气行的 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>

I am having a little problem using different colors in a ListView.

I have tried several things but nothing seems to work

private class GameRowAdapter extends ArrayAdapter {
ArrayList objects;

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

Anyone who can tell me, what I am doing wrong?

Thank you.

UPDATE

Here is the xml of the inflatet row

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一场信仰旅途 2024-12-17 05:49:04

您仅在创建视图时定义颜色。但是,对于回收的视图,您需要再次显式设置颜色。不能保证 6 和 7 会是新视图。所以总是在每次调用 getView 时设置视图的颜色

You are only defining the color in the creation of the view. However for the recycled views you need to set the color again explicitly. There is no guarantee that 6 and 7 will be new views. So always set the color of the view with each call of getView

雄赳赳气昂昂 2024-12-17 05:49:04

编辑:运行小型本地测试后,我认为问题可能是每行中的 TextView 位于 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;
    }

Edit: After running a small local test, I think the issue may be that the TextViews you have in each row are in front of the ListView row view. Try setting the model.entry and model.score background colors. /Edit

I think it's coming from the convertViews being reused. Try moving this:

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

Outside of the if (row == null) block, and change it to this:

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

So your full getView method would be:

 @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;
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文