从选项卡1中选择ListView后切换到选项卡2

时间:2018-03-08 15:57:10

标签: android xamarin xamarin.android

这是一个完整的项目:Google Drive

关注代码:

Frag_Home_1_Tab.cs:

public class Frag_Home_1_Tab : Fragment
    {
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
        }

        public static Frag_Home_1_Tab NewInstance()
        {
            return new Frag_Home_1_Tab { Arguments = new Bundle() };
        }


        public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            View inflate = inflater.Inflate(Resource.Layout.frag_tab1, container, false);

            ListView mListView = inflate.FindViewById<ListView>(Resource.Id.listView);

            ViewPager viewPager = inflate.FindViewById<ViewPager>(Resource.Id.viewPager);

            mListView.ItemClick += (sender, e) =>
            {
                mListView.SetItemChecked(e.Position, true);

                // ** Here, how to navigate to tab 2 and refresh gridview **
            };

            ArrayAdapter<string> adapter = new ArrayAdapter<string>(Activity, Android.Resource.Layout.SimpleListItemActivated1, List());

            mListView.Adapter = adapter;

            // Set the ChoiceMode
            mListView.ChoiceMode = ChoiceMode.Single;

            // Select default
            mListView.SetItemChecked(0, true);

            return inflate;
        }

        private List<string> List()
        {
            List<string> list = new List<string>
            {
                "List 1",
                "List 2",
                "List 3",
                "List 4",
                "List 5",
                "List 6",
                "List 7",
                "List 8",
                "List 9"
            };

            return list;
        }
    }

frag_tab1:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ListView
        android:minWidth="25px"
        android:minHeight="25px"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView" />
</LinearLayout>

如果从选项卡1的“List5”列表中选择,则在选项卡2的网格视图中加载5行,然后导航到选项卡2.

如果从选项卡1的“List9”列表中选择,则在选项卡2的网格视图中加载9行,然后导航到选项卡2.

如果从选项卡1的“List2”列表中选择,则在选项卡2的gridview中加载2行,然后导航到选项卡2.

所以它会......

我想说的是:点击标签按钮1时导航到标签2.简单就是这样。

任何解决方案?

1 个答案:

答案 0 :(得分:1)

Frag_Home_1

public class Frag_Home_1 : Fragment
{
    TabLayout tabLayout;
    private static class SingletonHolder
    {
        public static Frag_Home_1 INSTANCE = new Frag_Home_1();
    }
    public static Frag_Home_1 getInstance()
    {
        return SingletonHolder.INSTANCE;
    }


    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View inflate = inflater.Inflate(Resource.Layout.frag_home_1, container, false);

        ViewPager viewPager = inflate.FindViewById<ViewPager>(Resource.Id.viewPager);
        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(ChildFragmentManager);
        viewPager.Adapter = adapter;

        //TabLayout 
        tabLayout = inflate.FindViewById<TabLayout>(Resource.Id.sliding_tabs);
        tabLayout.SetupWithViewPager(viewPager);

        return inflate;
    }

    public void toTab2()
    {
        TabLayout.Tab tab = tabLayout.GetTabAt(1);
        tab.Select();
    }

    public class MyFragmentPagerAdapter : FragmentPagerAdapter
    {
        public string[] titles = new string[] { "Tab1", "Tab2" };
        List<Fragment> fgls = new List<Fragment>();
        public MyFragmentPagerAdapter(FragmentManager fm) : base(fm)
        {
            fgls.Add(Frag_Home_1_Tab.NewInstance());
            fgls.Add(Frag_Home_2_Tab.getInstance());
        }

        public override Fragment GetItem(int position)
        {
            return fgls[position];
        }

        public override ICharSequence GetPageTitleFormatted(int position)
        {

            return new String(titles[position]);
        }

        public override int Count
        {
            get { return titles.Length; }
        }
    }

}

Frag_Home_1_Tab的{​​{1}}方法:

OnCreateView

mListView.ItemClick += (sender, e) => { mListView.SetItemChecked(e.Position, true); Frag_Home_1.getInstance().toTab2(); Frag_Home_2_Tab.getInstance().updateGrid(e.Position); // ** Here, how to navigate to tab 2 and refresh gridview ** };

Frag_Home_2_Tab

public class Frag_Home_2_Tab : Fragment,adapterUpdate { ArrayAdapter<string> adapter; List<string> numbers = new List<string>(); GridView grid; public override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); } private static class SingletonHolder { public static Frag_Home_2_Tab INSTANCE = new Frag_Home_2_Tab(); } public static Frag_Home_2_Tab getInstance() { return SingletonHolder.INSTANCE; } public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { initData(9); View view = inflater.Inflate(Resource.Layout.frag_tab2, container, false); grid = (GridView)view.FindViewById(Resource.Id.gridview); grid.ChoiceMode = ChoiceMode.Single; grid.ItemClick += (sender, e) => { grid.SetItemChecked(e.Position, true); }; adapter = new ArrayAdapter<string>(Activity, Resource.Layout.item_tab_2, numbers); grid.Adapter = adapter; return view; } private void initData(int count) { numbers.Clear(); for (int i = 0; i < count; i++) { numbers.Add((i + 1).ToString()); } } public void updateGrid(int count) { initData(count+1); adapter = new ArrayAdapter<string>(Activity, Resource.Layout.item_tab_2, numbers); grid.Adapter = adapter; } } public interface adapterUpdate { void updateGrid(int count); } Frag_Home_1中,我使用单个实例来避免Frag_Home_2_Tab