0

I'm using a MvxListView coupled with a FrameLayout which host a fragment detailing the selected item's information. It worked fine until I needed to load an item from a file and select it programmatically : the selected item I set using SetItemChecked(position,true) wouldn't be highlighted anymore.

So I followed the instructions on this answer https://stackoverflow.com/a/10791326/3629868 which pointed me to the use of a Selector to change the display depending on the state. The problem is that when I add the Selector to my TextView as in :

android:textColor="@drawable/list_item_text_selector"

If I try to display my activity I will get a TargetInvocationException, see the first lines of the stack below :

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 

I first thought it might be MvvmCross related as I found this topic on SO : MvvmCross 3.0.14 - MvxListView selection not working in Android but one comment linked to this issue https://github.com/MvvmCross/MvvmCross/issues/481 which was fixed in 3.1.1-beta5 and since I'm using v3.1.1 it should not be related.

Here is the code I use :

The 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 from the layout folder

<?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 from the drawable folder

<?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>

(Actually I found an answer that could prove to be a satisfying solution (see How to highlight the selected item in an MvxListView) but it doesn't use the Selector which feels better in my opinion so I'm curious on how to resolve this with a Selector.)

Community
  • 1
  • 1
tperon
  • 1
  • 3
  • What if you do your selector not on textColor but on background drawable? I was looking at this example http://stackoverflow.com/questions/3592780/selector-on-background-color-of-textview – PkL728 Jun 01 '14 at 12:02
  • @PkL728 I implemented the example you provided, there is no more exception but I don't see any effect of the selector either.. :/ – tperon Jun 02 '14 at 14:24
  • Ok, so after my brain got refreshed during the weekend I realised the initial reason why I decided to use a `Selector` (item wouldn't be highlighted) was a mistake from my part : I used a bad index to set the selected item... Such a stupid mistake... I don't know why I didn't think of it earlier... Anyway, the question was about using the `Selector` and that particular problem isn't solved so I'll let it open until a satisfying answer is provided. But I'll be moving on with my code so I might not be able to test answers right away. – tperon Jun 02 '14 at 14:52

0 Answers0