12-02-1395، 07:12 ب.ظ
با سلام دوستان من میخوام توی sliding tab layout که دارم توی هر تب 3تا فرگمنت داشته باشم. روش کدنیسی که در زیر دارم ایا درست هست؟
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; } }