基于 属性 启用 MvvmCross MvxListView ItemClick

MvvmCross MvxListView ItemClick Enabled Based on property

所以我有一个 MvxListView

<Mvx.MvxListView
    android:id="@+id/receptionsListView"
    android:divider="@drawable/divider"
    android:scrollbars="vertical"
    android:choiceMode="singleChoice"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="left|start"
    local:MvxItemTemplate="@layout/item_supplier"
    local:MvxBind="ItemsSource ReceptionSuppliersList;  ItemClick SelectReceptionCommand;" />

我想 enabled/disabled 一些基于列表中模型值的项目。像

  local:MvxBind="ItemsSource ReceptionSuppliersList; ItemClick SelectReceptionCommand; Enabled ReceptionSuppliersList.IsValid" />

根据我的测试,这只是禁用了我所有的列表项,因为没有这样的 属性 ReceptionSuppliersList.IsValid(它是 ReceptionSuppliersList[i].IsValid )。我怎样才能做到这一点。

我也试过像这样在 item_supplier 上添加启用 属性

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="0dp"
    style="@style/ExtendProTheme.ReceptionItem"
    local:MvxBind="Enabled IsValid" >

但是还是不行

有什么想法可以根据 属性 从我的列表中禁用某些项目吗?

PS : 我的物品看起来像这样

public string Username { get; set; }

public string DeviceId { get; set; }

public bool IsValid { get; set; }

这不是真的 problem-solve,但它应该适合你的情况:

  1. 在项目上添加灰色覆盖,使其看起来像是未启用。
  2. 检查是否在 SelectReceptionCommand 的方法处理程序上启用了项目。
    
    private void OnSelectReceptionCommandExecuted(Reception item)
            {
                if (!reception.IsEnabled)
                {
                    return;
                }
                // your code here
            }

您可以创建自定义适配器来处理 enabling/disabling 列表项。您只需要将 Adapters ItemsSource 投射到您的 ViewModels IsEnabled 覆盖中的 ReceptionSuppliersList 类型。

public class CustomAdapter : MvxAdapter
{
    public CustomAdapter(Context context) 
        : base(context)
    {
    }

    public CustomAdapter(Context context, IMvxAndroidBindingContext bindingContext) 
        : base(context, bindingContext)
    {
    }

    protected CustomAdapter(IntPtr javaReference, JniHandleOwnership transfer) :
        base(javaReference, transfer)
    {
    }

    public override bool IsEnabled(int position)
    {
        var items = ItemsSource as List<TestModel>;
        return items[position].IsValid;
    }
}

将适配器分配给您的 MvxListView:

var receptionsListView = FindViewById<MvxListView>(Resource.Id.receptionsListView);
receptionsListView.Adapter = new CustomAdapter(this, BindingContext as IMvxAndroidBindingContext);