我有一个列表视图,其中我有一些按钮。单击每个按钮时,我正在改变它的当前颜色&设置其他颜色按钮作为默认颜色。所以当我按下按钮清理第一个位置,然后它的颜色变为绿色&其他按钮颜色设置为灰色。我一次可以看到5行。当我向下滚动时,第7行的干净按钮也会变绿。我不知道为什么。我认为这是列表视图回收视图问题,但根据第6行按钮的颜色应该改变。请帮助我为什么发生这种情况我已经尝试了我使用过的每一件可能的事情查看持有人模式但这也行不通。列表视图中滚动使用resued项目?
获取视图方法
@Override
public View getView(final int position, View rowView, ViewGroup parent) {
select_postion=position;
Log.i("error","select--"+select_postion);
note_pos = position;
// View rowView = convertView;
if (rowView == null)
{
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(layoutResourceId, null, true);
holder = new ViewHolder();
holder.componentName = (TextView) rowView
.findViewById(R.id.location_list_row);
holder.clean = (Button) rowView.findViewById(R.id.btn1);
holder.dirty = (Button) rowView.findViewById(R.id.btn2);
holder.dc = (Button) rowView.findViewById(R.id.btn3);
holder.na = (Button) rowView.findViewById(R.id.btn4);
holder.camra = (ImageView) rowView.findViewById(R.id.btn5);
holder.camra.setTag(position);
holder.notes = (Button) rowView.findViewById(R.id.btn6);
holder.count_text = (TextView) rowView
.findViewById(R.id.circle_count);
holder.red_circle = (ImageView) rowView.findViewById(R.id.img_red);
holder.position=position;
holder.clean.setTag(holder);
holder.camra.setTag(holder);
holder.na.setTag(holder);
holder.dc.setTag(holder);
holder.notes.setTag(holder);
holder.dirty.setTag(holder);
rowView.setTag(holder);
}
else
{
// rowView=convertView;
holder=(ViewHolder)rowView.getTag();
}
LocationInspectionBean location_obj = values.get(position);
if (values.get(position).getImages() != null)
{
imgpath = values.get(position).getImages();
imgpath1 = imgpath.split(",");
count = imgpath1.length;
holder.red_circle.setVisibility(View.VISIBLE);
holder.count_text.setVisibility(View.VISIBLE);
holder.count_text.setText(String.valueOf(count));
}
holder.componentName.setText(location_obj.getComp_name());
if (location_obj.getInspectionstatus().equalsIgnoreCase("1"))
{
holder.clean.setBackgroundColor(Color.parseColor("#1C6614"));
Log.i("SYNC", "Status is 1");
}
else if (location_obj.getInspectionstatus().equalsIgnoreCase("3"))
{
holder.dirty.setBackgroundColor(Color.parseColor("#FC4E3B"));
Log.i("SYNC", "Status is 3");
}
else if (location_obj.getInspectionstatus().equalsIgnoreCase("4")) {
holder.na.setBackgroundColor(Color.parseColor("#0D6CC3"));
Log.i("SYNC", "Status is 4");
}
else if (location_obj.getInspectionstatus().equalsIgnoreCase("2")) {
holder.dc.setBackgroundColor(Color.parseColor("#E87403"));
Log.i("SYNC", "Status is 2");
}
if (location_obj.getNotes().isEmpty()) {
holder.dirty.setBackgroundColor(Color.parseColor("#8A8787"));
}
else if (location_obj.getNotes().isEmpty()) {
holder.dc.setBackgroundColor(Color.parseColor("#8A8787"));
}
holder.clean.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
// Log.i("SYNC", "camera"+String.valueOf(index));
v.setBackgroundColor(Color.parseColor("#1C6614"));
ViewHolder h = (ViewHolder)v.getTag();
index= h.position;
Toast.makeText(getApplicationContext(), Integer.toString(position), Toast.LENGTH_SHORT).show();
h.dirty.setBackgroundColor(Color.parseColor("#8A8787"));
h.na.setBackgroundColor(Color.parseColor("#8A8787"));
h.dc.setBackgroundColor(Color.parseColor("#8A8787"));
Status = "1";
// index = (Integer)v.getTag();
Log.i("SYNC", String.valueOf(index));
String timeStamp = new SimpleDateFormat(
"MM/dd/yyyy hh:mm:ss a").format(new Date());
db.updateInspectionDetails(inspection_id, user_id,
location_inspection_array.get(position)
.getComponentid(), subclient_id, client_id,
Status, images_path_string, timeStamp);
return false;
}
});
您需要存储按钮的状态,即哪个按钮是'绿色',哪个按钮是'灰色'。 –
我怎么能这样做。很少有人建议把按钮放在if块中,并且他们中的一些人建议把if块放在外面,那么什么是正确的方式? – Techiee
根据位置状态,你正在改变'颜色',我是对吗? –