首先 定义一个背景选择器:list_item_selector_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="@color/trade_stock_list_item_bg" />
<item android:state_selected="true"
android:drawable="@color/trade_stock_list_item_bg"/>
</selector>
第二步:在自定义的baseAdapter中的
public View getView(int position, View convertView, ViewGroup parent) {
// 获取当前Item
View view;
if(convertView == null){
view = mInflater.inflate(
R.layout.trade_buy_sale_list_items, parent, false);
view.setBackgroundResource(R.drawable.list_item_selector_bg);
}else{
view =convertView;
}
view.setBackgroundResource(R.drawable.list_item_selector_bg);是关键。
最后在listView 的点击事件处理中
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
view.setSelected(true);
}
});
view.setSelected(true)是关键。