我在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;
}
}
任何能告诉我的人,我做错了什么?
谢谢。
更新
这是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>
答案 0 :(得分:2)
您只是在创建视图时定义颜色。但是对于循环视图,您需要再次明确设置颜色。无法保证6和7将是新视图。所以每次调用getView
时总是设置视图的颜色答案 1 :(得分:0)
编辑:运行一个小的本地测试后,我认为问题可能是每行中的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;
}