我有一个ListView & listselector.xml。最初我使用setSeletion(位置)来设置项目。但使用此代码时,项目不会突出显示。只有当我点击一个项目时才会突出显示。Android:突出显示setSelecton上的项目
ArrayAdapter<String> ad = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listItems);
timeListView = (ListView) findViewById(R.id.listList);
timeListView.setAdapter(ad);
timeListView.setOnItemClickListener(this);
timeListView.setSelector(R.drawable.listselector);
timeListView.setSelection(toSelectPos);
=============================
XML
<ListView android:id="@+id/listList" android:layout_width="match_parent" android:layout_height="wrap_content" android:dividerHeight="1dp" >
listselector.xml代码
<?xml version="1.0" encoding="UTF-8"?>
<!-- listselector.xml -->
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item android:state_focused="true" android:state_selected="false"
android:drawable="@drawable/focused"/>
<!-- Pressed -->
<item android:state_selected="true" android:state_focused="false"
android:drawable="@drawable/selected" />
</selector>
上面的代码突出全的ListView,我只是要突出项目(BG改变颜色)。为此,我相信我将不得不为项目创建另一个xml,并且为项目&设置选择器属性,而不是像上面所做的那样列出。纠正我,如果我错了。
重点是,无论如何只有点击和setSelection上的选定项目应该改变。使用上面的代码,它不会在setSelection上更改,如何实现这一点。
我查看了其他Q,但找不到我要找的点,所以请不要将它标记为其他Q的重复。
感谢
你想多选还是单选? – rajpara 2012-08-07 09:26:03
@AndroidCoader,仅限单个选择 – Tvd 2012-08-07 09:27:26
setSeletion(位置)设置当前选择的项目。如果在触摸模式下,该项目不会被选中,但它仍然会被适当定位。如果指定的选择位置小于0,则选择位置0处的项目。 – 2012-08-07 09:29:34