有一個用於擴展ListFragment的自定義適配器。列表的每一行都是左側的ImageView和右側的TextView。單擊圖像時,ImageView應將其背景更改爲不同的圖像。我實現了這種方式:在列表視圖中更改單擊的imageview背景
public class MyAdapter extends BaseAdapter{
...
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// context.firstViewItem = position;
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_layout,
null);
viewHolder = new ViewHolder();
viewHolder.likeBtn = (ImageView)convertView.findViewById(R.id.likeBtn);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.likeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
view.setBackgroundResource(R.drawable.abc_btn_rating_star_on_mtrl_alpha);
}
});
}
return convertView;
}
}
但問題是,這不僅ImageView的背景的變化,但在列表視圖變化的第三或第四排的每個ImageView的。我怎樣才能改變點擊的ImageView的背景?
護理細說R.id.IMAGE_1? – Nazerke 2015-04-06 11:04:38
它是您的圖像視圖ID,位於xml文件中 – AndroidHacker 2015-04-06 11:05:48