2013-04-15 77 views
2

我創建了一個從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);  
    } 
} 
+0

你可以找到你的方式在這些鏈接,返回:[鏈接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 –

回答

4

嘗試使用simple_list_item_activated_1

adapter = new ArrayAdapter<Record>(this, android.R.layout.simple_list_item_activated_1, 
                 values); 
0

試試這個,在蜂窩或更高,所選項目將保持突出顯示。由於android.R.layout.simple_list_item_activated_1只適用於honeycomb或更高版本,因此您應該像這樣添加布局以支持較舊的平臺。

int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1 
       : android.R.layout.simple_list_item_1; 


adapter = new ArrayAdapter<Record>(this, layout, values); 
1
private int selectedValue; 
    private View row; 

    ListView.setOnItemClickListener(new OnItemClickListener() { 

     @Override 
     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      // TODO Auto-generated method stub 
      selectedValue=arg2; 
      if(row!=null) 
      { 
           row.setBackgroundResource(R.drawable.group_item_normal); 
      } 
      row=arg1; 
     arg1.setBackgroundResource(R.drawable.group_item_pressed); 

     } 
    }); 

不要在getView(): - v是對象由getView()

if(selectedValue==position) 
     { 
      v.setBackgroundResource(R.drawable.group_item_pressed); 

     } 
     else{ 
      v.setBackgroundResource(R.drawable.group_item_normal); 

     } 
+0

@yinhao這看起來不錯..你試過這個嗎? – Sunny

+2

是的,它爲listview的一行突出顯示。如果你想要多個然後使用arrayList。 – user2251725

相關問題