【发布时间】:2017-07-24 16:39:29
【问题描述】:
我想制作一个在toolbar 顶部和下方有一个工具栏的活动,tablayout 有一个带有 3 个选项卡的滑动。
我想在每个标签中使用fragment。像这样:
tab1内的片段1
tab2 内的片段 2
tab3 内的片段 3
我该如何定义它。
下面是我自己的pagerAdapter 代码和3 个不同的fragment,但我不知道它是否正确:
class MyPagerAapter extends FragmentPagerAdapter {
String[] tabs;
public MyPagerAapter(FragmentManager fm) {
super(fm);
//tabs = getResources().getStringArray(R.array.tabs);
tabs = new String[]{"پیامهای دریافتی", "پیامهای ارسالی", "ارسال پیام"};
}
@Override
public Fragment getItem(int position) {
Fragment myFragment=null;
if (position == 0) {
myFragment = MyFragment0.getInstance(0);
}
if (position == 1) {
myFragment = MyFragment1.getInstance(1);
}
if (position == 2) {
myFragment = MyFragment2.getInstance(2);
}
return myFragment;
}
@Override
public CharSequence getPageTitle(int position) {
return tabs[position];
}
@Override
public int getCount() {
return 3;
}
}
public static class MyFragment0 extends Fragment {
private TextView textView;
public static MyFragment0 getInstance(int position) {
MyFragment0 myFragment = new MyFragment0();
Bundle args = new Bundle();
args.putInt("position", position);
myFragment.setArguments(args);
return myFragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_one, container, false);
textView = (TextView) layout.findViewById(R.id.position);
Bundle bundle = getArguments();
if (bundle != null) {
textView.setText("The Page Selected Is 0000000" + bundle.getInt("position"));
}
return layout;
}
}
public static class MyFragment1 extends Fragment {
private TextView textView;
public static MyFragment1 getInstance(int position) {
MyFragment1 myFragment = new MyFragment1();
Bundle args = new Bundle();
args.putInt("position", position);
myFragment.setArguments(args);
return myFragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_two, container, false);
textView = (TextView) layout.findViewById(R.id.position);
Bundle bundle = getArguments();
if (bundle != null) {
textView.setText("The Page Selected Is 1111111" + bundle.getInt("position"));
}
return layout;
}
}
public static class MyFragment2 extends Fragment {
private TextView textView;
public static MyFragment2 getInstance(int position) {
MyFragment2 myFragment = new MyFragment2();
Bundle args = new Bundle();
args.putInt("position", position);
myFragment.setArguments(args);
return myFragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_three, container, false);
textView = (TextView) layout.findViewById(R.id.position);
Bundle bundle = getArguments();
if (bundle != null) {
textView.setText("The Page Selected Is 22222222" + bundle.getInt("position"));
}
return layout;
}
}
}
【问题讨论】:
-
在 SO 和其他地方有很多这样的例子。我建议你对你的问题非常具体。
-
您所说的解决方案在哪里? @ReazMurshed