java - Add new fragment tab to existing tabbed activity on button click in Android -
i have tabbed activity named stage1.java, java class named stage1_fragment , sections_pager_adapter.java.
i want add new fragment tab existing viewpager on floating button clicked without refreshing other tabs.
stage1.java:
public class stage1 extends appcompatactivity implements stage1_fragment.stage1data{ private static final string tag = "stage1"; private sections_pager_adapter msectionspageadapter; private viewpager mviewpager; private arraylist valuestage1frag; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_stage1); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar_stage1); setsupportactionbar(toolbar); msectionspageadapter = new sections_pager_adapter(getsupportfragmentmanager()); mviewpager = (viewpager) findviewbyid(r.id.container_stage1); setupviewpager(mviewpager); tablayout tablayout = (tablayout) findviewbyid(r.id.tabs_stage1); tablayout.setupwithviewpager(mviewpager); floatingactionbutton fab = (floatingactionbutton) findviewbyid(r.id.fab); fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { //add tab on listener } }); } private void setupviewpager(viewpager viewpager){ sections_pager_adapter adapter = new sections_pager_adapter(getsupportfragmentmanager()); adapter.addfragment(new stage1_fragment(), "feasibility study"); adapter.addfragment(new planning_fragment(), "planning"); adapter.addfragment(new alignment_fragment(), "alignment"); adapter.addfragment(new cross_section_fragment(), "cross-section"); viewpager.setadapter(adapter); } @override public void sendstage1data(arraylist stage1data) { valuestage1frag = stage1data; }}
sections_pager_adapter.java
public class sections_pager_adapter extends fragmentpageradapter { private final list<fragment> mfragmentlist = new arraylist<>(); private final list<string> mfragmenttitlelist = new arraylist<>(); public void addfragment(fragment fragment,string title){ mfragmentlist.add(fragment); mfragmenttitlelist.add(title); } public sections_pager_adapter(fragmentmanager fm) { super(fm); } @override public charsequence getpagetitle(int position){ return mfragmenttitlelist.get(position); } @override public fragment getitem(int position) { return mfragmentlist.get(position); } @override public int getcount() { return mfragmentlist.size(); } }
stage1_fragment.java
public class stage1_fragment extends fragment { private static final string tag = "stage1_fragment"; private button btnstage1next; private stage1data interfaceimplementor; private arraylist<string> sendstage1data; public interface stage1data{ public void sendstage1data(arraylist senddata); } @override public void onattach(activity context){ super.onattach(context); this.interfaceimplementor = (stage1data)context; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_stage1, container, false); return view; } }
wiki
Comments
Post a Comment