我創建了一個從ListActivity類派生的RecordActivity類,併爲在.xml中定義的ListView設置了選擇模式和選擇器。默認行爲是所選項目只有在我按下時纔會高亮顯示。我想保持選中的項目被突出顯示。我試圖覆蓋ArrayAdapter的getView方法,但是,它不起作用。任何幫助將不勝感激。如何在ListActivity類中保持所選項目高亮顯示
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
public class RecordsActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.records);
List<Record> values = getAllRecords();
// Use the SimpleCursorAdapter to show the
// elements in a ListView
adapter = new ArrayAdapter<Record>(this, android.R.layout.simple_list_item_1,
values);
setListAdapter(adapter);
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
getListView().setSelector(android.R.color.holo_red_dark);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
selectedItem = position;
v.setSelected(true);
}
}
你可以找到你的方式在這些鏈接,返回:[鏈接1] [1] [ LINK2] [2] [鏈接3] [3] [鏈路4] [4] [1]:http://stackoverflow.com/questions/5058291/highlight-listview-selected-row [2]:http://stackoverflow.com/questions/10788688/programmatically-select-item-listview [3]:http://stackoverflow.com/questions/5853719/highlighting-the-selected-item- in-the-listview-in-android [4]:http://stackoverflow.com/questions/5972155/does-anyone-know-how-to-highlight-a-selected-item-in-a-android -listview –