Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add Fragments dynamically to ViewPager
List<Fragment> fragments = buildFragments();
ArrayList<String> categories = {"1", "2", "3", ..., "n"};
mPager = (ViewPager) v.findViewById(R.id.pager);
mPageAdapter = new MyFragmentPageAdapter(this,getSupportFragmentManager(), fragments, categories);
mPager.setAdapter(mPageAdapter);
//Add a new Fragment to the list with bundle
Bundle b = new Bundle();
b.putInt("position", i);
String title = "asd";
mPageAdapter.add(MyFragment.class, title, b);
mPageAdapter.notifyDataSetChanged();
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentPageAdapter extends FragmentPagerAdapter {
public static int pos = 0;
private List<Fragment> myFragments;
private ArrayList<String> categories;
private Context context;
public MyFragmentPageAdapter(Context c, FragmentManager fragmentManager, List<Fragment> myFrags, ArrayList<String> cats) {
super(fragmentManager);
myFragments = myFrags;
this.categories = cats;
this.context = c;
}
@Override
public Fragment getItem(int position) {
return myFragments.get(position);
}
@Override
public int getCount() {
return myFragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
setPos(position);
return categories.get(position);
}
public static int getPos() {
return pos;
}
public void add(Class<Fragment> c, String title, Bundle b) {
myFragments.add(Fragment.instantiate(context,c.getName(),b));
categories.add(title);
}
public static void setPos(int pos) {
MyFragmentPageAdapter.pos = pos;
}
}
private List<android.support.v4.app.Fragment> buildFragments() {
List<android.support.v4.app.Fragment> fragments = new ArrayList<android.support.v4.app.Fragment>();
for(int i = 0; i<categories.size(); i++) {
Bundle b = new Bundle();
b.putInt("position", i);
fragments.add(Fragment.instantiate(this,MyPagerFragment.class.getName(),b));
}
return fragments;
}
@devgripes

This comment has been minimized.

Copy link

@devgripes devgripes commented Aug 28, 2015

can you post MyFragment.class here?

@devgripes

This comment has been minimized.

Copy link

@devgripes devgripes commented Aug 28, 2015

i can't also pass bundle values using this code

@Krishansharma

This comment has been minimized.

Copy link

@Krishansharma Krishansharma commented Mar 29, 2016

Thanks

@kanchan2mj

This comment has been minimized.

Copy link

@kanchan2mj kanchan2mj commented May 4, 2016

Was Really Helpful :)

@SagarPanwala

This comment has been minimized.

Copy link

@SagarPanwala SagarPanwala commented May 10, 2016

I don't want to loose the page scrolled. I want to persist the scrolled page when I adds the new fragment to the end of the list. So it will not shows directly to user. Whenever user scrolls to the last item , he can understand that yeah there is new fragment added in the list.

@ArunCyberenue

This comment has been minimized.

Copy link

@ArunCyberenue ArunCyberenue commented Jan 19, 2017

Good

@Mukeshkannan-Nplus

This comment has been minimized.

Copy link

@Mukeshkannan-Nplus Mukeshkannan-Nplus commented Feb 26, 2017

thankyou

@MaheshCapcee

This comment has been minimized.

Copy link

@MaheshCapcee MaheshCapcee commented Mar 30, 2017

can you give me the link to download this project

@abanoubhannaazer

This comment has been minimized.

Copy link

@abanoubhannaazer abanoubhannaazer commented Jan 24, 2018

Thanks for the great code!

@rishadb7

This comment has been minimized.

Copy link

@rishadb7 rishadb7 commented Jun 5, 2018

Thanx for this code

ping me for complete source code
rishadb7@gmail.com

@GuyDviri

This comment has been minimized.

Copy link

@GuyDviri GuyDviri commented Sep 10, 2018

Can its possible to attach fragment to a viewPager after its create with gerFragmentManager()..beingTrans..()..add(simpleFra).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment