我正在使用一个MvxListView
加上一个FrameLayout,它承载一个片段,详细说明所选项目的信息。它工作正常,直到我需要从文件加载项目并以编程方式选择它:我使用SetItemChecked(position,true)
设置的所选项目将不再突出显示。
所以我按照这个答案https://stackoverflow.com/a/10791326/3629868的说明,指出我使用Selector
根据状态改变显示。问题是,当我将Selector
添加到我的TextView
时,如:
android:textColor="@drawable/list_item_text_selector"
如果我尝试显示我的活动,我会得到一个TargetInvocationException
,请看下面堆栈的第一行:
MvxBind:Error: 10.52 Exception during creation of TextView from type Android.Widget.TextView - exception TargetInvocationException: Exception has been thrown by the target of an invocation.
at System.Reflection.MonoCMethod.InternalInvoke (System.Object obj, System.Object[] parameters) [0x00000] in <filename unknown>:0
at System.Reflection.MonoCMethod.DoInvoke (System.Object obj, BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
at System.Reflection.MonoCMethod.Invoke (BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00000] in <filename unknown>:0
at System.Activator.CreateInstance (System.Type type, BindingFlags bindingAttr, System.Reflection.Binder binder, System.Object[] args, System.Globalization.CultureInfo culture, System.Object[] activationAttributes) [0x00000] in <filename unknown>:0
at System.Activator.CreateInstance (System.Type type, System.Object[] args, System.Object[] activationAttributes) [0x00000] in <filename unknown>:0
at System.Activator.CreateInstance (System.Type type, System.Object[] args) [0x00000] in <filename unknown>:0
at Cirrious.MvvmCross.Binding.Droid.Binders.MvxAndroidViewFactory.CreateView (System.String name, Android.Content.Context context, IAttributeSet attrs) [0x00000] in <filename unknown>:0
我首先认为可能是MvvmCross相关,因为我在SO MvvmCross 3.0.14 - MvxListView selection not working in Android上找到了这个主题,但是有一条与此问题相关的评论https://github.com/MvvmCross/MvvmCross/issues/481已在3.1.1-beta5中修复,因为我是使用v3.1.1它不应该相关。
以下是我使用的代码:
MvxListView
:
<Mvx.MvxListView
android:id="@+id/allSitesDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#30ffffff"
android:dividerHeight="1dp"
android:background="#111"
local:MvxBind="ItemsSource SitesSearchResult"
local:MvxItemTemplate="@layout/mysitelistitem" />
布局文件夹中的mysitelistitem.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="Text Name"
android:padding="16dp"
android:textSize="20sp"
android:textColor="@drawable/list_item_text_selector" />
</LinearLayout>
可绘制文件夹中的list_item_text_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<Selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_activated="true"
android:color="@color/item_selected_text"/>
<item
android:state_pressed="true"
android:color="@color/item_selected_text"/>
<item
android:state_pressed="false"
android:color="@color/item_selected_text"/>
</Selector>
(实际上我找到了一个可以证明是一个令人满意的解决方案的答案(参见How to highlight the selected item in an MvxListView),但它并没有使用我认为感觉更好的Selector
所以我很好奇如何要使用Selector
解决此问题。)