3
我目前在Android的C#XAMARIN中使用View-pager和Tab-layout。在此選項卡更改事件我想驗證當前選項卡窗體。在Xamrain中切換標籤之前進行驗證Android
如果它是有效的,那麼我想讓用戶選擇下一個選項卡。像這樣,我想爲所有四個選項卡執行此操作。
我使用正常的查看傳呼機和標籤佈局。請給我任何文件或任何幫助代碼。提前致謝!!
我試着用下面的代碼。
namespace xxxxxxxx
{
[Activity(Label = "xxxxx", MainLauncher = false)]
public class xxxxActivity : BaseActivity, TabLayout.IOnTabSelectedListener
{
private DrawerLayout mDrawerLayout;
private NavigationView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private NavigationView navigationView;
private string mDrawerTitle;
private string[] menuTitles;
int oldPosition = -1;
AppCompatNavigationService nav = ServiceLocator.Current.GetInstance<AppCompatNavigationService>();
protected override int LayoutResource
{
get
{
return Resource.Layout.xxxxxx;
}
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
mDrawerTitle = this.Title;
menuTitles = this.Resources.GetStringArray(Resource.Array.menu_array);
mDrawerLayout = FindViewById<DrawerLayout>(Resource.Id.drawer_layout);
mDrawerList = FindViewById<NavigationView>(Resource.Id.left_drawer);
mDrawerLayout.SetDrawerShadow(Resource.Drawable.drawer_shadow, GravityCompat.Start);
var fragments = new Android.App.Fragment[]
{
new xxxxxFragment(this),
new yyyyyyFragment(),
new zzzzzzFragment(),
new aaaaaaFragment()
};
var titles = CharSequence.ArrayFromStringArray(new[]
{
"xxxxx",
"yyyyyy",
"zzzzzz",
"aaaaaa"
});
SupportActionBar.Title = "";
SupportActionBar.SetHomeAsUpIndicator(Resource.Drawable.ic_menu);
mDrawerList.NavigationItemSelected += NavigationView_NavigationItemSelected;
mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawerLayout,
Resource.Drawable.ic_drawer,
Resource.String.drawer_open,
Resource.String.drawer_close);
mDrawerLayout.SetDrawerListener(mDrawerToggle);
var viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
viewPager.Adapter = new TabsFragmentPagerAdapter(FragmentManager, fragments, titles);
// Give the TabLayout the ViewPager
var tabLayout = FindViewById<TabLayout>(Resource.Id.sliding_tabs);
tabLayout.SetupWithViewPager(viewPager);
tabLayout.SetOnTabSelectedListener(this);
}
private void NavigationView_NavigationItemSelected(object sender, NavigationView.NavigationItemSelectedEventArgs e)
{
Title = e.MenuItem.TitleFormatted.ToString();
switch (e.MenuItem.ItemId)
{
case (Resource.Id.nav_aa):
////nav.NavigateTo(MainActivity.aaaaa);
break;
case (Resource.Id.nav_bb):
nav.NavigateTo(MainActivity.bbbbbb);
break;
case (Resource.Id.nav_cc):
nav.NavigateTo(MainActivity.cccccc);
break;
case (Resource.Id.nav_ss):
nav.NavigateTo(MainActivity.dddddd);
break;
case (Resource.Id.nav_ff):
nav.NavigateTo(MainActivity.eeeeee);
break;
case (Resource.Id.nav_ee):
nav.NavigateTo(MainActivity.ffffff);
break;
}
// Close drawer
mDrawerLayout.CloseDrawers();
}
public override bool OnOptionsItemSelected(IMenuItem item)
{
switch (item.ItemId)
{
case Android.Resource.Id.Home:
mDrawerLayout.OpenDrawer(Android.Support.V4.View.GravityCompat.Start);
return true;
}
return base.OnOptionsItemSelected(item);
}
public void OnTabReselected(TabLayout.Tab tab)
{
}
public void OnTabSelected(TabLayout.Tab tab)
{
ViewPager viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
viewPager.SetCurrentItem(tab.Position, false);
}
public void OnTabUnselected(TabLayout.Tab tab)
{
bool isValidationSuccess;
switch (tab.Position)
{
case 0:
isValidationSuccess = OnaaaaaTabSelected();
break;
case 1:
isValidationSuccess = OnbbbbbTabSelected();
break;
case 2:
isValidationSuccess = OncccccTabSelected();
break;
case 3:
isValidationSuccess = OnddddddTabSelected();
break;
default:
isValidationSuccess = false;
break;
}
if (!isValidationSuccess)
{
//Stay on same TAB
//TabLayout tabLayout = FindViewById<TabLayout>(Resource.Id.sliding_tabs);
//ViewPager viewPager = FindViewById<ViewPager>(Resource.Id.viewpager);
//viewPager.SetCurrentItem(tab.Position, false);
//tabLayout.SetScrollPosition(tab.Position, 0f, false);
}
}
}
}