我想使用BaseAdapter 在ListView中顯示item。我在BaseAdapter使用下面的代碼:
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
//...
convertView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
v.setBackgroundResource(R.drawable.ic_corner_four_click);
break;
case MotionEvent.ACTION_UP:
v.setBackgroundResource(R.drawable.ic_corner_four);
break;
}
return false;
}
});
}
當點擊item時,背景就變成 ic_corner_four_click。但是當放開手指或移動到其它的item,就不會再更新成 ic_corner_four。如何修改?
你完全可以只用一個selector來做,類似於:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/list_focus_color" />
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/list_focus_color" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/list_focus_color" />
<item android:drawable="@drawable/list_normal_color" />
</selector>
然後設置成item的整個layout的background