2013-02-25 76 views
2

我有一個具有ListView的活動,並且我創建了基於BaseAdapter的自定義適配器 。如何使用具有ListView和自定義適配器的選擇器來指示所選項目

定製適配器的GetView方法使用自定義佈局:

view = context.LayoutInflater.Inflate(Resource.Layout.BluetoothDeviceListItem, null); 

佈局看起來是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/BluetoothDeviceListSelector"> 
    <TextView android:id="@+id/txtBluetoothDeviceName" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      style="@android:style/TextAppearance.Large"/> 
    <TextView android:id="@+id/txtBluetoothDeviceAddress" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginLeft="5dp" 
      style="@android:style/TextAppearance.Medium"/> 
</LinearLayout> 

基本上我顯示的藍牙設備的名稱和它的地址下面。
但我希望用戶能夠在ListView中選擇一個項目,並且該項目應該保持突出顯示(至少),或者我可能想要爲其添加圖標或其他任何項目。

據我所知,由於我使用自定義佈局,我失去了默認選擇指示器 ,必須使用我自己的選擇器。我發現了一個在線教程,這樣的事情:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" > 
    <shape> 
     <gradient 
     android:startColor="#E77A26" 
     android:endColor="#1DB38B" 
     android:angle="270" /> 
    </shape> 
    </item> 
    <item android:state_selected="true"> 
    <shape> 
     <gradient 
     android:startColor="#E77A26" 
     android:endColor="#2B37AE" 
     android:angle="270" /> 
    </shape> 
    </item> 
    <item android:state_focused="true"> 
    <shape> 
     <gradient 
     android:startColor="#E77A26" 
     android:endColor="#EEFFEE" 
     android:angle="270" /> 
    </shape> 
    </item> 
</selector> 

但這唯一的一點是,只要改變顏色爲用戶按下並在ListView持有 的項目。只要他放開,沒有任何突出顯示。

現在我無法弄清楚究竟是什麼導致了這個問題。

  • 沒有我的ListView失去選擇支持的地方,一路上
  • 對於選擇根本就是錯誤的XML
  • 我要補充選擇支持的適配器(這似乎有些奇怪,因爲它應該獨立於視圖)

聲明:我已經看到了一些相關的問題,但他們並不真正幫助我。
此外,我目前無法獲取大部分在線文檔歸因於長城

+1

如果你使用的是Android 3.0+,請使用'activated'狀態來實現:http://stackoverflow.com/questions/9729517/showing-the-current-selection-in-a-listview – CommonsWare 2013-02-25 14:57:59

+0

好吧,魅力。我想將您的評論標記爲答案,但這不起作用。你會把它作爲答案發布,所以我可以標記它(因爲我認爲它也可能對其他人有用),還是應該刪除該問題,因爲它或多或少是重複的,儘管我認爲它們有所不同? – TimothyP 2013-02-25 15:10:10

+0

如果您採取的方法與我在另一個問題上的回答完全不同,那麼您可能應該自己寫答案,概述所做的事情。同樣,如果你想提供比我的簡單配方更多的細節,你自己的答案會很棒! – CommonsWare 2013-02-25 20:53:24

回答

3

最後,基於我只是用激活狀態@CommonsWare評論:

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_pressed="true" > 
    <shape> 
     <gradient 
     android:startColor="#E77A26" 
     android:endColor="#1DB38B" 
     android:angle="270" /> 
    </shape> 
    </item> 
    <item android:state_activated="true"> 
    <shape> 
     <gradient 
     android:startColor="#E77A26" 
     android:endColor="#EEFFEE" 
     android:angle="270" /> 
    </shape> 
    </item> 
</selector> 

這就是說,我現在已經從顯示所選擇的移動離開項目和我只需 導航到一個新的活動,直接點擊一個項目,以符合Android設計指引。

1

您可以將選定的位置保留在內存中。在您的適配器中,添加一個變量來存儲選定的項目。然後在getView()方法中,如果當前位置等於您選擇的項目,則執行一些不同的操作。如果你想突出你的項目,setBackgroundColor()

下面是一個例子

@Override public View getView(int position, View convertView, 
    ViewGroup parent) { 

     View row = convertView; 

     if (row == null) {  
      LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);   
      row = inflater.inflate(R.layout.my_xml_file, parent,false); } 
     if(position==selectedPosition){    
      row.setBackgroundColor(Color.parseColor("#800ff000"));  } 
     else{  
      row.setBackgroundColor(Color.TRANSPARENT);  } 

     return row; } 
+0

問題在於這違反了關注點的分離。 – TimothyP 2013-02-25 16:20:26

+0

「該項目應該保持突出顯示(至少),或者我可能想添加一個圖標或任何其他。」 這將使該項目的視圖的定製更多一點;) – Gordak 2013-02-25 17:00:39

+0

我也會考慮你的解決方案。同時我使用了激活狀態。但是,這可能不允許我添加圖標,您的解決方案可能會。 – TimothyP 2013-02-25 17:12:31

相關問題