How can i manage layout file in tabbed activity in android? -
explanation:
i took tabbed activity while created project. in there single layout file fragments in viewpager.i want set different layout different fragment , add tabbed dynamically dynamic fragment.
suppose, have size 5 5 tab created,if have 2 create 2 tab.
here mainactivity.java in inner class also
public class mainactivity extends appcompatactivity { private sectionspageradapter msectionspageradapter; public viewpager mviewpager; string[] title={"shoes","bag","three","four","five","six","seven","eight","nine","ten"}; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); msectionspageradapter = new sectionspageradapter(getsupportfragmentmanager()); mviewpager = (viewpager) findviewbyid(r.id.container); msectionspageradapter.nooffragment(10); msectionspageradapter.addpagetitle(title); mviewpager.setadapter(msectionspageradapter); tablayout tablayout = (tablayout) findviewbyid(r.id.tabs); tablayout.setupwithviewpager(mviewpager); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { snackbar.make(view, "replace own action", snackbar.length_long) .setaction("action", null).show(); } }); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } public static class placeholderfragment extends fragment { private static final string arg_section_number = "section_number"; public placeholderfragment() { } public static placeholderfragment newinstance(int sectionnumber) { placeholderfragment fragment = new placeholderfragment(); bundle args = new bundle(); args.putint(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); return rootview; } } public class sectionspageradapter extends fragmentpageradapter { int val=0; list<string> pagetitle=new arraylist<>(); public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { return placeholderfragment.newinstance(position); } @override public int getcount() { // show 3 total pages. return val; } public void addpagetitle(string[] title){ for(int i=0;i<title.length;i++){ pagetitle.add(title[i]); } } @override public charsequence getpagetitle(int position) { return pagetitle.get(position); } public void nooffragment(int size){ val=size; } } }
in above code created dynamic tab.i want separate layout each tab.
please, me solve out.
you should create fragment each view , set view on onviewchanged
@override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { return inflater.inflate(r.layout.your_view, container, false); }
and should create custom adapter this
public class myadapter extends fragmentpageradapter { list<fragment> fragments; list<string> titles; public myadapter(fragmentmanager fm, list<fragment> fragments, list<string> titles) { super(fm); this.fragments = fragments; this.titles = titles; } @override public fragment getitem(int position) { return fragments.get(position); } @override public int getcount() { return fragments.size(); } @override public charsequence getpagetitle(int position) { return titles.get(position); } }
then activity should create list of fragments , pass adapter
myadapter myadapter = new myadapter(getsupportfragmentmanager(), fragmentlist, titlelist); viewpager viewpager = (viewpager) findviewbyid(r.id.viewpager); tablayout tablayout= (tablayout) findviewbyid(r.id.tabs); viewpager.setadapter(myadapter); tablayout.setupwithviewpager(viewpager);
Comments
Post a Comment