每行带有按钮的MvxRecyclerView

每行带有按钮的MvxRecyclerView

本文介绍了每行带有按钮的MvxRecyclerView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Xamarin.Android和MvvmCross制作一个Android应用程序.我有一个MvxRecyclerView:

I am making an Android application with Xamarin.Android and MvvmCross. I have a MvxRecyclerView:

<MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@null"
    local:MvxItemTemplate="@layout/auto_complete_search_item"
    local:MvxBind="ItemsSource SearchAutoCompleteItems; ItemClick SearchAutoCompleteItemClick" />

还有我的auto_complete_search_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="@dimen/auto_complete_search_item_height"
android:orientation="horizontal">
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="?android:attr/selectableItemBackground"
        android:gravity="center_vertical"
        android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/margin_large"
        local:MvxBind="Text Title" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="@dimen/margin_large"
        local:MvxBind="Text Category" />
    </LinearLayout>
    <TextView
        android:id="@+id/fill_button"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textSize="@dimen/text_medium"
        android:textColor="@color/black"
        android:background="?android:attr/selectableItemBackground"
        android:gravity="center_vertical"
        local:MvxLang="Text fa-arrow-up"
        local:MvxBind="Style ., Converter=String,     ConverterParameter=fonts/fontawesome.ttf" />
</LinearLayout>

我的点击处理程序ItemClick SearchAutoCompleteItemClick可以工作,但是当用户分别单击我的fill_button时,我需要处理click事件.我该如何实现?

My click handler ItemClick SearchAutoCompleteItemClick works, but I need to handle the click event for when the user clicks on my fill_button separately. How can I achieve this?

推荐答案

我想出了如何使用自定义MvxRecyclerView和自定义MvxRecyclerAdapter进行此操作的方法:

I figured out how to do this using a custom MvxRecyclerView and custom MvxRecyclerAdapter:

public class TwoPieceMvxRecyclerView : MvxRecyclerView
{
    private ICommand _itemClickPiece1;
    private ICommand _itemClickPiece2;

    public ICommand ItemClickPiece1
    {
        get { return _itemClickPiece1; }
        set
        {
            if (ReferenceEquals(_itemClickPiece1, value))
            {
                return;
            }

            _itemClickPiece1 = value;
        }
    }

    public ICommand ItemClickPiece2
    {
        get { return _itemClickPiece2; }
        set
        {
            if (ReferenceEquals(_itemClickPiece2, value))
            {
                return;
            }

            _itemClickPiece2 = value;
        }
    }

    public TwoPieceMvxRecyclerView(Context context, IAttributeSet attr) : base(context, attr)
    {
    }

    public TwoPieceMvxRecyclerView(IntPtr javaReference, JniHandleOwnership transfer) : base(javaReference, transfer)
    {
    }
}

public class TwoPieceMvxRecyclerAdapter : MvxRecyclerAdapter, IOnClickListener
{
    public TwoPieceMvxRecyclerAdapter(IMvxAndroidBindingContext context) : base(context)
    {

    }

    public TwoPieceMvxRecyclerView TwoPieceMvxRecyclerView { get; set; }

    protected override Android.Views.View InflateViewForHolder(Android.Views.ViewGroup parent, int viewType, MvvmCross.Binding.Droid.BindingContext.IMvxAndroidBindingContext bindingContext)
    {
        var view = base.InflateViewForHolder(parent, viewType, bindingContext);

        var clickablePiece1 = view.FindViewById<View>(Resource.Id.clickable_piece1);
        var clickablePiece2 = view.FindViewById<View>(Resource.Id.clickable_piece2);

        clickablePiece1.SetOnClickListener(this);
        clickablePiece2.SetOnClickListener(this);

        return view;
    }

    public void OnClick(View v)
    {
        var viewHolder = this.TwoPieceMvxRecyclerView.FindContainingViewHolder(v);

        var item = GetItem(viewHolder.LayoutPosition); // What different is viewHolder.AdapterPosition? I tested it with 100 items and it's always the same, but I'm not sure if this will never break...

        if (v.Id == Resource.Id.clickable_piece1)
        {
                TwoPieceMvxRecyclerView.ItemClickPiece1.Execute(item);
        }
        else if (v.Id == Resource.Id.clickable_piece2)
        {
                TwoPieceMvxRecyclerView.ItemClickPiece2.Execute(item);
        }
    }
}

然后在包含的活动(或片段)中:

And then in the containing activity (or fragment):

protected override void OnCreate(Bundle bundle)
{
    base.OnCreate(bundle);

    SetContentView(Resource.Layout.activity_search);

    var r = FindViewById<TwoPieceMvxRecyclerView>(Resource.Id.my_twopiecemvxrecyclerview);
    var adapter = new TwoPieceMvxRecyclerAdapter(((IMvxAndroidBindingContext)BindingContext));
    adapter.TwoPieceMvxRecyclerView = r;
    r.Adapter = adapter;
}

并像这样使用它:

<My.Namespace.TwoPieceMvxRecyclerView
    android:id="@+id/my_twopiecemvxrecyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@null"
    local:MvxItemTemplate="@layout/my_two_piece_layout"
    local:MvxBind="ItemsSource MyItems; ItemClickPiece1 MyItemClick1; ItemClickPiece2 MyItemClick2" />

my_two_piece_layout.xml需要具有ID为clickable_piece1clickable_piece2

这篇关于每行带有按钮的MvxRecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 22:49