我是android的新手,尤其是Xamarin Android C#。我有底部导航选项卡和片段。它们是fragment_picture(tab_picture),fragment home(tab_home),fragment通知(tab_notifications),fragment_profile(tab_profile)。在fragment_picture(tab_picture)中,我有2个选项卡。它们是fragment_camera(tab_camera)和fragment_gallery(tab_gallery)。我想移动fragment_picture的选项卡,例如,当我单击tab_camera时,它显示了tab_camera的XML,而当我单击tab_gallery时,它显示了tab_gallery的XML。
我不使用viewpager。我只是在使用片段。该选项卡不会将位置更改为其XML。我在Google中搜索了很多内容,但我仍然感到困惑。请帮助我
using System;
using Android.OS;
using Android.Support.Design.Widget;
using Android.Views;
using Android.Support.V4.App;
using Android.Widget;
namespace MyPhotographicMemory.Droid.Fragments
{
public class Fragment_Picture : Fragment
{
TabLayout tabLayout;
private TabLayout.Tab tabGallery, tabCamera;
Fragment fragment = null;
public override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
}
public static Fragment_Picture NewInstance()
{
var frag2 = new Fragment_Picture { Arguments = new Bundle() };
return frag2;
}
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
var v = inflater.Inflate(Resource.Layout.FragmentPicture, null);
tabLayout = v.FindViewById<TabLayout>(Resource.Id.pictureTabLayout);
tabLayout.TabSelected += (object sender, TabLayout.TabSelectedEventArgs e) =>
{
switch (e.Tab.Position)
{
case Resource.Layout.FragmentCamera: //Resource.Id.tabItem1
fragment = new Fragment_Camera();
TabLayout.Tab tab = tabLayout.GetTabAt(0);
tab.Select();
break;
case Resource.Layout.FragmentGallery: //Resource.Id.tabItem1
fragment = new Fragment_Gallery();
TabLayout.Tab tab1 = tabLayout.GetTabAt(1);
tab1.Select();
break;
}
};
return v;
}
}
}
您是否想获得像这样的GIF结果?
如果是这样,则应使用片段来实现嵌套的选项卡。
这是我的第一个片段中的代码。
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)
{
var ignored = base.OnCreateView(inflater, container, savedInstanceState);
var view = inflater.Inflate(Resource.Layout.fragment1, null);
BottomNavigationView bottomNavigationView= view.FindViewById<BottomNavigationView>(Resource.Id.mynested_bottom_navi);
bottomNavigationView.NavigationItemSelected += BottomNavigationView_NavigationItemSelected;
LoadFragment(Resource.Id.tab_camera);
return view;
}
private void BottomNavigationView_NavigationItemSelected(object sender, BottomNavigationView.NavigationItemSelectedEventArgs e)
{
LoadFragment(e.Item.ItemId);
}
//switch fragment by tab id.
void LoadFragment(int id)
{
Fragment fragment = null;
switch (id)
{
case Resource.Id.tab_gallery:
fragment = Fragment_Gallery.NewInstance();
break;
case Resource.Id.tab_camera:
fragment = Fragment_Camera.NewInstance();
break;
}
if (fragment == null)
return;
FragmentManager.BeginTransaction()
.Replace(Resource.Id.mynest_content_frame, fragment)
.Commit();
}
}
这里是我的演示,您可以参考它。https://drive.google.com/file/d/1XZE6YopdsvDHY8-DwXBxRpHz3oFlJbOR/view