问题描述
这是一个完整的项目:Google 云端硬盘.
Here is a complete project: 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.
If you select from the "List5" list in tab 1, load 5 rows in the gridview of tab 2 and navigate to tab 2.
如果从选项卡 1 的List9"列表中选择,则在选项卡 2 的网格视图中加载 9 行并导航到选项卡 2.
If you select from the "List9" list in tab 1, load 9 rows in the gridview of tab 2 and navigate to tab 2.
如果您从选项卡 1 的List2"列表中选择,则在选项卡 2 的网格视图中加载 2 行并导航到选项卡 2.
If you select from the "List2" list in tab 1, load 2 rows in the gridview of tab 2 and navigate to tab 2.
就这样……
我想说的是:当您点击标签按钮 1 时导航到标签 2.就这么简单.
What I'm trying to say is: navigate to tab 2 when you click on tab button 1. Simple like that.
有什么解决办法吗?
推荐答案
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
的 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
中,我使用单个实例来避免NullPointException
.
In Frag_Home_1
and Frag_Home_2_Tab
, I use single instance to avoid NullPointException
.
这篇关于从选项卡 1 中选择 ListView 后切换到选项卡 2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!