我只是第一次(大约一个星期)使用Xamarin android(本机),我从这里到那里获取代码以了解其工作原理,但此刻我有些困惑。
我有一个带有TabLayout的导航视图。导航视图片段由SupportFragmentManager管理,我稍后将其传递给viewpager以将其他片段添加到我的布局中。
第二次从导航面板中选择一个菜单项时,就会出现我的问题。例如,如果我从MenuItem_1切换到MenuItem_2,则MenuItem_2的布局与我在Menu_Item1中看到的布局相同,但是选项卡标题发生了变化,工具栏标题和颜色也发生了变化,我唯一的问题是在选择其余一个MenuItem之后将显示与第一个相同的内容。
这是我的MainActivity代码:
using Android.App;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Graphics;
using MyApp.Fragments;
using Android.Support.Design.Widget;
using Android.Support.V4.Widget;
using Android.Support.V4.View;
using Android.Support.V7.App;
using Android.Content;
using TabIt
using MyApp.Fragments.Tabs;
using Android.Support.V4.App;
namespace MyApp
{
//[Activity(ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait, Label = "@string/app_name", MainLauncher = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTop)]
[Activity(ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait, MainLauncher = false, LaunchMode = Android.Content.PM.LaunchMode.SingleTop, Icon ="@drawable/icon")]
public class MainActivity : AppCompatActivity, View.IOnClickListener
{
int oldPosition = -1;
DrawerLayout drawerLayout;
NavigationView navigationView;
IMenuItem previousItem;
ImageView navMenuImg;
TabLayout tabLayout;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.drawer_layout);
var toolbar = FindViewById<Android.Support.V7.Widget.Toolbar>(Resource.Id.toolbar);
tabLayout = FindViewById<TabLayout>(Resource.Id.sliding_tabs);
if (toolbar != null)
{
SetSupportActionBar(toolbar);
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetHomeButtonEnabled(true);
}
drawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu_black);
navigationView = FindViewById<NavigationView>(Resource.Id.nav_view);
navMenuImg = FindViewById<ImageView>(Resource.Id.nav_image);
navigationView.NavigationItemSelected += (sender, e) =>
{
if (previousItem != null)
previousItem.SetChecked(false);
navigationView.SetCheckedItem(e.MenuItem.ItemId);
previousItem = e.MenuItem;
if (toolbar != null)
{
SupportActionBar.SetDisplayHomeAsUpEnabled(true);
SupportActionBar.SetHomeButtonEnabled(true);
}
switch (e.MenuItem.ItemId)
{
case Resource.Id.nav_home:
ListItemClicked(0);
break;
case Resource.Id.nav_item1:
ListItemClicked(0);
toolbar.Title = "MenuItem1";
break;
case Resource.Id.nav_item2:
ListItemClicked(1);
toolbar.Title = "MenuItem2";
break;
case Resource.Id.nav_loginItem:
if (toolbar != null)
{
SupportActionBar.SetDisplayHomeAsUpEnabled(false);
SupportActionBar.SetHomeButtonEnabled(false);
}
ListItemClicked(2);
break;
}
drawerLayout.CloseDrawers();
};
if (savedInstanceState == null)
{
navigationView.SetCheckedItem(Resource.Id.nav_MenuItem1);
ListItemClicked(0);
}
}
private void ListItemClicked(int position)
{
if (position == oldPosition)
return;
oldPosition = position;
Android.Support.V4.App.Fragment fragment = null;
Android.Support.V4.App.Fragment[] fragArray = null;
string[] titles = null;
switch (position)
{
case 0:
fragment = Fragment1.NewInstance();
break;
fragArray = new Android.Support.V4.App.Fragment[]
{
new TabFragment1(),
new TabFragment2(),
new TabFragment3()
};
titles = new string[]{"Item1Tab1","Item1Tab2","Item1Tab3" };
FnInitTabLayout(fragArray, titles);
case 1:
fragment = Fragment2.NewInstance();
fragArray = new Android.Support.V4.App.Fragment[]
{
new TabFragment1(),
new TabFragment2(),
new TabFragment3()
};
titles = new string[]{"Item2Tab1","Item2Tab2" };
FnInitTabLayout(fragArray, titles);
break;
case 2:
fragment = LoginFragment.NewInstance();
break;
}
SupportFragmentManager.BeginTransaction()
.Replace(Resource.Id.content_frame, fragment)
.Commit();
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Android.Resource.Id.MenuItem1:
drawerLayout.OpenDrawer(GravityCompat.Start);
return true;
}
return base.OnOptionsItemSelected(item);
}
void FnInitTabLayout(Android.Support.V4.App.Fragment[] tabs, string[] fragTitles)
{
tabLayout.SetTabTextColors(Android.Graphics.Color.Blue, Android.Graphics.Color.White);
var fragments = tabs;
var titles = CharSequence.ArrayFromStringArray(fragTitles);
var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
viewPager.Adapter = new TabsFragmentPagerAdapter(SupportFragmentManager, fragments, titles);
tabLayout.SetupWithViewPager(viewPager);
}
}
}
我的main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!--android:fitsSystemWindows="true"-->
<!-- The main content view -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="@+id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/toolbar"
layout="@layout/toolbar"
android:layout_alignParentTop="true"/>
<FrameLayout
android:id="@+id/content_frame"
android:layout_below="@id/toolbar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<android.support.design.widget.TabLayout
android:id="@+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
app:tabMode="fixed"
app:tabGravity="fill" />
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#ffffff" />
</android.support.design.widget.AppBarLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header"
app:menu="@menu/nav_menu"
android:fitsSystemWindows="true" />
</android.support.v4.widget.DrawerLayout>
还有我的VievPager适配器
using System;
using Android.Support.V4.App;
using Java.Lang;
namespace TabIt
{
public class TabsFragmentPagerAdapter : FragmentPagerAdapter
{
private readonly Fragment[] fragments;
private readonly ICharSequence[] titles;
public TabsFragmentPagerAdapter(FragmentManager fm, Fragment[] fragments, ICharSequence[] titles) : base(fm)
{
this.fragments = fragments;
this.titles = titles;
}
public override int Count
{
get
{
return fragments.Length;
}
}
public override Fragment GetItem(int position)
{
return fragments[position];
}
public override ICharSequence GetPageTitleFormatted(int position)
{
return titles[position];
}
}
}
我尝试在主要活动中调用的几乎所有资源中使用.Dispose()。我还对tabLayout进行了快速监视,以查看它具有多少个标签,计数是否正确。问题只是可视化。就像我提到的那样,一旦从“导航”菜单中选择一个MenuItem,就不必更改为另一个看到相同内容的MenuItem。
有人可以指出我正确的方向吗?还是至少看看有什么不对劲的地方?
一如既往地感谢您。