2

I need to find a view I declared in axml in a MvxFragment. In an Activity I can call FindViewById<T>(RESOURCE_ID) in OnCreate. This doesn't work in fragments.

This is how it works with normal Android fragments: findViewById in Fragment

How do I inflate a view in a MvxFragment without losing the binding context?

Community
  • 1
  • 1
j7nn7k
  • 16,709
  • 18
  • 76
  • 87

1 Answers1

7

This is how it works:

You need to know about the two extension methods EnsureBindingContextIsSet() and BindingInflate().

public class MyFragment : MvxFragment
{
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Android.OS.Bundle savedInstanceState)
    {
        this.EnsureBindingContextIsSet(savedInstanceState);
        var view = this.BindingInflate(Resource.Layout.YOUR_VIEW, container, false);
        var searchField = view.FindViewById<T>(Resource.Id.RESOURCE_ID);


        return view;
    }
}
j7nn7k
  • 16,709
  • 18
  • 76
  • 87