android - MvvmCross + Fragments + Back Button = 崩溃或控件不响应/绑定(bind)

标签 android google-maps android-fragments mvvmcross

编辑 我最初是作为一个问题问 2 个问题,但不清楚,我正在编辑它以关注更重要的问题。

我有一个使用 DrawerLayout 作为 FirstView (MvxFragmentActivity) 的应用程序,单击抽屉上的菜单项会导致 Activity 的内容切换到关联的 Fragment (MvxFragment)。在其中一个 fragment 中,我有一个显示信息站点标记的 Google map 。当应用程序首次启动时,或者如果我使用菜单进行导航,一切正常。但是如果我切换 fragment ,比如公司信息页面,然后点击硬件后退按钮(当每个 fragment 加载时,我调用 fragmentTransaction.AddToBackStack),然后尝试向 map 添加引脚,它实际上并没有添加引脚.从我能够做的挖掘来看,它似乎正在将重复的布局放入 fragment 中,而我绑定(bind)到错误的布局(BindingInflate 似乎正在放置重复项)。这是我的代码:

FirstView.axml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
<!-- The navigation drawer -->
<Mvx.MvxListView
    local:MvxBind="ItemsSource MenuItems; ItemClick SelectMenuItemCommand"
    local:MvxItemTemplate="@layout/drawer_menu_item"
    android:id="@+id/left_drawer"
    android:divider="@android:color/transparent"
    android:dividerHeight="0dp"
    android:choiceMode="singleChoice"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="@color/white"
    android:listSelector="@drawable/menu_background" />
</android.support.v4.widget.DrawerLayout>

map View .axml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<com.google.android.gms.maps.MapView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/main_map" />
</LinearLayout>

FirstView.cs(无论如何相关部分,基于来自 MvvmCross github 的 MvxFragmentActivity 演示):

    public bool Show(MvxViewModelRequest request)
    {
        Mvx.Trace(MvxTraceLevel.Diagnostic, "FirstView Show: {0}", request.ViewModelType.Name);
        var title = string.Empty;
        var section = ViewModelMapper.GetSectionForViewModel(request.ViewModelType);
        var fragType = ViewMapper.GetViewTypeForSection(section);
        if (IsCurrentFragmentTheGivenType(fragType))
        {
            return true;
        }

        title = GetFragmentTitle(section);

        MvxFragment frag = (MvxFragment)Activator.CreateInstance(fragType);
        var loaderService = Mvx.Resolve<IMvxViewModelLoader>();
        var viewModel = loaderService.LoadViewModel(request, null /* saved state */);

        frag.ViewModel = viewModel;

        var fragTrans = this.SupportFragmentManager.BeginTransaction();
        fragTrans.Replace(Resource.Id.content_frame, frag);
        if (!firstTime)
        {
            fragTrans.AddToBackStack(section.ToString());
        }
        fragTrans.SetTransition((int)FragmentTransit.FragmentOpen);
        fragTrans.Commit();

        return true;
    }

在我的 MapView fragment 代码中:

public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    var ignored = base.OnCreateView(inflater, container, savedInstanceState);
    var view = this.BindingInflate(layoutId, null);
    this.mapView = view.FindViewById<MapView>(Resource.Id.main_map);
    mapView.OnCreate(savedInstanceState);
    var map = mapView.Map;
    map.UiSettings.MyLocationButtonEnabled = true;
    map.MyLocationEnabled = true;
}

无论是否使用 this.RetainInstance = true; 我都试过了,但都无济于事。任何帮助将不胜感激。

最佳答案

EDIT 在原始答案下方添加了示例代码

我不确定您的设置是否正确。你的整体项目流程设置是这样的吗example?

一般设置是每个选项卡都是自己的 View 模型。我在自己的项目中启动并运行了它,并且没有遇到任何绑定(bind)不起作用的问题。听起来您没有正确处理后退按钮。如果您的 mapView 是它自己的选项卡,点击“后退”按钮应该退出应用程序,而不是将您带到不同的选项卡。

这是我的设置主选项卡设置的示例:

主视图.cs

public class MainView : MvxTabsFragmentActivity
{
    public MainViewModel MainViewModel
    {
        get { return (MainViewModel)ViewModel; }
    }

    public MainView() 
        : base(Resource.Layout.MainView, Resource.Id.actualtabcontent)
    {
    }

    protected override void AddTabs(Bundle args)
    {
        AddTab<TabView1>("Tab1", "Tab 1", args, MainViewModel.TabViewModel1);
        AddTab<TabView2>("Tab2", "Tab 2", args, MainViewModel.TabViewModel2);
        AddTab<TabView3>("Tab3", "Tab 3", args, MainViewModel.TabViewModel3);
    }
}

MainViewModel.cs

public class MainViewModel : BaseViewModel
{

    public BaseViewModel TabViewModel1 { get; set; }
    public BaseViewModel TabViewModel2 { get; set; }
    public BaseViewModel TabViewModel3 { get; set; }

    public MainViewModel()
    {
        TabViewModel1 = Mvx.IocConstruct<TabViewModel1>();
        TabViewModel2 = Mvx.IocConstruct<TabViewModel2>();
        TabViewModel3 = Mvx.IocConstruct<TabViewModel3>();
    }
}

主视图.axml

<?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:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TabHost
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <TabWidget
            android:id="@android:id/tabs"
            android:orientation="horizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0" />
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />
        <FrameLayout
            android:id="@+android:id/actualtabcontent"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />
    </LinearLayout>
</TabHost>
</LinearLayout>

TabView1.cs

public class TabView1 : MvxFragment
{

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

使用此设置,我不会丢失任何绑定(bind)或类似的东西。

关于android - MvvmCross + Fragments + Back Button = 崩溃或控件不响应/绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23772520/

相关文章:

android - Ormlite连接mysql数据库

javascript - 适用于多个地点的 Google Maps JavaScript API v3 : getDetails(request, 回调)

javascript - 如何在谷歌地图中找到经纬度为一个点的地址?

android - 我什么时候需要基础 Activity 和基础 fragment ?

安卓ViewPager : unable to switch pages onClick

android - 菜单未出现在 Fragment 中

android - 在android终端模拟器中执行python脚本

android - 如何在日历中添加提醒

java - 非 Activity 类通知不起作用

android - 如何修复关于 Google map 的新问题 "No virtual method zzts()"(Android)