استفاده از 3 فرگمنت در یک sliding tab layout دارای 3 تب

ساخت وبلاگ
با سلام دوستان من میخوام توی 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); } retu myFragment; } @Override public CharSequence getPageTitle(int position) { retu tabs[position]; } @Override public int getCount() { retu 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); retu 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")); } retu 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); retu 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")); } retu 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); retu 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")); } retu layout; } }

برنامه نویس...
ما را در سایت برنامه نویس دنبال می کنید

برچسب : نویسنده : خنجی prog بازدید : 191 تاريخ : يکشنبه 12 ارديبهشت 1395 ساعت: 22:48

خبرنامه