gpt4 book ai didi

c# - Xamarin Android 中选项卡更改前的验证

转载 作者:行者123 更新时间:2023-11-30 18:19:07 26 4
gpt4 key购买 nike

我目前在 C# XAMARIN for Android 中使用 View-pager 和 Tab-layout。在此 View 中,在选项卡更改事件中,我想验证当前选项卡表单。

如果它有效,那么我想允许用户选择下一个选项卡。所有四个选项卡都相同。

我正在使用普通的 View-pager 和 Tab-layout。请给我任何文档或任何帮助代码。提前致谢!!

我试过下面的代码。

 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);
}
}



}
}

最佳答案

您可以使用 OnTouch 事件。以下代码可能对您有所帮助。

public class XXXXDetailsActivity : BaseActivity, LinearLayout.IOnTouchListener
{

protected override void OnCreate(Bundle savedInstanceState)
{
// Give the TabLayout the ViewPager
var tabLayout = FindViewById<TabLayout>(Resource.Id.sliding_tabs);
tabLayout.SetupWithViewPager(viewPager);

//Get the Linear Layouts of the above Layout and Set On Touch Listener for each TABs
LinearLayout tabStrip = ((LinearLayout)tabLayout.GetChildAt(0));

if (tabStrip.ChildCount > 0)
{
for (int i = 0; i < tabStrip.ChildCount; i++)
{
tabStrip.GetChildAt(i).SetOnTouchListener(this);
tabStrip.GetChildAt(i).Clickable = false;
}
}
}

/// <summary>
/// On Tab Touch - Events will be fired on Touch of TAB
/// </summary>
/// <param name="v"></param>
/// <param name="e"></param>
/// <returns></returns>
public bool OnTouch(View v, MotionEvent e)
{
//Do logic here.

}

}

关于c# - Xamarin Android 中选项卡更改前的验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39696452/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com