在选项卡式xamarin.android应用程序中的视图之间切换

时间:2019-03-05 12:25:08

标签: c# xamarin.android

我对Xamarin非常陌生,并尝试了一些测试应用程序;我创建了2个xaml视图,并希望在用户单击选项卡式按钮时在我的应用程序中显示这些视图。我知道我应该处理OnNavigationItemSelected事件,但是我根本不知道如何调用视图并转到视图。尝试为每个视图进行一个新的活动,但是它会以新应用的形式打开轻按的窗口。有什么帮助吗?

project layout here

1 个答案:

答案 0 :(得分:1)

我以 BottomNavigationView 为例,

资源/菜单中创建一个 main_bottom_navigation.xml

 <menu xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto">

    <item
      android:id="@+id/menu_contacts"
      android:enabled="true"
      android:icon="@mipmap/icon_contacts"
      app:showAsAction="ifRoom"
      android:title="联系人" />
   <item
      android:id="@+id/menu_discover"
      android:enabled="true"
      android:icon="@mipmap/icon_discover"
      app:showAsAction="ifRoom"
      android:title="发现" />
   <item
      android:id="@+id/menu_me"
      android:enabled="true"
      app:showAsAction="ifRoom"
      android:icon="@mipmap/me"
      android:title="我" />
 </menu>

activity_main.xaml 中:

<RelativeLayout 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">

  <FrameLayout
    android:id="@+id/ll_frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@+id/bv_bottomNavigation"
   />


 <android.support.design.widget.BottomNavigationView
    android:id="@+id/bv_bottomNavigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@color/write"
    app:itemIconTint="@drawable/bottom_navigation_item_selector"
    app:itemTextColor="@drawable/bottom_navigation_item_selector"
    app:menu="@menu/main_bottom_navigation" />
</RelativeLayout>

MainActivity.cs 中:

[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity,BottomNavigationView.IOnNavigationItemSelectedListener
{
    private BottomNavigationView mBottomNavigationView;

    private int lastIndex;
    List<Android.Support.V4.App.Fragment> mFragments;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.activity_main);
        initBottomNavigation();
        initFragments();

    }

    // Initialize the fragments
    public void initFragments()
    {
        mFragments = new List<Android.Support.V4.App.Fragment>();
        mFragments.Add(new ContactsFragment());
        mFragments.Add(new DiscoverFragment());
        mFragments.Add(new AccountFragment());
        // Initialization display ContactsFragment
        setFragmentPosition(0);

    }

    public void initBottomNavigation()
    {
        mBottomNavigationView = FindViewById<BottomNavigationView>(Resource.Id.bv_bottomNavigation);

        mBottomNavigationView.SetOnNavigationItemSelectedListener(this);
}

   //Show the corresponding fragment according to positon and hide the previous fragment
   private void setFragmentPosition(int position)
    {
      FragmentTransaction ft = SupportFragmentManager.BeginTransaction();
      Android.Support.V4.App.Fragment currentFragment = mFragments[position];
      Android.Support.V4.App.Fragment lastFragment = mFragments[lastIndex];
      lastIndex = position;
      ft.Hide(lastFragment);
      if (!currentFragment.IsAdded)
       {
        SupportFragmentManager.BeginTransaction().Remove(currentFragment).Commit();
        ft.Add(Resource.Id.ll_frameLayout, currentFragment);
       }
      ft.Show(currentFragment);
      ft.CommitAllowingStateLoss();
    }
    //Listen for Tab select by MenuItem's id
    public bool OnNavigationItemSelected(IMenuItem item)
    {
        switch (item.ItemId)
        {
            case Resource.Id.menu_contacts:
                setFragmentPosition(0);
                break;
            case Resource.Id.menu_discover:
                setFragmentPosition(1);
                break;
            case Resource.Id.menu_me:
                setFragmentPosition(2);
                break;
            default:
                break;
        }
        // Return true, otherwise click invalid
        return true;
    }
}

fragment_contacts.xaml 中:(其他与此类似的片段)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
   <TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="联系人"
    android:textColor="@color/theme"
    android:textSize="23sp" />
</LinearLayout>

ContactsFragment.cs 中:(其他与此类似的片段)

public class ContactsFragment : Android.Support.V4.App.Fragment
{
    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Use this to return your custom view for this Fragment
        // return inflater.Inflate(Resource.Layout.YourFragment, container, false);

        return inflater.Inflate(Resource.Layout.fragment_contacts, container, false);
    }
}
相关问题