-
因为我们处理的是片段,所以我将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正在显示所选的片段/页面/选项卡,并通过平滑过渡相互更新。