我尝试更改列表视图中所选项目的背景,当我滚动列表视图时,我获得了更多项目。 我知道它发生了,因为我回收我的项目列表,但我找不到如何解决这个问题
这里是我的代码: class BabyAdapter扩展了ArrayAdapter {
BabyHolder babyholder;
BabyAdapter(ArrayList<BabyHolder> list) {
super(BabyNames.this, R.layout.namerow, R.id.tvName, list);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
TextView tv = (TextView) row.getTag();
if(tv == null){
tv = (TextView) row.findViewById(R.id.tvName);
row.setTag(tv);
}
row.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
v.setBackgroundColor(Color.BLUE);
}
});
babyholder = getItem(position);
tv.setTag(Integer.valueOf(position));
tv.setText(babyholder.name);
return row;
}
}
public BabyHolder getItem(int position) {
return (((BabyAdapter) getListAdapter()).getItem(position));
}
感谢您的帮助:]
答案 0 :(得分:0)
您可以转到用于给listView充气的layout.xml文件,然后将背景设置为如下所示:
android:background="?android:attr/selectableItemBackground"