Android ListView 具有不同的颜色
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您仅在创建视图时定义颜色。但是,对于回收的视图,您需要再次显式设置颜色。不能保证 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
编辑:运行小型本地测试后,我认为问题可能是每行中的 TextView 位于 ListView 行视图的前面。尝试设置 model.entry 和 model.score 背景颜色。 /编辑
我认为它来自被重用的convertViews。尝试将其移至
if (row == null) 块之外,并将其更改为:
因此完整的 getView 方法将是:
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:
Outside of the if (row == null) block, and change it to this:
So your full getView method would be: