我有一個具有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
- 我要補充選擇支持的適配器(這似乎有些奇怪,因爲它應該獨立於視圖)
聲明:我已經看到了一些相關的問題,但他們並不真正幫助我。
此外,我目前無法獲取大部分在線文檔歸因於長城
如果你使用的是Android 3.0+,請使用'activated'狀態來實現:http://stackoverflow.com/questions/9729517/showing-the-current-selection-in-a-listview – CommonsWare 2013-02-25 14:57:59
好吧,魅力。我想將您的評論標記爲答案,但這不起作用。你會把它作爲答案發布,所以我可以標記它(因爲我認爲它也可能對其他人有用),還是應該刪除該問題,因爲它或多或少是重複的,儘管我認爲它們有所不同? – TimothyP 2013-02-25 15:10:10
如果您採取的方法與我在另一個問題上的回答完全不同,那麼您可能應該自己寫答案,概述所做的事情。同樣,如果你想提供比我的簡單配方更多的細節,你自己的答案會很棒! – CommonsWare 2013-02-25 20:53:24