代码之家  ›  专栏  ›  技术社区  ›  Gurmeet

Xamarin Android底部导航未在所有屏幕上显示

  •  0
  • Gurmeet  · 技术社区  · 7 年前

    我已经在XamarinAndroid中实现了底部导航。单击底部导航栏中的每个图标时,我需要显示不同的屏幕。 我在主要活动中工作得很好,但在选择底部导航项时,如何在保持底部导航可见的同时显示新屏幕。

    我在Xamarin Android上的经验很少,所以不知道是否有像布局页面这样的东西可以容纳底部导航。

    enter image description here

    enter image description here

    我读到 框架布局和片段

    2 回复  |  直到 7 年前
        1
  •  2
  •   Pooja Kamath    7 年前

    底部栏将添加到第一个活动中。如果更改活动,则在选择时,底部栏将消失。

    您需要添加带有片段的视图寻呼机来实现它

    你可以看看这个 tutorial

    您还可以看到此代码 sample

        2
  •  1
  •   FreakyAli    7 年前

    您可以按照以下步骤实现BottomNavigationView:

    • 因为我们处理的是片段,所以我将MainActivity基类更改为 Android.Support.V7.App.AppCompatActivity Android.Support.V7.App.AppActivity 是的,我们将使用Android支持库V4和V7。查看此博客以了解更多信息 understanding support libraries

      public class MainActivity : Android.Support.V7.App.AppCompatActivity
      
    • 为我们的MainActivity创建一个布局,我们将添加一个FrameLayout来保存片段、一个ViewPager和一个来自Android.Support.Design库的BottomNavigationView,如下所示:

       <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
      <FrameLayout
       android:id="@+id/fragment_content"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_above="@+id/bottom_navigation" />
      <android.support.v4.view.ViewPager
       android:id="@+id/viewpager"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_above="@id/bottom_navigation" />
      <android.support.design.widget.BottomNavigationView
       android:id="@+id/bottom_navigation"
       android:layout_width="match_parent"
       android:layout_height="56dp"
       android:layout_gravity="start"
       android:layout_alignParentBottom="true"
       android:background="@android:color/white"
       app:menu="@menu/navigation_main" />
      </RelativeLayout>
      
    • 为片段创建一个布局。我将通过图标和标签保持简单,您可以在此处保留您想要的任何内容:

      <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <ImageView
       android:id="@+id/imageView"
       android:src="@drawable/abc_ic_star_black_48dp"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true" />
       <TextView
       android:id="@+id/title"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_centerInParent="true"
       android:text="Fragment"
       android:textAlignment="center"
       android:textSize="@dimen/abc_text_size_display_1_material"
       android:layout_below="@id/imageView"
       android:layout_centerVertical="false" />
       </RelativeLayout>
      
    • 导航u main.xml //xml文件的名称

       <menu xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto">
      
       <item
         android:orderInCategory="0"
         android:id="@+id/menu_genres"
         android:enabled="true"
         android:title="Genres"
         android:icon="@drawable/tab_genres"
         app:showAsAction="always" />
      
        <item
          android:orderInCategory="1"
          android:id="@+id/menu_titles"
          android:enabled="true"
          android:title="Titles"
          android:icon="@drawable/tab_titles"
          app:showAsAction="always" />
      
         <item
          android:orderInCategory="2"
          android:id="@+id/menu_stream"
          android:enabled="true"
          android:title="Stream"
          android:icon="@drawable/tab_stream"
          app:showAsAction="always" />
      
       <item
          android:orderInCategory="3"
          android:id="@+id/menu_showtimes"
          android:enabled="true"
          android:title="Showtimes"
          android:icon="@drawable/tab_showtimes"
          app:showAsAction="always" />
        </menu>
      

      orderInCategory ,这是选项卡项从左到右的顺序。

      好的,现在我们把我们将要处理的视图放在一起。现在让我们进入底部导航视图。

      在MainActivity中,获取BottomNavigationView并进行设置。

        bottomNavigationView= FindViewById<BottomNavigationView>(Resource.Id.bottom_navigation);
      
    • RemoveShiftMode(_navigationView);//Below the FindViewById
      
      private void RemoveShiftMode(BottomNavigationView view)  // a Method in the Activity
      {   
      var menuView = (BottomNavigationMenuView) view.GetChildAt(0);
      try
      {
        var shiftingMode = menuView.Class.GetDeclaredField("mShiftingMode");
        shiftingMode.Accessible = true;
        shiftingMode.SetBoolean(menuView, false);
        shiftingMode.Accessible = false;
      
        for (int i = 0; i < menuView.ChildCount; i++)
        {
         var item = (BottomNavigationItemView)menuView.GetChildAt(i);
         item.SetShiftingMode(false);
         // set checked value, so view will be updated
         item.SetChecked(item.ItemData.IsChecked);
        }
        } catch (System.Exception ex) {
       System.Diagnostics.Debug.WriteLine((ex.InnerException??ex).Message);
       }
      }
      

      现在,我们应该没事了。就这样。我们已经完成了BottomNavigationView的设置。我们还没有为每个选项卡实现片段,因此点击选项卡不会显示任何内容。我们将在ViewPager中显示片段。

    • public class ViewPagerAdapter : FragmentPagerAdapter
      {
         Fragment[] _fragments;
      
        public ViewPagerAdapter(FragmentManager fm, Fragment[] fragments) : base(fm)
        {
          _fragments = fragments;
        }
      
        public override int Count => _fragments.Length;
      
        public override Fragment GetItem(int position) => _fragments[position];
      }
      
    • 然后,ViewPager,

      // find the view
      _viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
      
      // set the adapter
      _viewPager.Adapter = new ViewPagerAdapter(SupportFragmentManager, _fragments);
      

    • 当用户在ViewPager视图之间滑动时,将触发ViewPager.PageSelected事件。类似地,当用户点击BottomNavigationView中的选项卡(导航菜单项)时,将触发BottomNavigationView.NavigationItemSelected事件。我们必须将这两个事件链接起来,以使BottomNavigationView和ViewPager同步。下面是我们打算怎么做。

       // wireup the page selection event
       _viewPager.PageSelected += ViewPager_PageSelected; 
      
      // wire up the selection event 
       _navigationView.NavigationItemSelected += NavigationView_NavigationItemSelected;
      
    • 当选择ViewPager页面时,我们会通知BottomNavigationView,反之亦然。这样地,

         private void ViewPager_PageSelected(object sender, ViewPager.PageSelectedEventArgs e)
          {
           var item = _navigationView.Menu.GetItem(e.Position);
           _navigationView.SelectedItemId = item.ItemId;
          }
      
          void NavigationView_NavigationItemSelected(object sender, 
          BottomNavigationView.NavigationItemSelectedEventArgs e)
          {
            _viewPager.SetCurrentItem(e.Item.Order, true);
          }
      
    • 就这样。现在,BottomNavigationView和ViewPager正在显示所选的片段/页面/选项卡,并通过平滑过渡相互更新。

    最后一件事,加载这些选项卡的片段。

          void InitializeTabs()
     {
         _fragments = new Fragment[] {
         TheFragment.NewInstance("Genres", "tab_genres"),
         TheFragment.NewInstance("Titles", "tab_titles"),
         TheFragment.NewInstance("Stream", "tab_stream"),
         TheFragment.NewInstance("Showtimes", "tab_showtimes")
         };
     }