-
-
Save govind2/2b0c0adc151765c54421fc40ab58581e to your computer and use it in GitHub Desktop.
Add Fragments dynamically to ViewPager
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment