Android的ListView控件。如何改变手动选择的项目的背景颜色 [英] Android ListView. How to change background color of manually selected item
问题描述
你能帮助我请。我需要更改手动选择 setSelection(INT POS)功能,我的列表视图项的背景颜色,我需要留在新的颜色,直到新的setSelection电话。我已经阅读了如何做到这一点的一些话题,但我仍然没有更迭。谢谢!
Can You help me please. I need to change background color of my list view item which is selected manually by setSelection(int pos) function and I need to stay with new color until new setSelection call. I have read some topics of how to do this, but I still have no succes. Thanks!
推荐答案
我已经成功地实现了通过几个选择的不同状态
I've managed to accomplish that by making several selectors for the different states
第一次把这个在您的列表视图
first put this in your listview
android:listSelector="@drawable/list_selector"
然后,在绘制控制diferent州创建XML文件
Then create xml files in drawable to control the diferent states
@绘制/ 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>
@绘制/ 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>
@绘制/ 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选择
In your ListView Selection
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,long arg3) {
view.setSelected(true);
...
}
}
不要忘了添加的 list_background_ pressed 和 list_background 以你的价值观/ color.xml或只是手动设置的颜色中的每个文件。
Don't forget to add list_background_pressed and list_background to your values/color.xml or just set the color manually in each file.
和我相信,当您使用 setSelection(INT POS),将自动USET你设置为selectected,布局
And I Believe that when you use setSelection(int pos) that will automatically uset the layout you've set as selectected.
希望它帮助。
这篇关于Android的ListView控件。如何改变手动选择的项目的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!