先在drawable里写个 item_selector_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="
//这里是设置按下不松手时候的颜色,也可以单纯用颜色 @android:color/xxx
//在colors里设置例如 <color name="transparent">#00000000</color> 这样
<item android:state_pressed="true" android:drawable="@drawable/list_item_p" />
<item android:drawable="@android:color/transparent"/>//默认时候颜色
</selector>
然后在layout里把item布局background设为这个item_selector_bg.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/item_selector_bg"
android:orientation="horizontal"
>
然在baseadapter中的getview()方法里
convertView.setTag(position);
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(oldView == null){
//第一次点击
oldView = v;
v.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.list_item_p));
}else{
//非第一次点击
//把上一次点击的 变化
oldView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_selector_bg));
v.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.list_item_p));
//保存oldView
oldView = v;
}
}
});
//这一段是状态保存
if(oldView != null && (position == (Integer)oldView.getTag())){// 为点击 item
convertView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.list_item_p));
}else{
convertView.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.item_selector_bg));
}
后来我发现因为我写的是VideoView,这个思路不适用,VideoView应该是根据当前播放什么,而使相应的item变色