MvxListView可勾选列表项 [英] MvxListView checkable list item

查看:201
本文介绍了MvxListView可勾选列表项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得的CustomChoiceList与MvvmCross工作,但挣扎获得样品的工作,该列表项将不会被选中。

I wanted to get the CustomChoiceList to work with MvvmCross, but was struggling getting the sample working, the ListItem wouldn't get selected.

在事实上该示例使用扩展的LinearLayout并实现ICheckable定制的LinearLayout。
当使用带MvxAdapter和MvxListView相同的布局方法OnCreateDrawableState不会被调用以及文本和选择图标永远不会燃点高。

In fact the sample uses a custom LinearLayout that extends LinearLayout and implements ICheckable. When using the same layout with a MvxAdapter and an MvxListView the method OnCreateDrawableState is never called and the text and selection icon are never high-lighted.

我知道,所选择的项目可以存储在视图模型

I'm aware that the selected item could be stored in the ViewModel.

下面是原来的示例:
https://github.com/xamarin/monodroid-samples/tree/master/CustomChoiceList

推荐答案

在事实上MvxAdapter类充气列表项目布局到幕后MvxListItemView,所以你实际上得到解决您的列表项模板额外的FrameLayout。 MvxListItemView没有实现ICheckable,因此,信息是否检查的项目不传播。

In fact the MvxAdapter class inflates the list item layout into a MvxListItemView behind the scenes, so you actually get an additional FrameLayout around your list item template. MvxListItemView doesn't implement ICheckable and therefore the information whether to check the item is not propagated.

关键是要实现一个自定义MvxAdapter覆盖CreateBindableView并返回MvxListItemView的一个子类实现ICheckable。
您还必须设置的android:duplicateParentState =真正的 INT列表项模板的根(list_item.axml)

The trick is to implement a custom MvxAdapter overwriting CreateBindableView and return a subclass of MvxListItemView that implements ICheckable. You also must set the android:duplicateParentState="true" int the root of the list item template (list_item.axml)

您可以在这里找到完整的项目:
https://github.com/takoyakich/mvvmcross-samples/tree/master/CustomChoiceList

You can find the complete project here: https://github.com/takoyakich/mvvmcross-samples/tree/master/CustomChoiceList

以下相关的变化:

list_item.axml:

list_item.axml:

<?xml version="1.0" encoding="utf-8"?>
<customchoicelist.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
    android:duplicateParentState="true"
...

扩展MvxAdapter:

Extended MvxAdapter:

class MyMvxAdapter : MvxAdapter {
        private readonly Context _context;
        private readonly IMvxAndroidBindingContext _bindingContext;

        public MyMvxAdapter(Context c) :this(c, MvxAndroidBindingContextHelpers.Current())
        {
        }
        public MyMvxAdapter(Context context, IMvxAndroidBindingContext bindingContext) :base(context, bindingContext)
        {
            _context = context;
            _bindingContext = bindingContext;
        }
        protected override MvxListItemView CreateBindableView(object dataContext, int templateId)
        {
            return new MyMvxListItemView(_context, _bindingContext.LayoutInflater, dataContext, templateId);
        }
    }

扩展MvxListItemView:

Extended MvxListItemView :

class MyMvxListItemView : MvxListItemView, ICheckable
{

    static readonly int[] CHECKED_STATE_SET = {Android.Resource.Attribute.StateChecked};
    private bool mChecked = false;

    public MyMvxListItemView(Context context,
                           IMvxLayoutInflater layoutInflater,
                           object dataContext,
                           int templateId)
        : base(context, layoutInflater, dataContext, templateId)
    {         
    }

    public bool Checked {
        get {
            return mChecked;
        } set {
            if (value != mChecked) {
                mChecked = value;
                RefreshDrawableState ();
            }
        }
    }

    public void Toggle ()
    {
        Checked = !mChecked;
    }

    protected override int[] OnCreateDrawableState (int extraSpace)
    {
        int[] drawableState = base.OnCreateDrawableState (extraSpace + 1);

        if (Checked)
            MergeDrawableStates (drawableState, CHECKED_STATE_SET);

        return drawableState;
    }
}

这篇关于MvxListView可勾选列表项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆