我必须将特定listView项的背景设置为灰色,条件是isDelivered()和getPicked()都应为true。 对于打开活动时出现的前几个listItem,背景设置正确但是一旦我向下滚动,随机项背景设置为灰色,不符合我的条件。
(我感觉它的位置问题)但是我的自定义适配器类扩展了ArrayAdapter,我的logcat上的位置也是正确的顺序0,1,2,3 ......
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.custaddress, parent, false);
}
final View rowView = convertView;
tripPickUp = (Button) rowView.findViewById(R.id.picked);
tripDelivered = (Button) rowView.findViewById(R.id.delivered);
tripIsDelivered = (TextView) rowView.findViewById(R.id.isdelivered);
if (tripsheet[position].getPicked() == false
&& tripsheet[position].isDelivered() == false) {
tripPickUp.setVisibility(View.VISIBLE);
tripDelivered.setVisibility(View.INVISIBLE);
tripIsDelivered.setVisibility(View.INVISIBLE);
}
else if (tripsheet[position].isDelivered() == false
&& tripsheet[position].getPicked() == true) {
tripPickUp.setVisibility(View.INVISIBLE);
tripDelivered.setVisibility(View.VISIBLE);
tripIsDelivered.setVisibility(View.INVISIBLE);
} else {
tripDelivered.setVisibility(View.INVISIBLE);
tripIsDelivered.setVisibility(View.VISIBLE);
tripPickUp.setVisibility(View.INVISIBLE);
rowView.setBackgroundResource(R.color.gray);
}
return rowView;
}
基本上,else语句是执行的条件。
答案 0 :(得分:0)
这是因为出于性能原因,适配器会回收视图。如果你确保始终设置背景,不仅当它应该是灰色时,你应该没事。
换句话说,不要信任视图以在xml中声明初始状态,请确保明确设置任何项目在任何条件下都可以更改的所有颜色和其他属性。
答案 1 :(得分:0)
GetView重用视图。无论它处于(或颜色)状态,它们可能与xml中的状态不同。
所以基本上你将一些项目设置为灰色(当条件为真时),并且当它们被重用时,你不会将它们设置为白色。
这是让你前进的事情:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.custaddress, parent, false);
}
final View rowView = convertView;
tripPickUp = (Button) rowView.findViewById(R.id.picked);
tripDelivered = (Button) rowView.findViewById(R.id.delivered);
tripIsDelivered = (TextView) rowView.findViewById(R.id.isdelivered);
if (tripsheet[position].getPicked() == false
&& tripsheet[position].isDelivered() == false) {
tripPickUp.setVisibility(View.VISIBLE);
tripDelivered.setVisibility(View.INVISIBLE);
tripIsDelivered.setVisibility(View.INVISIBLE);
rowView.setBackgroundResource(Color.WHITE);
}
else if (tripsheet[position].isDelivered() == false
&& tripsheet[position].getPicked() == true) {
tripPickUp.setVisibility(View.INVISIBLE);
tripDelivered.setVisibility(View.VISIBLE);
tripIsDelivered.setVisibility(View.INVISIBLE);
rowView.setBackgroundResource(Color.WHITE);
} else {
tripDelivered.setVisibility(View.INVISIBLE);
tripIsDelivered.setVisibility(View.VISIBLE);
tripPickUp.setVisibility(View.INVISIBLE);
rowView.setBackgroundResource(R.color.gray);
}
return rowView;
}
答案 2 :(得分:0)
你可以尝试一件事:
删除:final View rowView = convertView;
而不是返回rowView
只返回convertView
并替换此
rowView.setBackgroundResource(R.color.gray);
通过
convertView.setBackgroundResource(R.color.gray);
我也遇到了这个问题。我认为制作rowView final会产生问题。