C# 在选项卡式xamarin.android应用程序中切换视图
我对Xamarin非常陌生,正在尝试一些测试应用程序;我已经创建了2个xaml视图,我想在用户单击选项卡按钮时在我的应用程序上显示这些视图。我知道我应该处理C# 在选项卡式xamarin.android应用程序中切换视图,c#,xamarin.android,C#,Xamarin.android,我对Xamarin非常陌生,正在尝试一些测试应用程序;我已经创建了2个xaml视图,我想在用户单击选项卡按钮时在我的应用程序上显示这些视图。我知道我应该处理OnNavigationItemSelected事件,但是我就是不知道如何调用视图并转到它。为每个视图尝试了一个新的活动,但是它会作为一个新的应用程序打开点击的窗口。需要帮忙吗 我使用底部导航视图作为示例 在资源/菜单中创建主\u bottom\u navigation.xml: <menu xmlns:android="http:/
OnNavigationItemSelected
事件,但是我就是不知道如何调用视图并转到它。为每个视图尝试了一个新的活动,但是它会作为一个新的应用程序打开点击的窗口。需要帮忙吗
我使用底部导航视图作为示例 在资源/菜单中创建主\u bottom\u 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(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;
}
}
如果您使用的是选项卡式视图,那么您应该处理片段,对吗?
[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;
}
}
<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>
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);
}
}