我有一個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