【问题标题】:MVVMCROSS Android ListView RefreshMVVMCROSS Android ListView 刷新
【发布时间】:2014-05-05 13:59:11
【问题描述】:

我创建了一个 listview.axaml 并在该 listview 中调用我的自定义 template.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"
    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 { 活动_活动; 私有 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 类更改图像视图...

我可以使用 GetBindableView 方法更改 ImageView 图像,但在 ui 上它不会立即刷新,但是当我们滚动列表视图时,图像会得到反映。

谁能帮我解决这个问题。

ui 没有立即刷新?

谢谢, 阿曼

已编辑

请在下方找到 ValueConverterCode

    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 ;
        }
     }

注意:- 我的图片位于 Resources/Drawable 文件夹中

【问题讨论】:

  • 你为什么不为该图像使用转换器?如果您为这种事情使用自定义适配器,那么您就错过了使用 Mvvm 的全部意义......
  • 是的,你是对的,使用转换器将是理想的选择,但我已经尝试过,而不是继续使用适配器,kIndly 找到我的转换器代码

标签: android android-listview xamarin.android mvvmcross


【解决方案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 版本

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-18
    • 2011-01-08
    • 2016-01-17
    相关资源
    最近更新 更多