简体   繁体   中英

How to swipe the same Fragment multiple times using ViewPager?

I want to use a ViewPager in order to swipe through Multiple Fragments , all this fragments are related to the same Layout so that I want to swipe the same Fragment multiple times like in the following pic:

在此处输入图片说明

as in the picture it's the same fragment but with different data assigned to it.

I'm using the regular page adapter:

public class LightPageAdapter extends FragmentStatePagerAdapter {
    public LightPageAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    public Fragment getItem(int i) {

        switch (i) {
        case 0:
            // Fragment for Center side
            return new Light_Center_frag();
        }
        return null;

    }

    @Override
    public int getCount() {

        return 1;

    }

}

I want to handle two points when doing that :

1- the number of fragments to swipe will be dynamic(got in the run_time).

2- I don't want to just change the page contents while swiping, but I do want to obtain the swiping sense as in the following image:

在此处输入图片说明

Without knowing how you obtain the data that you want displayed in the fragments, you could try to use a Bundle when instantiating the fragments.

Bundle args = new Bundle(); args.putExtra(KEY, someData);

and then

Fragment fragment = new Light_Center_frag();
fragment.setArguments(args);
return fragment;

In your fragment then you can call

getArguments();

to retrieve that Bundle and work with whatever is in there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM