Android 如何使用带有ListView和自定义适配器的选择器来指示所选项目
我有一个具有ListView的活动,并且我创建了一个自定义适配器 基于BaseAdapter 自定义适配器的GetView方法使用自定义布局:Android 如何使用带有ListView和自定义适配器的选择器来指示所选项目,android,android-layout,xamarin.android,Android,Android Layout,Xamarin.android,我有一个具有ListView的活动,并且我创建了一个自定义适配器 基于BaseAdapter 自定义适配器的GetView方法使用自定义布局: view = context.LayoutInflater.Inflate(Resource.Layout.BluetoothDeviceListItem, null); 布局如下所示: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http:/
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是否完全错误
我是否应该向适配器添加选择支持,这似乎很奇怪,因为它应该独立于视图
免责声明:我已经看到一些相关的问题,但它们并没有真正帮助我。
此外,我目前无法访问大多数在线文档,因为长城可以将选定位置保留在内存中。在适配器中,添加一个变量以存储所选项目。然后在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; }
您可以将所选位置保留在内存中。在适配器中,添加一个变量以存储所选项目。然后在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; }
最后,基于@commonware注释,我简单地使用了激活状态:
<?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设计指南。最后,基于@commonware注释,我只是使用了激活状态:
<?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设计指南。如果您使用的是Android 3.0+,请使用激活状态:Ok,这就像一个符咒。我想把你的评论作为答案,但那不行。你能把它作为一个答案贴出来吗?这样我就可以标记它,因为我认为它可能对其他人也有用,或者我应该删除这个问题,因为它或多或少是一个重复的问题,尽管在我看来差异很大?如果你在另一个问题上采取了与我的答案完全不同的方法,你可能应该写下你自己的答案,概述你所做的事。同样,如果你想提供比我的果壳食谱更多的细节,你自己的答案会很棒!如果你使用的是安卓3.0+,请使用激活状态:Ok,这很有魅力。我想把你的评论作为答案,但那不行。你能把它作为一个答案贴出来吗?这样我就可以标记它,因为我认为它可能对其他人也有用,或者我应该删除这个问题,因为它或多或少是一个重复的问题,尽管在我看来差异很大?如果你在另一个问题上采取了与我的答案完全不同的方法,你可能应该写下你自己的答案,概述你所做的事。同样,如果你想提供比我的果壳食谱更多的细节,你自己的答案会很棒!问题是它违反了关注点的分离。该项目至少应该保持高亮显示,否则我可能想添加一个图标或任何东西。这将使项目视图的定制更加方便;我也会研究你的解决方案。同时,我使用了激活状态。但这可能不允许我添加图标,您的解决方案可能会。您还需要在单击列表项时使用“notifydatasetchanged”方法来重建视图。啊,是的,我发现问题在于它违反了关注点的分离。该项至少应该保持高亮显示,或者我可能想在其上添加一个图标或其他什么。这将使项目视图的定制更加方便;我也会研究你的解决方案。同时,我使用了激活状态。但这可能不允许我添加图标,您的解决方案可能会。您还需要在单击列表中的某个项目时使用“notifydatasetchanged”方法来重建视图。啊,是的,我找到了这一部分