我在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;
}
};
如果单击一个项目,我想更改textcolor。但每当我点击列表视图中的一个项目时,两个项目的颜色现在正在改变。这两个项之间的距离总是5.我在getView代码上使用convertView。和textcolor更改代码在clicklistener方法中。我能用setTag,getTag解决这个问题。如果是的话,我该如何解决这个问题?
答案 0 :(得分:3)
将此代码放在适配器类中:
private int selectedPos = -1;
public void setSelectedPosition(int pos){
selectedPos = pos;
notifyDataSetChanged();
}
@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]);
if (selectedPos == position) {
title.setTextColor(Color.parseColor("#368AFF"));
title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
} else {
title.setTextColor(Color.parseColor("#000000"));
}
return convertView;
}
点击项目,调用方法设置当前位置:
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
adapter.setSelectedPosition(pos);
}
答案 1 :(得分:0)
试试如下:
private class ListViewItemClickListener implements
AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
mPager.setCurrentItem(position);
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;
view.setTag((Integer) position);
}
}
答案 2 :(得分:0)
试试这个
convertView.setOnClickListener
而不是
onItemClick