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

问题描述 投票:0回答:1

我对Xamarin非常新,并尝试了一些测试应用程序;我创建了2个xaml视图,我想在用户点击选项卡式按钮时在我的应用上显示这些视图。我知道我应该处理OnNavigationItemSelected事件,但是我根本无法弄清楚如何调用视图并转向它。为每个视图尝试了一个新活动,但是它将点击的窗口打开为一个新的应用程序。有什么帮助吗?

project layout here

c# xamarin.android
1个回答
1
投票

我在这里使用BottomNavigationView作为例子,

在Resources / menu中创建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>

在Contacts Fragment.vs中:(其他类似于此的片段)

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);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.