关于listview中的tag,点击item背景切换。松手也不会变。
tag可以用来保留一个当前被点击的 view。下次再点击的时候,可以清除被保留view的背景。
menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View tag=(View)parent.getTag();
if (tag != null)
{
if (tag.getId()==id) return;
tag.setBackgroundResource(R.drawable.disselected);
}
view.setBackgroundResource(R.drawable.selected);
parent.setTag(view);
}
});
上面的adapter用到了自己写的初始化list view的背景色选项
import java.util.List;
import java.util.Map;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SimpleAdapter;
public class changeBGAdapter extends SimpleAdapter {
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View v = super.getView(position, convertView, parent);
v.setBackgroundResource(R.drawable.disselected);
return v;
}
public changeBGAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
super(context, data, resource, from, to);
}
}