MVVMCROSS MVXLISTVIEW ITEMCLICK基于属性启用



所以我有一个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;" />

我想根据列表中模型的值启用/禁用某些项目。像

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

从我测试的内容中,由于没有这样的属性接待upplierslist.isvalid(它的接待台上upplierslist [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; }

您可以创建一个自定义适配器来处理列表项目的启用/禁用。您只需要将适配器ItemsSource投入到您的ViewModelsIsEnabled覆盖中的接待示台列表类型。

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

这不是一个真正的问题,但它应该适合您的情况:

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

最新更新