带有选项卡的ViewPager中的嵌套片段

时间:2014-10-13 23:30:01

标签: android xamarin android-viewpager android-tabs android-nested-fragment

我们希望制作一个“类似Facebook的”导航。

我们有3个标签,带有ViewPager,所以这里我们有3个classifiedizedViewModel(见下文)。  每个页面都是TabRootFragment

public partial class MainView : MvxActionBarActivity, ActionBar.ITabListener

public class MvxFragmentTabPagerAdapter<TFragment> : FragmentPagerAdapter where TFragment : Fragment

private ViewPager pager;

protected override void OnCreate(Bundle bundle)
{
        ...
        this.SupportActionBar.NavigationMode = (int)ActionBarNavigationMode.Tabs;
        ...
        this.fragmentTabPagerAdapter = new MvxFragmentTabPagerAdapter<TabRootFragment>(this.SupportFragmentManager);
        foreach (var categorizedViewModel in this.MainViewModel.CategorizedViewModels)
        {
            this.AddPage(this.fragmentTabPagerAdapter, categorizedViewModel);
        }

        this.pager.Adapter = this.fragmentTabPagerAdapter;
        this.pager.SetOnPageChangeListener(new ViewPageListenerForActionBar(this.SupportActionBar));
}


    private void AddPage(MvxFragmentTabPagerAdapter<TabRootFragment> adapter, CategorizedViewModel categorizedViewModel)
    {
        Mvx.Trace("MainView => AddPage {0}", Enum.GetName(typeof(Category), categorizedViewModel.Category));
        var nestedFragement = new TestView();
        nestedFragement.ViewModel = categorizedViewModel;

        var fragment = new TabRootFragment(nestedFragement);
        adapter.AddFragment(fragment);

        ActionBar.Tab tab = this.SupportActionBar.NewTab();
        tab.SetIcon(CategoryHelper.GetDrawableId(categorizedViewModel.Category));
        tab.SetTabListener(this);
        this.SupportActionBar.AddTab(tab);
    }

    public void OnTabSelected(ActionBar.Tab tab, FragmentTransaction ft)
    {
        Mvx.Trace("MainView => OnTabSelected()");
        this.pager.SetCurrentItem(this.SupportActionBar.SelectedNavigationIndex, false);           
    }

我们为每个标签(viewPager的每个片段),一个具有framelayout的TabRootFragment:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <FrameLayout
        android:id="@+id/ChildFragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
</LinearLayout>

因此,我们想在这个TabRootFragment中添加一些TabChildFragments,以便有3个独立的片段堆栈(“facebook-like”:)

public class TabRootFragment : Fragment
{
    /// <summary>
    /// The root children
    /// </summary>
    public TabChildFragment rootChildren;

    /// <summary>
    /// Initializes a new instance of the <see cref="TabRootFragment"/> class.
    /// </summary>
    /// <param name="rootChildren">The root children.</param>
    public TabRootFragment(TabChildFragment rootChildren)
    {
        this.rootChildren = rootChildren;
    }

    /// <summary>
    /// Called when [create view].
    /// </summary>
    /// <param name="inflater">The inflater.</param>
    /// <param name="container">The container.</param>
    /// <param name="savedInstanceState">State of the saved instance.</param>
    /// <returns>View.</returns>
    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        Mvx.Trace("TabRootFragment => OnCreateView");
        return inflater.Inflate(Resource.Layout.TabRootFragment, container, false);
    }

    /// <summary>
    /// Called when [view created].
    /// </summary>
    /// <param name="view">The view.</param>
    /// <param name="savedInstanceState">State of the saved instance.</param>
    public override void OnViewCreated(View view, Bundle savedInstanceState)
    {
        Mvx.Trace(
            "TabRootFragment => OnViewCreated {0}",
            Enum.GetName(typeof(Category), this.rootChildren.ViewModel.Category));
        base.OnViewCreated(view, savedInstanceState);
        this.ChildFragmentManager.BeginTransaction()
            .Add(Resource.Id.ChildFragment, this.rootChildren)
            .Commit();
    }

在我们到达 TabRootFragment OnViewCreated 之前,似乎一切顺利。 当我们点击片段管理器事务时,我们会收到一个很好的异常:

 Java.Lang.IllegalStateException: Activity has been destroyed

似乎 TabRootFragment 未附加到活动。但它是由 OnTabSelected 方法中的SetCurrentItem设置为寻呼机......

如何通过ViewPager检查此片段是否与活动连接良好?

1 个答案:

答案 0 :(得分:0)

唐恩是对的,这与此有关:https://stackoverflow.com/a/15656428/5210:)

除了我的片段从未分离,并且需要直接从 TabRootFragment 的OnAttach方法应用修复。

奖励:Xamarin代码:

public class TabRootFragment : MvxFragment
{
    public override void OnAttach(Activity activity)
    {
        Mvx.Trace("TabRootFragment => OnAttach");
        base.OnAttach(activity);
        this.InitChildFragmentManagerState();
    }

    /// <summary>
    /// Initializes the state of the child fragment manager.
    /// </summary>
    private void InitChildFragmentManagerState()
    {
        var classRefProp = typeof(Fragment).GetProperty(
            "class_ref",
            BindingFlags.NonPublic | BindingFlags.Static);

        IntPtr classRef = (IntPtr)classRefProp.GetValue(this);
        var field = JNIEnv.GetFieldID(
            classRef,
            "mChildFragmentManager",
            "Landroid/support/v4/app/FragmentManagerImpl;");

        JNIEnv.SetField(this.Handle, field, IntPtr.Zero);
    }
}