2014-05-05 138 views
1

我已經創建了一個listview.axaml,並在那個listview裏面我打電話給我的自定義template.axml文件。MVVMCROSS Android ListView刷新

下面是我的列表視圖看起來怎麼樣

<?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" 
    android:background="@android:color/background_light"> 
    <Mvx.MvxListView 
     android:id="@+id/ListView" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:cacheColorHint="#FFDAFF7F" 
     local:MvxBind="ItemsSource MyLists; ItemClick ShowDetailCommand" 
     local:MvxItemTemplate="@layout/customlistview" /> 
</LinearLayout> 
下面

是我的自定義模板,看看如何喜歡

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:local="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="63dp"> 
    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent"> 
     <ImageView 
      android:src="@drawable/greenColor" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:id="@+id/Status" /> 
     <LinearLayout 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:id="@+id/linearLayoutOfList" 
      android:layout_weight="0.8"> 
      <TextView 
       android:text="Text" 
       android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:id="@+id/Name" 
       android:textColor="@color/gray_color" 
       android:textStyle="bold" 
       android:textAppearance="?android:attr/textAppearanceMedium" 
       android:singleLine="true" 
       local:MvxBind="Text Name" /> 
     </LinearLayout> 
     <ImageView 
      android:src="@drawable/rightArrow" 
      android:layout_width="wrap_content" 
      android:layout_height="match_parent" 
      android:id="@+id/imageView1" /> 
    </LinearLayout> 
</RelativeLayout> 

下面是我在哪裏基於一定的價值

改變圖像我的適配器類

公共類CustomAdapter:MvxAdapter { 活動_activity; private int _position;

public CustomAdapter(Context context, IMvxAndroidBindingContext bindingContext) 
     : base(context, bindingContext) 
    { 
     _activity = (Activity)context; 
    }  

    public override View GetView(int position, View convertView, ViewGroup parent) 
    {   
     return base.GetView(position, convertView, parent); 
    } 

    protected override View GetBindableView(View convertView, object source, int templateId) 
    { 
     var view = convertView ?? _activity.LayoutInflater.Inflate(Resource.Layout.CustomListView, null, false); 

     if (source is Project.Mobile.Core.Models.MyList) 
     { 
      ImageView imageView = view.FindViewById<ImageView>(Resource.Id.Status); 

      switch ((int)source.Flag) 
      { 
       case 1: 

        imageView.SetImageResource(Resource.Drawable.greenColor); 
        break; 
       case 2: 

        imageView.SetImageResource(Resource.Drawable.yellowColor); 
        break; 
       case 3: 

        imageView.SetImageResource(Resource.Drawable.redColor); 
        break; 
       default: 

        imageView.SetImageResource(Resource.Drawable.greenColor); 
        break; 
      } 
     } 
     return base.GetBindableView(view, source, templateId); 
    } 
} 

我改變使用CustomAdapter類的ImageView ...

我能夠改變使用GetBindableView方法,但在用戶界面上不立即刷新,但ImageView的影像,當我們滾動列表視圖然後圖像得到反映。

任何人都可以幫我在這裏排序。

Ui不會立即刷新?

感謝, Aaman

編輯

請在下面找到

public class ImageChangeValueConverter : MvxValueConverter<int, string> 
    { 
     protected override string Convert(int value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      string strImage = string.Empty; 
      switch ((int)value) 
      { 
       case 1: 
        strImage = "res:greenColor"; 
        break; 
       case 2: 
        strImage = "res:yellowColor"; 
        break; 

       case 3: 
        strImage = "res:redColor"; 
        break; 
       default: 
        strImage = "res:greenColor"; 
        break; 
      } 
      return strImage ; 
     } 
    } 

注ValueConverterCode: - 我的圖像都躺在裏面資源/可繪製文件夾

+0

爲什麼你不使用轉換器的圖像?您錯過了使用Mvvm的全部要點,如果您使用自定義適配器來處理這種事情...... – Cheesebaron

+0

是的,您正確使用轉換器將是理想的選擇,但我已經嘗試過,首先要轉向使用適配器, kIndly找到我的代碼轉換器 – aamankhaan

回答

1

我已經解決我的問題使用轉換器,但在UI級別上,我使用了DrawableName代替的ImageUrl像下面

 <ImageView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/taskStatus" 
     local:MvxBind="DrawableName DynamicImageChange" /> 

器和轉換器類返回字符串像下面

protected override object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
     { 
      string ImagePath = string.Empty; 
      ImagePath = "@drawable/greenstriponlist"; 
      return ImagePath ; 
     } 

我希望這可以幫助別人。

注意: - 使用mvvmcross 3.1版本