How Can I Add Different Data To 3 Fragment In 3 Tabs?
i want to know that how can i add some different data like an array to each fragments that are in TabLayout? i mean that fragment1 show array1, fragment2 show array2 and fragment3
Solution 1:
you should use SetArguement for each of your fragments.
privatevoidsetupViewPager(ViewPager viewPager) {
ViewPagerAdapteradapter=newViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(newFragmentOne(), "پیامهای دریافتی",MY_ARRAY_1,MY_SEC_ARRAY_1);
adapter.addFragment(newFragmentTwo(), "پیامهای ارسالی",MY_ARRAY_2,MY_SEC_ARRAY_2);
adapter.addFragment(newFragmentThree(), "ارسال پیام",MY_ARRAY_3,MY_SEC_ARRAY_3);
viewPager.setAdapter(adapter);
}
then in your adapter :
publicvoidaddFragment(Fragment fragment, String title,Object data,ArrayList<MY_DATA_MODEL> array2) {
Bundle mBundle=newBundle();
mBundle.put("dataArray",data);
mBundle.put("dataArray2",array2);
fragment.setArgument(mBundle);
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
Now in each of your fragments, you should override setArguments and get your data from bundle there.
Don't forget to make your data model parcelable if you want to pass them in bundle and intents.
Solution 2:
view pager file
publicclassSamplePagerAdapterextendsFragmentPagerAdapter {
String[] title = {
"stringA",
"stringB",
"stringC",
}
publicSamplePagerAdapter(FragmentManager fm) {
super(fm);
}
@Overridepublic Fragment getItem(int position) {
/** Show a Fragment based on the position of the current screen */if (position == 0) {
return FragmentA.newInstance(title);
}
elseif(position==1)
{
returnnewFragmentB.newInstance(title);
}
else { return FragmentC.newInstance(title);}
}
@OverridepublicintgetCount() {
// Show 3 total pages.return3;
}
}
Fragment file
publicclassFragmentAextendsFragment {
publicstaticFragmentA newInstance(String [] s) {
FragmentA A = newFragmentA();
Bundle args = newBundle();
args.putStringArrayList("elist",s);
A.setArgumentas(args);
return A;
}
}
publicViewonCreateView(LayoutInflater inflater,
ViewGroup containerObject,
Bundle savedInstanceState){
//here is your argumentsBundle bundle=getArguments();
//here is your list array String[] myStrings=bundle.getStringArray("elist");
}
publicclassFragmentBextendsFragment {
publicstaticFragmentB newInstance(String [] s) {
FragmentB B = newFragmentB();
Bundle args = newBundle();
args.putStringArrayList("elist",s);
B.setArgumentas(args);
return B;
}
}
publicViewonCreateView(LayoutInflater inflater,
ViewGroup containerObject,
Bundle savedInstanceState){
//here is your argumentsBundle bundle=getArguments();
//here is your list array String[] myStrings=bundle.getStringArray("elist");
}
publicclassFragmentCextendsFragment {
publicstaticFragmentC newInstance(String [] s) {
FragmentC C = newFragmentC();
Bundle args = newBundle();
args.putStringArrayList("elist",s);
C.setArgumentas(args);
return C;
}
}
publicViewonCreateView(LayoutInflater inflater,
ViewGroup containerObject,
Bundle savedInstanceState){
//here is your argumentsBundle bundle=getArguments();
//here is your list array String[] myStrings=bundle.getStringArray("elist");
}
Post a Comment for "How Can I Add Different Data To 3 Fragment In 3 Tabs?"