如果条件无效,则截取TabSelected - Android TabLayout

问题描述 投票:0回答:1

我试图拦截TabSelected中的EventHandler TabLayout,如果没有保存某些数据则显示一个Dialog。如果用户单击“是”,则会显示所选的新选项卡。如果不是,则显示旧选项卡(保持不变)。

如何显示上一个标签(如果条件无效)?:

MyActivity.cs

private int previousTabPosition =-1;
private TabLayout tabLayout;

...
OnCreate(){
tabLayout = FindViewById<TabLayout>(Resource.Id.tabbar);
tabLayout.SetupWithViewPager(pager);
tabLayout.TabSelected += TabLayout_TabSelected;
...
}
private void TabLayout_TabSelected(object sender, TabLayout.TabSelectedEventArgs e)
    {
        int selectedTab = e.Tab.Position;
    }

我的片段适配器:

public class MFragmentAdapter : FragmentStatePagerAdapter
{
    private const int BasePagesCount = 7;

    private FragmentManager fragmentManager;
    private Context context;
    private ItemViewModel Vm;
    private bool canSelect;
    private int pagesCount = BasePagesCount;
    private CustomViewPager viewPager;

    public InterventionFragmentAdapter(ItemViewModel Vm, bool canSelect, Context context, FragmentManager fm, CustomViewPager viewPager) : base(fm)
    {
        this.fragmentManager = fm;
        this.context = context;
        this.Vm = Vm;
        this.canSelect = canSelect;
        this.viewPager = viewPager;
        if (Vm.Troubleshooting())
        {
            pagesCount--;
        }
    }

    public override int Count
    {
        get { return pagesCount; }
    }

    public override Fragment GetItem(int position)
    {
        switch (position)
        {
            case 1:
                return FirstFragment.GetInstance(this.Vm);
            case 2:

                return SecondFragment.GetInstance(this.Vm);
            case 3:
               ....
            default:
                return DefaultFragment.GetInstance(this.Vm, this.canSelect);
        }
    }

    public override ICharSequence GetPageTitleFormatted(int position)
    {
        switch (position)
        {
            case 0:
                return new String(context.GetString(Resource.String.first_tab));
            case 1:
                return new String(context.GetString(Resource.second_tab));
            ....
            default:
                return new String("-");
        }
    }

    public void UpdateFragments(ItemViewModel Vm)
    {
        this.Vm = Vm;
        foreach (Fragment fragment in fragmentManager.Fragments)
        {
            if (fragment is IChangeListener)
            {
                (fragment as IChangeListener).OnObjectChanged(Vm);
            }
        }
    }


    public int GetFirstPageIndex()
    {
        if (pagesCount == BasePagesCount)
        {
            return 4;
        }
        else
        {
            return 3;
        }
    }


    public int GetSecondPageIndex()
    {
        if (pagesCount == BasePagesCount)
        {
            return 3;
        }
        return -1;
    }
...
    }

MyActivity.axml:

        <android.support.design.widget.TabLayout
        android:id="@+id/tabbar"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:layout_gravity="bottom"
        android:background="?attr/colorPrimary"
        app:tabContentStart="30dp"
        app:tabGravity="center"
        app:tabIndicatorColor="?attr/colorAccent"
        app:tabMode="scrollable"
        app:tabSelectedTextColor="?attr/colorAccent"
        app:tabTextAppearance="@style/TabLayoutTextAppearance"
        app:tabTextColor="@android:color/white"/>
        <Project.Views.Droid.CustomViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

谢谢您的帮助。

android xamarin xamarin.android android-tablayout
1个回答
1
投票

不要使用tabLayout.Tab Selected事件并拦截tabLayout.Click事件

创建一个SimpleFragmentPagerAdapter:

  public class SimpleFragmentPagerAdapter : FragmentPagerAdapter
    {

        private static string[] mTitles = { "tab1", "tab2", "tab3" };
        private TablayoutActivity tablayoutActivity;

        public SimpleFragmentPagerAdapter(Android.Support.V4.App.FragmentManager fm, TablayoutActivity tablayoutActivity) : base(fm)
        {
            this.tablayoutActivity = tablayoutActivity;
        }

        public override int Count => mTitles.Length;

        public override Android.Support.V4.App.Fragment GetItem(int position)
        {
            return PageFragment.newInstance(position + 1);
        }
        //custom Tab's style
        public View GetTabView(int position)
        {
            View tabView = LayoutInflater.From(tablayoutActivity).Inflate(Resource.Layout.tabitem, null);
            TextView textView = tabView.FindViewById<TextView>(Resource.Id.tv);
            textView.Text = mTitles[position];
            return tabView;
        }
    }

然后在OnCreate方法中:

protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.tablayout);
        // Create your application here
        pagerAdapter = new SimpleFragmentPagerAdapter(SupportFragmentManager, this);
        viewPager = (ViewPager)FindViewById(Resource.Id.viewpager);
        tabLayout = (TabLayout)FindViewById(Resource.Id.tabs);
        pagerAdapter = new SimpleFragmentPagerAdapter(SupportFragmentManager, this);
        viewPager.Adapter=pagerAdapter;
        tabLayout.SetupWithViewPager(viewPager);

        //Set the tablayout.Tab's style and intercept the tabLayout.Clicked event by use "tabView.SetOnTouchListener"
        for (int i = 0; i < tabLayout.TabCount; i++)
        {
            TabLayout.Tab tab = tabLayout.GetTabAt(i);
            if (tab != null)
            {
                tab.SetCustomView(pagerAdapter.GetTabView(i));
                if (tab.CustomView != null)
                {
                    View tabView = (View)tab.CustomView.Parent;
                    tabView.Tag=i;
                    tabView.SetOnTouchListener(this);
                }
            }
        }
    }

最后,在OnTouch方法中:

public bool OnTouch(View v, MotionEvent e)
    {
       if(e.Action == MotionEventActions.Down) { 
        int pos = (int)v.Tag;
        AlertDialog alertDialog = new AlertDialog.Builder(this)
                .SetTitle("notice")
                .SetMessage("Have you saved data?")
                //if no will stay current tab
                .SetNegativeButton("No", delegate
                {
                    return;
                })
                //if yes will display new tab
                .SetPositiveButton("Yes", delegate
                {
                    TabLayout.Tab tab = tabLayout.GetTabAt(pos);
                    if (tab != null)
                    {
                        tab.Select();
                    }
                })

                .Create();

        alertDialog.Show();
        }
        return true;
    }
© www.soinside.com 2019 - 2024. All rights reserved.