2010-10-08 174 views
15

一個活動有一個按鈕和一個ListView。 最初,只有按鈕是可見的。當按下按鈕時,顯示ListView。 顯示時,我是否可以將一個特定項目顯示爲選定/聚焦?Android:在ListView打開時將項目設置爲選定項目?

用例可能是假設它是一個語言設置列表,並且當列表打開時,當前選擇的語言必須顯示爲突出顯示。

如果我知道該項目的索引,如何將其設置爲專注於顯示?

回答

15

簡而言之,ListView :: setSelection(int position)就是你需要的。但是,根據設備是否處於觸摸模式,它可能有或沒有視覺效果(背景突出顯示)。欲瞭解更多詳情,請參閱Android ListView Selection Problem

+1

不,setSelection(pos)不能解決我的問題!該設備不處於觸摸模式,但我仍然沒有得到背景突出顯示。我只是在模擬器上測試。我的列表視圖是使用選擇器xml,希望對此沒有影響! – kiki 2010-10-08 07:20:30

+0

您如何知道您的設備未處於觸摸模式?也許你可以嘗試使用導航鍵來突出顯示按鈕,並使用中鍵來按下按鈕。看看這是否有所作爲。 – onlygo 2010-10-08 07:28:17

+0

我現在只是在Eclipse中使用模擬器。並使用我的電腦鍵盤來處理。 – kiki 2010-10-08 07:46:17

11

如果您使用您的AdapterListView此代碼添加到您的適配器:

public class MyAdapter extends ArrayAdapter<MyClass> { 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      LayoutInflater inflator = (LayoutInflater) getContext() 
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      rowView = inflator.inflate(R.layout.my_adapter, null); 
     } else { 
      rowView = (View) convertView; 
     } 

     //... 

     // set selected item 
     LinearLayout ActiveItem = (LinearLayout) rowView; 
     if (position == selectedItem){ 
      ActiveItem.setBackgroundResource(R.drawable.background_dark_blue); 

      // for focus on it 
      int top = (ActiveItem == null) ? 0 : ActiveItem.getTop(); 
      ((ListView) parent).setSelectionFromTop(position, top); 
     } 
     else{ 
      ActiveItem.setBackgroundResource(R.drawable.border02); 
     }  
    } 

    private int selectedItem; 

    public void setSelectedItem(int position) { 
     selectedItem = position; 
    }  
} 

在你Activity

myAdapter.setSelectedItem(1); 
+0

Works great buddy – 2016-03-15 14:29:42

37

我後我的解決方案,因爲谷歌仍然不知道答案。

getListView().setItemChecked(selectedGroupIndex, true); 
+0

投票+1接受此帖作爲答案,而不是setSelection – radistao 2015-11-05 00:28:22

+1

這對我不起作用,請您詳細說明一下嗎?這在2018年仍然相關嗎? – 2018-01-04 12:13:52

0

我使用的適配器,並不想設置自定義背景顏色,但使用android:state_selected在drawable xml中。 SetSelection對我不起作用,但也許這也是因爲我需要SetNotifyDataChanged,它顯示Selected State不是持久的。

我還發現ListView中的項目的Selected狀態不是持久的,因爲SetNotifyDataChanged導致更新清理它們的ListView佈局。在適配器的GetView中將項目設置爲「選中」也太快了。

最後,我設置了選定項目的視圖的選擇狀態後,列表視圖的佈局已被改變,這是什麼時候觸發LayoutChange事件(在Java中它可能附加到ListView的OnLayoutChangeListener)。

爲了使它非常容易,我將所選項目的視圖存儲爲適配器的SelectedItemView。 在ListView的LayoutChange事件處理程序中,我只是將適配器的SelectedItemView.Selected設置爲true。

下面是從我的活動中的代碼,我設置的適配器爲ListView還訂閱LayoutChange(或Java中附加一個OnLayoutChangeListener)

 ringTonesListView.Adapter = ringTonesListAdapter; 
     ringTonesListView.LayoutChange += (s, layoutChangeArgs) => { 
      //At this stage the layout has been updated and the Selected can be set to true for the view of the selected item. This will result in android:state_selected logic to be applied as desired and styling can be completely done per layout in Resources. 
      ringTonesListAdapter.SelectedItemView.Selected = true; 
     }; 

這裏是我的適配器代碼:

public class RingTonesListAdapter : BaseAdapter<RingToneItem> 
{ 
    List<RingTone> Items { get; set; } 

    public override View GetView(int position, View convertView, ViewGroup parent) 
    { 
     View view = convertView; 

     // re-use an existing view, if one is available 
     // otherwise create a new one 
     if (view == null) 
     { 
      view = Context.LayoutInflater.Inflate(Resource.Layout.AlertSoundItem, parent, false); 
      view.Click += SelectRingTone; 
     } 

     RingTone ringTone = this[position]; 
     if (ringTone.Selected) 
     { 
      //==> Important 
      //Store this view since it's the view for the Selected Item 
      SelectedItemView = view; 
      //Setting view.Selected to true here doesn't help either, since Selected will be cleared after. 
     } 

     return view; 
    } 

    private void SelectRingTone(object sender, EventArgs args) 
    { 
     View view = (View)sender; 
     string title = view.FindViewById<TextView>(Resource.Id.ringToneTitle).Text; 
     RingToneItem ringToneItem = Items.First(rt => rt.Title == title); 
     if (!ringToneItem.Selected) 
     { 
      //The RingTone was not selected and is selected now 
      //Deselect Old and Select new 
      foreach (RingToneItem oldItem in Items.Where(rt => rt.Selected)) 
      { 
       oldItem.Selected = false; 
      } 

      // Select New RingTone 
      ringToneItem.Selected = true; 
      //Update the ListView. 
      //This will result in removal of Selected state for all Items when the ListView updates it's layout 
      NotifyDataSetChanged(); 
     } 

     //Now play the test sound 
     NotifierService.TestSound(Context, ringToneItem); 
    } 

    public View SelectedItemView { get; set; } 
}