Android ListView微粒行背景颜色更改
我有一个自定义的Android ListView微粒行背景颜色更改,android,listview,Android,Listview,我有一个自定义的列表视图,其中我需要以绿色显示可用的移位颜色,以红色显示不可用的移位。我在它的getView()中制作了一个自定义适配器,我正在尝试检查该值并更改颜色 自定义适配器中的GetView如下- public View getView(int position, View convertView, ViewGroup parent) { View v = convertView; if(v==null){ v = inflater.inflate(R.l
列表视图
,其中我需要以绿色显示可用的移位颜色
,以红色显示不可用的移位。我在它的getView()
中制作了一个自定义适配器,我正在尝试检查该值并更改颜色
自定义适配器中的GetView如下-
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v==null){
v = inflater.inflate(R.layout.simplerow, null);
}
ViewHolder holder = null;
if(convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.simplerow, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.rowTextView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.rowTextView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
}
holder.textView1.setText(objects.get(position).getprop1());
holder.textView2.setText(objects.get(position).getprop2());
return convertView;
}
但是我现在变得这样了-
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v==null){
v = inflater.inflate(R.layout.simplerow, null);
}
ViewHolder holder = null;
if(convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.simplerow, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.rowTextView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.rowTextView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
}
holder.textView1.setText(objects.get(position).getprop1());
holder.textView2.setText(objects.get(position).getprop2());
return convertView;
}
每当我滚动时,颜色也会以其他值显示。将您替换为getView()
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.simplerow, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.rowTextView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.rowTextView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
/**
* here check your condtion like this and set color to background no need of for loop
***/
if(objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
}else
{
// set normal color here
}
holder.textView1.setText(objects.get(position).getprop1());
holder.textView2.setText(objects.get(position).getprop2());
return convertView;
}
将您替换为
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.simplerow, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.rowTextView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.rowTextView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
/**
* here check your condtion like this and set color to background no need of for loop
***/
if(objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
}else
{
// set normal color here
}
holder.textView1.setText(objects.get(position).getprop1());
holder.textView2.setText(objects.get(position).getprop2());
return convertView;
}
在为一个视图设置红色背景时,必须使用else case为其余视图设置正常背景
if(objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
} else{
//here set normal background for view.
}
希望这对您有所帮助。当您为一个视图设置红色背景时,您必须使用else case为其余视图设置正常背景
if(objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
} else{
//here set normal background for view.
}
希望这能对您有所帮助。这是由于ListView的视图重用功能。如果未满足Shift-3条件,则必须将行的背景设置为默认行颜色,因为具有红色背景的行可以在列表中的任何位置重复使用
if (objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout) convertView.findViewById(R.id.RelativeLayoutSingleRow))
.setBackgroundResource(R.drawable.red_bg);
} else {
//default bg
((RelativeLayout) convertView.findViewById(R.id.RelativeLayoutSingleRow))
.setBackgroundResource(deafult_bg);
}
这是由于ListView的视图重用能力。如果未满足Shift-3条件,则必须将行的背景设置为默认行颜色,因为具有红色背景的行可以在列表中的任何位置重复使用
if (objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout) convertView.findViewById(R.id.RelativeLayoutSingleRow))
.setBackgroundResource(R.drawable.red_bg);
} else {
//default bg
((RelativeLayout) convertView.findViewById(R.id.RelativeLayoutSingleRow))
.setBackgroundResource(deafult_bg);
}
这是解决办法
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.simplerow, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.rowTextView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.rowTextView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout) convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
} else {
//Update to default color
}
holder.textView1.setText(objects.get(position).getprop1());
holder.textView2.setText(objects.get(position).getprop2());
return convertView;
}
如果条件匹配,则设置红色,否则设置默认颜色。以下是解决方案
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
convertView = inflater.inflate(R.layout.simplerow, null);
holder.textView1 = (TextView) convertView.findViewById(R.id.rowTextView1);
holder.textView2 = (TextView) convertView.findViewById(R.id.rowTextView2);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout) convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
} else {
//Update to default color
}
holder.textView1.setText(objects.get(position).getprop1());
holder.textView2.setText(objects.get(position).getprop2());
return convertView;
}
如果条件匹配,则设置红色
颜色,否则设置默认颜色。您还需要在此处添加其他部分,如下所示-
for (int i = 1; i < 9 ; i++) {
if(objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
}
else{
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setB ackgroundResource(R.drawable.green_bg); //your green color
}
}
for(int i=1;i<9;i++){
if(objects.get(position.getprop1().equalsIgnoreCase(“Shift-3”)){
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
}
否则{
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setB backgroundResource(R.drawable.green_bg);//您的绿色
}
}
您还需要在此处添加其他部分,如下所示-
for (int i = 1; i < 9 ; i++) {
if(objects.get(position).getprop1().equalsIgnoreCase("Shift -3")) {
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
}
else{
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setB ackgroundResource(R.drawable.green_bg); //your green color
}
}
for(int i=1;i<9;i++){
if(objects.get(position.getprop1().equalsIgnoreCase(“Shift-3”)){
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setBackgroundResource(R.drawable.red_bg);
}
否则{
((RelativeLayout)convertView.findViewById(R.id.RelativeLayoutSingleRow)).setB backgroundResource(R.drawable.green_bg);//您的绿色
}
}
您必须使用getViewType()
。尝试此代码(我为您修改了代码):
编辑:我必须解释一下。如果您不使用
getViewType()
方法,并且如果语句与此处的其他答案一样,那么如果列表中有许多项(足以滚动),则在滚动时,这些项将动态更改颜色。您必须使用getViewType()
。尝试此代码(我为您修改了代码):
编辑:我必须解释一下。如果您不使用
getViewType()
方法,并且如果语句与此处的其他答案相同,那么如果列表中有许多项(足以滚动),滚动时,您的项目将动态更改颜色。在getView中设置适配器检查时,在保持架中保留一个变量。您能否给出一个示例在getView中设置适配器检查时,在保持架中保留一个变量。您能否给出一个示例?实际上,该循环用于动态检查。为了进行测试,我设置了一个静态值。它给出了和前面相同的结果…实际上,这个循环是用于动态检查的。为了进行测试,我设置了一个静态值。它给出的结果与之前的相同。。。