c# - 如何在片段中添加选项卡 - Xamarin?

标签 c# xamarin xamarin.android

完整代码:Google Drive .

我使用现成的模板“抽屉导航”创建,如下图所示:

enter image description here

遵循代码:

Fragment2.cs

public class Fragment2 : Fragment
    {
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your fragment here
        }

        public static Fragment2 NewInstance()
        {
            var frag2 = new Fragment2 { Arguments = new Bundle() };
            return frag2;
        }


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

fragment2.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">
    <TextView
        android:text="@string/fragment2"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/textView1"
        android:gravity="center" />
</LinearLayout>

ma​​in.axml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">
  <!-- The main content view -->
  <RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="match_parent">
    <android.support.design.widget.AppBarLayout
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/toolbar_layout">
      <include
          android:id="@+id/toolbar"
          layout="@layout/toolbar"
          app:layout_scrollFlags="scroll|enterAlways" />
    </android.support.design.widget.AppBarLayout>
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_below="@id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
  </RelativeLayout>
  <android.support.design.widget.NavigationView
      android:id="@+id/nav_view"
      android:layout_width="wrap_content"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      app:headerLayout="@layout/nav_header"
      app:menu="@menu/nav_menu"
      android:fitsSystemWindows="true" />
</android.support.v4.widget.DrawerLayout>

MainActivity.cs

Android.Support.V4.App.Fragment fragment = null;
switch (position)
{
    case 0:
        fragment = Fragment1.NewInstance();
        break;
    case 1:
        fragment = Fragment2.NewInstance();
        break;
}

如何在片段中放置 2 个选项卡?如下图所示:

enter image description here

我创建了一个抽屉导航,每个菜单都有它的片段,这个片段有2个选项卡。我需要帮助,例如在 xamarin C# 中放置 2 个选项卡。

有什么解决方案可以在我的片段中放置 2 个选项卡吗?

最佳答案

片段 1:

using Android.OS;
using Android.Support.Design.Widget;
using Android.Support.V4.App;
using Android.Support.V4.View;
using Android.Views;
using Java.Lang;
using System.Collections.Generic;

namespace NavigationDrawerAndTabs.Fragments
{
    public class Fragment1 : Fragment
    {
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            // Create your fragment here
        }

        public static Fragment1 NewInstance()
        {
            var frag1 = new Fragment1 { Arguments = new Bundle() };
            return frag1;
        }


        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View inflate = inflater.Inflate(Resource.Layout.fragment1, container, false);

            ViewPager viewPager = inflate.FindViewById<ViewPager>(Resource.Id.viewPager);
            MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(ChildFragmentManager);
            viewPager.Adapter = adapter;

            //TabLayout 
            TabLayout tabLayout = inflate.FindViewById<TabLayout>(Resource.Id.sliding_tabs);
            tabLayout.SetupWithViewPager(viewPager);

            return inflate;
        }

        public class MyFragmentPagerAdapter : FragmentPagerAdapter
        {
            public string[] titles = new string[] { "Tab1", "Tab2" };
            List<Fragment> fgls = new List<Fragment>();
            public MyFragmentPagerAdapter(Android.Support.V4.App.FragmentManager fm)
                : base(fm)
            {
                fgls.Add(Fragment2.NewInstance());
                fgls.Add(Fragment3.NewInstance());
            }

            public override Android.Support.V4.App.Fragment GetItem(int position)
            {
                return fgls[position];
            }

            public override ICharSequence GetPageTitleFormatted(int position)
            {

                return new Java.Lang.String(titles[position]);
            }

            public override int Count
            {
                get { return titles.Length; }
            }


        }
    }


}

片段 1 的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

  <android.support.design.widget.TabLayout
      android:id="@+id/sliding_tabs"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      app:tabMode="fixed">

  </android.support.design.widget.TabLayout>
  <android.support.v4.view.ViewPager
      android:id="@+id/viewPager"
      android:layout_width="match_parent"
      android:layout_height="match_parent">
  </android.support.v4.view.ViewPager>
</LinearLayout>

片段2:

using Android.OS;
using Android.Support.V4.App;
using Android.Views;

namespace NavigationDrawerAndTabs.Fragments
{
    public class Fragment2 : Android.Support.V4.App.Fragment
    {
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your fragment here
        }

        public static Fragment2 NewInstance()
        {
            var frag2 = new Fragment2 { Arguments = new Bundle() };
            return frag2;
        }


        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            return inflater.Inflate(Resource.Layout.fragment2, container, false);
        }
    }
}

片段3:

using Android.OS;
using Android.Support.V4.App;
using Android.Views;

namespace NavigationDrawerAndTabs.Fragments
{
    public class Fragment3 : Fragment
    {
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Create your fragment here
        }

        public static Fragment3 NewInstance()
        {
            var frag3 = new Fragment3 { Arguments = new Bundle() };
            return frag3;
        }


        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            return inflater.Inflate(Resource.Layout.fragment3, container, false);
        }
    }
}

关于c# - 如何在片段中添加选项卡 - Xamarin?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49112823/

相关文章:

xamarin - "Unauthorized"Simple.OData.Client 通过使用来自 Xamarin PCL 的有效凭据访问 SharePoint REST API 的异常

c# - 在 Xamarin 中绑定(bind) .jar 时出现 Java 堆空间 OutOfMemoryError

c# - (Xamarin,Android)此代码如何帮助减少引用实例?

java - 遍历/转换表达式树

c# - 来自 Web 表单的 Web-API 调用 - AJax。不存在 'Access-Control-Allow-Origin' header

android - 错误 "ResolveLibraryProjectImports"任务意外失败

android - Visual Studio Mac - Android Build - Enable Developer Instrumentation 无法禁用

c# - DataGridView tab索引顺序里面的控件

c# - 不接收时检测端点关闭?

xamarin.forms - Xamarin Release Build 出现错误,无法解析 System.Void Xamarin.Forms.Log::Warning(System.String,System.String)