Android的ListView控件用不同的颜色 [英] Android ListView with different colors
问题描述
我有在用不同的颜色有点问题的ListView
。
我已经试过几件事情,但似乎没有任何工作。
私有类GameRowAdapter扩展ArrayAdapter {
ArrayList的对象;
公共GameRowAdapter(上下文的背景下,INT textViewResourceId,ArrayList的< RowModel>对象){
超(背景下,textViewResourceId,对象);
this.objects =物体;
} @覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
查看排= convertView;
RowModelViews模型;
如果(行== NULL){
LayoutInflater吹气= LayoutInflater.from(GamePlayerActivity.this);
行= inflater.inflate(R.layout.game_line_layout,NULL);
模型=新RowModelViews();
model.entry =(TextView中)row.findViewById(R.id.gameLineEntry);
model.score =(TextView中)row.findViewById(R.id.gameLineEntryScore);
row.setTag(模型);
}其他{
模型=(RowModelViews)row.getTag();
} 如果(位置== || 6位== 7){
row.setBackgroundColor(R.color.black);
}其他{
row.setBackgroundColor(R.color.light_transparent);
} model.entry.setText(objects.get(位置).entry);
model.score.setText(objects.get(位置).score); 返回行;
}
}静态类RowModelViews {
TextView的进入;
TextView的分数;}静态类RowModel {
字符串项;
字符串分; 公共RowModel(字符串输入,字符串分数){
this.entry =条目;
this.score =得分;
}
}
任何人谁可以告诉我,我做错了?
感谢您。
更新
下面是inflatet行的XML
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=横向>
< TextView的机器人:ID =@ + ID / gameLineEntry
机器人:layout_height =FILL_PARENT
机器人:layout_width =FILL_PARENT
机器人:layout_marginLeft =20dp
机器人:TEXTSIZE =20dp
机器人:重力=center_vertical/>
< TextView的机器人:ID =@ + ID / gameLineEntryScore
机器人:layout_height =FILL_PARENT
机器人:layout_width =65dp
机器人:layout_marginRight =20dp
机器人:比重=中心
风格=@风格/按钮
机器人:layout_gravity =右/>
< / LinearLayout中>
编辑:运行一个小的局部测试之后,我认为这个问题可能是你每行中有TextViews是在ListView行视图的前面。尝试将model.entry和model.score背景颜色。 /编辑
我认为这是从convertViews即将被重用。尝试移动这样的:
如果(位置== || 6位== 7){
row.setBackgroundColor(R.color.black);
}
在if(行== NULL)块的外面,改成这样:
如果(位置== || 6位== 7){
model.entry.setBackgroundColor(R.color.black);
model.score.setBackgroundColor(R.color.black);}其他{
model.entry.setBackgroundColor(/ *无论您的默认背景颜色为* /);
model.score.setBackgroundColor(/ *无论您的默认背景颜色为* /);
}
所以,你完全getView方法是:
@覆盖
公共查看getView(INT位置,查看convertView,父母的ViewGroup){
查看排= convertView;
RowModelViews模型;
如果(行== NULL){
LayoutInflater吹气= LayoutInflater.from(GamePlayerActivity.this);
行= inflater.inflate(R.layout.game_line_layout,NULL);
模型=新RowModelViews(行);
model.entry =(TextView中)row.findViewById(R.id.gameLineEntry);
model.score =(TextView中)row.findViewById(R.id.gameLineEntryScore);
row.setTag(模型);
}其他{
模型=(RowModelViews)row.getTag();
} 如果(位置== || 6位== 7){
model.entry.setBackgroundColor(R.color.black);
model.score.setBackgroundColor(R.color.black);
}其他{
model.entry.setBackgroundColor(/ *无论您的默认背景颜色为* /);
model.score.setBackgroundColor(/ *无论您的默认背景颜色为* /);
} model.entry.setText(objects.get(位置).entry);
model.score.setText(objects.get(位置).score); 返回行;
}
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>
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;
}
这篇关于Android的ListView控件用不同的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!