1

I have a View pager. The user can choose how many differents pages he can have. The pages are all the same layout but it will just load different data.

Here is my fragment adapter :

    public class FragmentAdapter extends FragmentPagerAdapter


{

    private final List<Fragment>  lstFragment = new ArrayList<>();
    private final List<String> lstTitles = new ArrayList<>();

    public FragmentAdapter(FragmentManager fm) {
        super(fm);
    }


    @Override
    public Fragment getItem(int i) {
        return lstFragment.get(i);
    }

    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return lstTitles.get(position);
    }

    @Override
    public int getCount() {
        return  lstTitles.size();
    }


    public void AddFragment (Fragment fragment , String title)
    {
        lstFragment.add(fragment);
        lstTitles.add(title);
    }
}

And here is the code to call the fragment multiple time :

 FragAdapter = new FragmentAdapter(getSupportFragmentManager());

    viewPager = (ViewPager) findViewById(R.id.main_tabs_pager);
    toolbar = (Toolbar) findViewById(R.id.main_page_toolbar);
    tabLayout = (TabLayout) findViewById(R.id.main_tabs);

    String[] Fragments = {"Frag1", "Frag2", "Frag3", "Frag4"};

    for (int i=0; i<Fragments.length; i++)
    {
        ((FragmentAdapter) FragAdapter).AddFragment(new MenuFragment(),Fragments[i]);
    }


    viewPager.setAdapter(FragAdapter);
    tabLayout = (TabLayout) findViewById(R.id.main_tabs);
    tabLayout.setupWithViewPager(viewPager);

So it works fine. But the only problem is that I don't know how to know the difference in code between the differents fragments.

Exemple : The frag1 must load 5 pictures about the sea The frag2 must load 8 pictures about the sun

How can I tell the fragment what to do? I tried to pass in the constructeur the arguments by exemple

 public MenuFragment(int numberofpictures, String picturesthemes)
{
    // Required empty public constructor
}

but the constructors must be empty because it is not called again when fragment is destroyed and recreated...

does anyone has an idea? thanks

UPDATE

I don't know if that is the good way but here is the way I did it :

In main activity I created :

 for (int i=0; i<Fragments.length; i++)
    {
        Bundle parameters = new Bundle();
        parameters.putInt("myInt", i);
        Fragment menuFragment = new MenuFragment();
        menuFragment.setArguments(parameters);
        ((FragmentAdapter) FragAdapter).AddFragment(menuFragment, Fragments[i]);
    }

Which give a everyfragment the the int i which is a reference to the title. Then I simply wrote this function :

 public String getName (int i)
{
    return Fragments[i];
}

which return the title based on the int that the fragment got thanks to the bundle

Then, In the MenuFragment() I used this :

 private void fillinthelist()
{

    myInt = getArguments().getInt("myInt");
    String test = ((MainActivity) getActivity()).getName(myInt);

    ListOfProgrammes.add(new Modele_carte(test));

}

so it gets the int from the bundle and make a like to it thanks to the function in MainActivity

Is it the good way to do it? It seems to work

ImNeos
  • 527
  • 6
  • 17
  • refer this question https://stackoverflow.com/questions/54780671/i-am-trying-to-add-dynamic-title-in-tablayout-but-on-basis-of-that-title-i-want/54787410#54787410 – Basha K Jun 20 '19 at 14:48
  • it doesn't seem to work, it tells me that I can call a method in a fragment from the activity but doesn't tell me how to be sure that the data passedm match with the title of the tablayout... – ImNeos Jun 20 '19 at 15:28
  • https://github.com/basha777/DynamicViewPagerWithSingleFragment did you check this example from the above link's solution. – Basha K Jun 20 '19 at 17:49

1 Answers1

0

You can attach a Bundle containing the parameters with setArguments(Bundle) :

Bundle parameters = new Bundle();
parameters.putInt("myInt", <int_value>);
Fragment menuFragment = new MenuFragment();
menuFragment.setArguments(arguments);
((FragmentAdapter) FragAdapter).AddFragment(menuFragment, Fragments[i]);

A common practice is to build and attach the Bundle in a fragment's class static factory method.

The fragment can use getArguments() to retrieve the parameters.

private int myInt;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    myInt = getArguments().getInt("myInt");
}
bwt
  • 17,292
  • 1
  • 42
  • 60
  • I used your technique and it seems to work. I updated the question can you check if you thing it's the correct way to proceed? – ImNeos Jun 20 '19 at 15:42
  • I usually get the arguments in `onCreate()` but it is perfectly fine to do it elsewhere. In fact it has nothing to to with the `ViewPager`, it is a generic way to transmit parameters to a fragment. – bwt Jun 20 '19 at 18:14
  • ok thank and is the bundle a linear way to pass data? I mean, it isn't async at all? If I a create the bundle for the fragment 1 then create the fragment 1 and then I create the bundle for fragment 2 and create fragment 2 ; there is no chance that fragment 2 intercept the data 1 ? – ImNeos Jun 21 '19 at 10:25
  • It is not async, each bundle is attached to a fragment instance, it is a bit like setting the values as instance fields except that the framework take care of the persistence – bwt Jun 21 '19 at 13:17