如何为"MvxLinearLayout"绑定到"ItemClick"? [英] How do I bind to `ItemClick` for a `MvxLinearLayout`?

查看:97
本文介绍了如何为"MvxLinearLayout"绑定到"ItemClick"?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个ScrollView,它最初包装了​​两个MvxListView控件.

I have a ScrollView which originally wrapped two MvxListView controls.

尽管Android不支持在ScrollView中具有ListView控件,但这是有道理的,因为它们都试图填充父级高度并提供自己的滚动逻辑.

Having ListView controls in a ScrollView isn't supported by Android though, which makes sense, because they both try to fill the parent height and provide their own scrolling logic.

我想要的是两个不可滚动的列表,它们的全高位于我的ScrollView内.如果不手动修改高度,则MvxListView扩展的ListView不支持此功能.

What I want is two unscrollable lists with their full height inside my ScrollView. ListView which MvxListView extends doesn't support this without hacking the height manually.

我想要这样做的原因是因为我有两个单独的列表,这些列表已绑定到单独的源,并且它们都有自己的标头.我需要所有这些内容在一个ScrollView中可滚动.

The reason I want this is because I have two separate lists that I have bound to separate sources and they both have their own header. I need all of this to be scrollable within one ScrollView.

然后我发现MvxLinearLayout是可绑定的LinearLayout,它具有我可以绑定的ItemSource属性.它的效果非常好,它可以显示我的物品并获得所有物品的全高,因此我可以滚动ScrollView中的两个列表.问题在于它似乎没有ItemClick属性,因此我没有办法从列表中获取用户输入.

Then I found MvxLinearLayout which is a bindable LinearLayout which has an ItemSource property I can bind to. It works excellent, it shows my items and get the full height of all items so I can scroll both my lists in my ScrollView. The problem is that it doesn't seem to have an ItemClick property, so I don't have a way to get user input from my list.

有人知道一种可绑定的干净方法吗?我不想在我的代码后附加onItemClick处理程序.是否有另一个MvvmCross控件可以执行我想要的操作?

Does anyone know a clean way of doing this in a bindable manner? I don't want to attach onItemClick handlers in my code behind. Is there another MvvmCross control that can do what I want?

推荐答案

您可以扩展MvxLinearLayout以支持ItemClick:

You can extend MvxLinearLayout to support ItemClick:

public class MvxClickableLinearLayout : MvxLinearLayout
{
    public MvxClickableLinearLayout(Context context, IAttributeSet attrs)
        : this(context, attrs, new MvxClickableLinearLayoutAdapter(context))
    {
    }

    public MvxClickableLinearLayout(Context context, IAttributeSet attrs, MvxClickableLinearLayoutAdapter adapter)
        : base(context, attrs, adapter)
    {
        var mvxClickableLinearLayoutAdapter = Adapter as MvxClickableLinearLayoutAdapter;
        if (mvxClickableLinearLayoutAdapter != null)
        {
            mvxClickableLinearLayoutAdapter.OnItemClick = OnItemClick;
        }
    }

    public ICommand ItemClick { get; set; }

    public void OnItemClick(object item)
    {
        if (ItemClick != null && ItemClick.CanExecute(item))
        {
            ItemClick.Execute(item);
        }
    }
}

适配器:

public class MvxClickableLinearLayoutAdapter : MvxAdapterWithChangedEvent, View.IOnClickListener
{
    public delegate void ItemClickDelegate(object item);

    public ItemClickDelegate OnItemClick;

    public MvxClickableLinearLayoutAdapter(Context context)
        : base(context)
    {
    }

    public void OnClick(View view)
    {
        var mvxDataConsumer = view as IMvxDataConsumer;

        if (mvxDataConsumer != null && OnItemClick != null)
        {
            OnItemClick(mvxDataConsumer.DataContext);
        }
    }

    protected override View GetView(int position, View convertView, ViewGroup parent, int templateId)
    {
        View view = base.GetView(position, convertView, parent, templateId);
        view.SetOnClickListener(this);
        return view;
    }
}

现在,您可以像绑定ListView一样绑定到ItemClick:

Now you can bind to ItemClick just like you would do with a ListView:

local:MvxBind="ItemClick SomeCommand" 

这篇关于如何为"MvxLinearLayout"绑定到"ItemClick"?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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