android - How to create dynamically Fragment pager adapter -
i unable create dynamic layout each tabs, able create tabs dynamically constructor(public mypageradapter(fragmentmanager fm , int nooftabs)), coudn't inflate view each tabs creating each fragment should dyanmic.
public class mypageradapter extends fragmentpageradapter { private final string[] titles = { "categories", "home", "top paid", "top free", "top grossing", "top new paid", "top new free", "trending" }; int nooftabs; **public mypageradapter(fragmentmanager fm , int nooftabs) { super(fm); this.nooftabs = nooftabs; }** /*public mypageradapter(fragmentmanager fm) { super(fm); }*/ @override public charsequence getpagetitle(int position) { return titles[position]; } @override public int getcount() { return nooftabs; //titles.length; } @override public fragment getitem(int position) { return superawesomecardfragment.newinstance(position); } }
i facing problem here should dynamic , not static 3 fragments(fragmenta, fragmentb , fragmentc), can number of fragments depending on int nooftabs:
@override public fragment getitem(int position) { fragment fragment =null; switch (position) { case 0: fragment = new fragmenta(); break; case 1: fragment = new fragmentb(); break; case 2: fragment = new fragmentc(); break; } return fragment; }
thanks in advance if 1 me in this, using of library pagerslidingtabstrip.
you can storing list of fragments , titles in pageradapter
:
/** * tabspageradapter. * * @author niels * @version 1.0 * @since 11-9-2015 */ public class tabspageradapter extends fragmentpageradapter { /** * contains fragments. */ private list<fragment> fragments = new arraylist<>(); /** * contains tab titles. */ private list<string> tabtitles = new arraylist<>(); /** * creates new pageradapter instance. * * @param fragmentmanager fragmentmanager. */ public tabspageradapter(fragmentmanager fragmentmanager) { super(fragmentmanager); } @override public int getcount() { return fragments.size(); } @override public fragment getitem(int position) { return fragments.get(position); } @override public charsequence getpagetitle(int position) { return tabtitles.get(position); } /** * adds fragment list, adds fragment's tab title. * * @param fragment new instance of fragment associated tab. * @param tabtitle string containing tab title fragment. */ public void addfragment(fragment fragment, string tabtitle) { fragments.add(fragment); tabtitles.add(tabtitle); } }
you can use such:
@override public void oncreate() { adapter = new tabspageradapter(getsupportfragmentmanager()); //tabspageradapter = whatever named pageradapter addfragments(); viewpager.setadapter(adapter); //viewpager = viewpager view instance strip.setviewpager(viewpager); //strip = pagerslidingtabstrip view instance } /** * adds fragments adapter. */ private void addfragments() { adapter.addfragment(new fragment1(), "fragment1"); adapter.addfragment(new fragment2(), "fragment2"); adapter.addfragment(new fragment3(), "fragment3"); adapter.addfragment(new fragment4(), "fragment4"); }
Comments
Post a Comment