我已经设法通过为不同的状态制作几个选择器来实现这一点
首先把它放在你的列表视图中
android:listSelector="@drawable/list_selector"
然后在drawable中创建xml文件来控制不同的状态
@drawable/list_selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/list_item_bg_normal" android:state_activated="false"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_pressed="true"/>
<item android:drawable="@drawable/list_item_bg_pressed" android:state_activated="true"/>
</selector>
@drawable/list_item_bg_normal
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/list_background"
android:endColor="@color/list_background"
android:angle="90" />
</shape>
@drawable/list_item_bg_pressed
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/list_background_pressed"
android:endColor="@color/list_background_pressed"
android:angle="90" />
</shape>
在您的 ListView 选择中
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
view.setSelected(true);
...
}
}
不要忘记将 list_background_pressed 和 list_background 添加到您的 values/color.xml 或在每个文件中手动设置颜色。
而且我相信,当您使用 setSelection(int pos) 时,它会自动使用您设置为选中的布局。
希望对你有帮助。