简体   繁体   English

如何为`MvxLinearLayout`绑定到'ItemClick`?

[英]How do I bind to `ItemClick` for a `MvxLinearLayout`?

I have a ScrollView which originally wrapped two MvxListView controls. 我有一个最初包含两个MvxListView控件的ScrollView

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. Android中不支持在ScrollView中使用ListView控件,这是有道理的,因为它们都试图填充父高度并提供自己的滚动逻辑。

What I want is two unscrollable lists with their full height inside my ScrollView . 我想要的是两个不可滚动的列表,它们在我的ScrollView的全高。 ListView which MvxListView extends doesn't support this without hacking the height manually. MvxListView扩展的ListView不支持此操作而不会手动破解高度。

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 . 我需要在一个ScrollView可以滚动所有这些。

Then I found MvxLinearLayout which is a bindable LinearLayout which has an ItemSource property I can bind to. 然后我找到了MvxLinearLayout ,它是一个可绑定的LinearLayout ,它有一个我可以绑定的ItemSource属性。 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 . 它工作得很好,它显示我的项目并获得所有项目的全部高度,因此我可以滚动我的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. 问题是它似乎没有ItemClick属性,所以我没有办法从我的列表中获取用户输入。

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. 我不想在后面的代码中附加onItemClick处理程序。 Is there another MvvmCross control that can do what I want? 还有另一个MvvmCross控件可以做我想要的吗?

You can extend MvxLinearLayout to support ItemClick : 您可以扩展MvxLinearLayout以支持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);
        }
    }
}

Adapter: 适配器:

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

Now you can bind to ItemClick just like you would do with a ListView : 现在,您可以像使用ListView一样绑定到ItemClick

local:MvxBind="ItemClick SomeCommand" 

You need to add a Click binding to the separate items inside the layout. 您需要将Click绑定添加到布局内的单独项目。 You can add a Click to any layout like this: 您可以将Click添加到任何布局,如下所示:

<RelativeLayout
    android:background="?android:attr/selectableItemBackground"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/relativeLayout1"
    local:MvxBind="Click SomeCommand">

Have you tried to specify an Item Template for the MvxLinearLayout? 您是否尝试为MvxLinearLayout指定项模板? eg, local:MvxItemTemplate="@layout/item_template" ? 例如, local:MvxItemTemplate="@layout/item_template" You can setup the MvvmCross Click binding inside the Item Template on the controls you want to handle clicks for. 您可以在要处理点击的控件上的项目模板内设置MvvmCross Click绑定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM