Fragments in MVVMCross 4.1.4 will not be displayed in the Activity (Android)

感情迁移 提交于 2019-12-12 06:37:06

问题


I try to add a fragment to an activity and have already looked at the example from Martijn00.

My problem is, that the activity will be displayed but the fragment will not and in debugging mode, the debugger is not running into the fragment's method OnCreateView. So the attribute of the fragment will be used, but not the fragment. I see only my activity with the green background. I am sure I do something wrong, but after 6 hours I do give up and ask you guys for help :)

Fragment:

namespace TopDownTodoList.Mobile.Droid.Views.Fragments{

[MvxFragment(typeof(MainViewModel), Resource.Id.content_frame, true)]
[Register("topdowntodolist.mobile.droid.views.fragments.TodoOverviewFragment")]
public class TodoOverviewFragment :  MvxFragment<TodoOverviewViewModel>
{
    protected int LayoutId => Resource.Layout.f_todo_overview_fragment;

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        this.Init();
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        return this.BindingInflate(this.LayoutId, null);
        //return inflater.Inflate(this.LayoutId, container, false);
    }

    public virtual void Init() { }
}}

Activity:

namespace TopDownTodoList.Mobile.Droid.Views{

[Activity(Label = "MainView")]
public class MainView : MvvmCross.Droid.FullFragging.Views.MvxActivity<MainViewModel>
{
    protected int LayoutId => Resource.Layout.main_view;

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

        SetContentView(this.LayoutId);

        this.Init();
    }

    public virtual void Init(){}
}}

App.cs:

        protected override void RegisterClasses()
    {
        RegisterAppStart<TodoOverviewViewModel>();
    }

ViewModels:

public class VMName : MvvmCross.Core.ViewModels.MvxViewModel{...}

FragmentLayout (f_todo_overview_fragment):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:mvx="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#0000aa"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_gravity="center"
    android:gravity="center"/></LinearLayout>

ActivityLayout (main_view):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:background="#aa0000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"><FrameLayout
  android:id="@+id/content_frame"
    android:background="#00bb00"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true" /></LinearLayout>

回答1:


Your Activity needs to be of the type MvxCachingFragmentCompatActivity or MvxCachingFragmentActivity to support hosting of fragments.

Secondly you need to ignore the normal inflator, and use binding inflate after that:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    var ignore = base.OnCreateView(inflater, container, savedInstanceState);
    return this.BindingInflate(FragmentId, null);
}


来源:https://stackoverflow.com/questions/37433704/fragments-in-mvvmcross-4-1-4-will-not-be-displayed-in-the-activity-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!