在Xamarin Android中更改选项卡之前的验证

时间:2016-09-26 06:55:09

标签: c# android xamarin android-viewpager android-tablayout

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

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

我正在使用普通的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);
            }
        }



    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用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.

    }

}