2013-06-28 43 views
1

我想将mvxbindable列表视图的高亮颜色从标准橙色自定义为动态颜色集。我认为这很容易,但我错了:)使用mvvmcross/mono droid自定义mvxbindablelist的选择器颜色

我发现我应该使用StateListDrawable在适配器中设置列表项的背景可绘制。我以下面的方式做到这一点(绿色是测试,_backgroundGradient是在构造函数中创建的);

public override View GetView(int position, View convertView, ViewGroup parent) 
     { 
      var view =base.GetView(position, convertView, parent); 

      StateListDrawable sld = new StateListDrawable(); 


      sld.AddState(new int[] { Android.Resource.Attribute.StateFocused }, new ColorDrawable(Color.Green)); 

      sld.AddState(new int[] { Android.Resource.Attribute.StatePressed }, new ColorDrawable(Color.Green)); 

      sld.AddState(new int[] {}, _backgroundGradient); 

      view.Focusable = true; 

      if (view != null) 
      { 
       view.SetBackgroundDrawable(sld);  
      } 


      return view; 
     } 

但是,当列表项被点击没有颜色我显示。下面是我的XML:

<listitem> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res/dk.appsfabrikken.cmsapp" 
    android:id="@+id/tableViewItem" 
    android:layout_width="fill_parent" 
    android:layout_height="90dp" 
    android:paddingBottom="1dp" 

    local:MvxBind="{'Click':{'Path':'ShowCellDetails'}}"> 

    <CmsApp.Droid.Controls.UmbracoImageView 
    android:id="@+id/viewImagePreview" 
    android:layout_width="90dp" 
    android:layout_height="90dp" 
    android:scaleType="centerCrop" 
    android:padding="5dp" 
    local:MvxBind="{'ImageData':{'Path':'ImageIconData'},'HideImage':{'Path':'Hidden'}}" /> 

    <TextView 
    android:layout_gravity="center_vertical" 
    android:id="@+id/title" 
    android:textStyle="bold" 
    android:textColor="@color/table_text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="5dp" 
    local:MvxBind="{'Text':{'Path':'Title'}}" /> 

</LinearLayout> 

我必须失去了一些东西 - 任何帮助是高度赞赏。

回答

1

好吧我自己解决了。这可能是一个新手的错误,但嘿,我解决了它。我试图向视图添加一个点击事件,这也没有被触发。所以我决定将StateListDrawable添加到listitem的linearlayout中,而不是视图的背景。这解决了我的问题。

我增加了以下我的方法在适配器:

var ll = view.FindViewById<LinearLayout>(Resource.Id.tableViewItem); 
      if (ll != null) 
      { 
       ll.SetBackgroundDrawable(sld); 
      } 

我仍然不能完全肯定它为什么当我把它添加到视图背景没有工作。 我希望这可以节省别人的一天,因为它毁了我的:)

+0

你知道你可以直接在XML中做到这一点吗? – Cheesebaron

+0

是的,但颜色的定义是动态的,并从web服务中反思。 – Bjarke