2016-09-26 4 views
3

현재 Android 용 C# XAMARIN에서 View-pager 및 탭 레이아웃을 사용 중입니다. 이 탭 변경 이벤트에서 현재 탭 양식의 유효성을 검사하려고합니다.Xamrain 안드로이드에서 탭 변경 전 유효성 확인

유효하면 다음 탭을 선택하도록 허용하고 싶습니다. 이 모든 4 개의 탭에 대해 이렇게하고 싶습니다.

정상적인보기 호출기와 탭 레이아웃을 사용하고 있습니다. 어떤 문서 나 도움말 코드를 알려주십시오. 미리 감사드립니다 !!

아래 코드를 사용해 보았습니다.

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



    } 
} 

답변

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. 

    } 

}