android - Fragment duplicate tab Layout -
i have fragment have 3 tabs in, after came homefragment activity it's same have duplicate layout
please inform me if i'm doing wrong ?
homefragment.class
public class homefragment extends fragment { public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); } public view oncreateview(layoutinflater inflater , viewgroup container , bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_home,container,false); // [start tabs] toolbar toolbar = (toolbar) rootview.findviewbyid(r.id.toolbar_fragment); //toolbar.setnavigationicon(android.r.drawable.ic_menu_preferences); ((appcompatactivity) getactivity()).setsupportactionbar(toolbar); ((appcompatactivity) getactivity()).getsupportactionbar().setdisplayoptions(actionbar.display_show_custom); ((appcompatactivity) getactivity()).getsupportactionbar().setdisplayhomeasupenabled(true); viewpager = (viewpager) rootview.findviewbyid(r.id.fragmetn_viewpager); setupviewpager(viewpager); tablayout = (tablayout) rootview.findviewbyid(r.id.tabs_fragment); tablayout.setupwithviewpager(viewpager); //[*end tab ] return rootview; } private void setupviewpager(viewpager viewpager){ viewpageradapter adapter = new viewpageradapter(getchildfragmentmanager()); adapter.notifydatasetchanged(); adapter.addfragment(new tab1fragment() , "tab1"); adapter.addfragment(new tab2fragment() , "tab2"); adapter.addfragment(new tab3fragment() , "tab3"); viewpager.setadapter(adapter); } }
try as:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="wrap_content"> <android.support.design.widget.tablayout android:id="@+id/tabs" app:tabgravity="fill" app:tabmode="scrollable" android:background="@color/material_blue_grey_800" app:tabindicatorcolor="@color/orange" app:tabselectedtextcolor="@color/orange" app:tabtextcolor="@color/white" android:layout_width="match_parent" android:singleline="true" android:layout_height="wrap_content"> </android.support.design.widget.tablayout> <android.support.v4.view.viewpager android:id="@+id/viewpager" android:layout_width="match_parent" android:layout_height="match_parent"> </android.support.v4.view.viewpager> </linearlayout>
and:
public class tabfragment extends fragment { public tablayout tablayout; public viewpager viewpager; private static int currentitem = 0; myadapter adapter; public static tabfragment newinstance(int someint) { tabfragment frag = new tabfragment(); bundle args = new bundle(); args.putint("tab", someint); frag.setarguments(args); return frag; } @override public void onsaveinstancestate(bundle outstate) { super.onsaveinstancestate(outstate); outstate.putint("currentitem", viewpager.getcurrentitem()); } public static tabfragment newinstance() { tabfragment frag = new tabfragment(); return frag; } @override public void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); adapter=new myadapter(getchildfragmentmanager()); currentitem=getarguments() != null ? getarguments().getint("tab") : 0; } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { /** *inflate tab_layout , setup views. */ view x = inflater.inflate(r.layout.tab_layout,null); tablayout = (tablayout) x.findviewbyid(r.id.tabs); viewpager = (viewpager) x.findviewbyid(r.id.viewpager); /** *set adapter view pager */ if (adapter==null) adapter=new myadapter(getchildfragmentmanager()); viewpager.setadapter(adapter); viewpager.setcurrentitem( currentitem); /** * , workaround , * setupwithviewpager doesn't work without runnable . * maybe support library bug . */ tablayout.post(new runnable() { @override public void run() { tablayout.setupwithviewpager(viewpager); } }); return x; } class myadapter extends fragmentpageradapter { public myadapter(fragmentmanager fm) { super(fm); } /** * return fragment respect position . */ @override public fragment getitem(int position) { switch (position){ case 0 : return new tab1fragment(); case 1 : return new tab2fragment(); case 2 : return new tab3fragment(); } return null; } @override public int getcount() { return 3; } @override public charsequence getpagetitle(int position) { switch (position){ case 0 : return "tab1"; case 1 : return "tab2"; case 2 : return "tab3"; } return null; } }
Comments
Post a Comment