How do I attach to an "ItemClick" for `MvxLinearLayout`?

I have a ScrollView that originally wrapped two MvxListView .

The presence of ListView in ScrollView not supported by Android, although this makes sense because they both try to fill in the parent height and provide their own scrolling logic.

I want my ScrollView have two unsolicited lists with their full height. ListView MvxListView extends does not support this without manually breaking the height.

The reason I want this is because I have two separate lists that I linked to separate sources, and both have their own title. I need all this to scroll in one ScrollView .

Then I found MvxLinearLayout , which is the LinearLayout that has an ItemSource property that I can bind to. It works great, it shows my objects and gets the full height of all the elements, so I can scroll both of my lists in ScrollView . The problem is that it does not have an ItemClick property, so I have no way to get user input from my list.

Does anyone know a clean way to do this using a binder? I do not want to attach onItemClick handlers in my code. Is there another MvvmCross control that can do what I want?

+5
source share
3 answers

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

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 in the same way as to ListView :

 local:MvxBind="ItemClick SomeCommand" 
+10
source

You need to add the Click binding to the individual elements within the layout. You can add a Click on any layout, for example:

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

Have you tried to specify an element template for MvxLinearLayout? e.g. local:MvxItemTemplate="@layout/item_template" ? You can configure the MvvmCross click binding in the element template on the controls for which you want to handle clicks.

0
source

All Articles