我在 Baseadapter 中使用了像这样的水平列表视图。
private class ListViewItemClickListener implements
AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (imageview != null) {
previousImage = (ImageView) imageview
.findViewById(R.id.imageView);
previousImage.setSelected(false);
TextView previousTitle = (TextView) textview
.findViewById(R.id.title);
previousTitle.setTextColor(Color.parseColor("#33D6AD"));
previousTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
}
selected_image = (ImageView) view.findViewById(R.id.imageView);
selected_image.setSelected(true);
selected_text = (TextView) view.findViewById(R.id.title);
selected_text.setTextColor(Color.parseColor("#368AFF"));
selected_text.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
select_position = position;
imageview = selected_image;
textview = selected_text;
}
}
private static String[] dataObjects = new String[] { "그랜드 캐니언",
"그레이트 베리어 리프", "디즈니 월드", "뉴질랜드 남섬", "케이프 타운", "암리차르의 황금사원",
"라스베가스", "시드니", "뉴욕", "타지마할" };
private BaseAdapter mAdapter = new BaseAdapter() {
@Override
public int getCount() {
return dataObjects.length;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.listitem, null);
}
TextView title = (TextView) convertView.findViewById(R.id.title);
title.setText(dataObjects[position]);
return convertView;
}
};
如果单击一项,我想更改文本颜色。但是每当我单击列表视图中的一项时,两项的颜色现在正在改变。这两项之间的距离始终为 5。我在 getView 代码上使用 convertView。并且 textcolor 更改代码在 clicklistener 方法中。我可以用 setTag、getTag 解决这个问题吗?如果是,我该如何解决?