i using navigation drawer display tabs have problems. link shows image of app ( dont have enough reputation post image)
http://imgur.com/3yk0dao,nyahjkh,mewcbht#0
in currentdata tab, use viewpage show tab. when choose other tab , currentdata tab again, have error.
here code:
mainactiviy.java
public class mainactivity extends fragmentactivity { private drawerlayout mdrawerlayout; private listview mdrawerlist; private actionbardrawertoggle mdrawertoggle; // nav drawer title private charsequence mdrawertitle; // used store app title private charsequence mtitle; // slide menu items private string[] navmenutitles; private typedarray navmenuicons; private arraylist<navdraweritem> navdraweritems; private navdrawerlistadapter adapter; viewpager viewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mtitle = mdrawertitle = gettitle(); // load slide menu items navmenutitles = getresources().getstringarray(r.array.nav_drawer_items); // nav drawer icons resources navmenuicons = getresources() .obtaintypedarray(r.array.nav_drawer_icons); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawerlist = (listview) findviewbyid(r.id.list_slidermenu); navdraweritems = new arraylist<navdraweritem>(); // adding nav drawer items array // location navdraweritems.add(new navdraweritem(navmenutitles[0], navmenuicons .getresourceid(0, -1))); // current data navdraweritems.add(new navdraweritem(navmenutitles[1], navmenuicons .getresourceid(1, -1))); // history navdraweritems.add(new navdraweritem(navmenutitles[2], navmenuicons .getresourceid(2, -1))); // graphic navdraweritems.add(new navdraweritem(navmenutitles[3], navmenuicons .getresourceid(3, -1))); // navdraweritems.add(new navdraweritem(navmenutitles[4], navmenuicons .getresourceid(4, -1))); // recycle typed array navmenuicons.recycle(); mdrawerlist.setonitemclicklistener(new slidemenuclicklistener()); // setting nav drawer list adapter adapter = new navdrawerlistadapter(getapplicationcontext(), navdraweritems); mdrawerlist.setadapter(adapter); // enabling action bar app icon , behaving toggle button getactionbar().setdisplayhomeasupenabled(true); getactionbar().sethomebuttonenabled(true); mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout, r.drawable.ic_drawer, // nav menu toggle icon r.string.app_name, // nav drawer open - description // accessibility r.string.app_name // nav drawer close - description // accessibility ) { public void ondrawerclosed(view view) { getactionbar().settitle(mtitle); // calling onprepareoptionsmenu() show action bar icons invalidateoptionsmenu(); } public void ondraweropened(view drawerview) { getactionbar().settitle(mdrawertitle); // calling onprepareoptionsmenu() hide action bar icons invalidateoptionsmenu(); } }; mdrawerlayout.setdrawerlistener(mdrawertoggle); if (savedinstancestate == null) { // on first time display view first nav item displayview(0); } } private class slidemenuclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // display view selected nav drawer item displayview(position); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // toggle nav drawer on selecting action bar app icon/title if (mdrawertoggle.onoptionsitemselected(item)) { return true; } // handle action bar actions click switch (item.getitemid()) { case r.id.action_settings: return true; default: return super.onoptionsitemselected(item); } } /* * * called when invalidateoptionsmenu() triggered */ @override public boolean onprepareoptionsmenu(menu menu) { // if nav drawer opened, hide action items boolean draweropen = mdrawerlayout.isdraweropen(mdrawerlist); menu.finditem(r.id.action_settings).setvisible(!draweropen); return super.onprepareoptionsmenu(menu); } /** * diplaying fragment view selected nav drawer list item * */ private void displayview(int position) { // update main content replacing fragments fragment fragment = null; switch (position) { case 0: fragment = new location(); break; case 1: fragment = new currentdatafragment(); break; case 2: fragment = new historydata(); break; case 3: fragment = new graphical(); break; case 4: fragment = new aboutus(); break; default: break; } if (fragment != null) { fragmentmanager fragmentmanager = getsupportfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.frame_container, fragment).commit(); // update selected item , title, close drawer mdrawerlist.setitemchecked(position, true); mdrawerlist.setselection(position); settitle(navmenutitles[position]); mdrawerlayout.closedrawer(mdrawerlist); } else { // error in creating fragment log.e("mainactivity", "error in creating fragment"); } } @override public void settitle(charsequence title) { mtitle = title; getactionbar().settitle(mtitle); } /** * when using actionbardrawertoggle, must call during * onpostcreate() , onconfigurationchanged()... */ @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); // sync toggle state after onrestoreinstancestate has occurred. mdrawertoggle.syncstate(); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); // pass configuration change drawer toggls mdrawertoggle.onconfigurationchanged(newconfig); } }
currentdatafragment.java
public class currentdatafragment extends fragment implements actionbar.tablistener { viewpager viewpager; tabscurrentpageradapter adapter; actionbar actionbar; private string[] tabs = {"sensor1","sensor2","sensor3"}; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.current_data_fragment, container,false); viewpager = (viewpager)view.findviewbyid(r.id.viewpage); actionbar = getactivity().getactionbar(); adapter = new tabscurrentpageradapter(getfragmentmanager()); viewpager.setadapter(adapter); actionbar.sethomebuttonenabled(true); actionbar.setdisplayhomeasupenabled(true); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); // adding tabs (string tab_name : tabs) { actionbar.addtab(actionbar.newtab().settext(tab_name) .settablistener(this)); viewpager.setonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpageselected(int arg0) { actionbar.setselectednavigationitem(arg0); } @override public void onpagescrolled(int arg0, float arg1, int arg2) { // todo auto-generated method stub } @override public void onpagescrollstatechanged(int arg0) { // todo auto-generated method stub } }); } return view; } @override public void ontabselected(tab tab, fragmenttransaction ft) { viewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(tab tab, fragmenttransaction ft) { // todo auto-generated method stub } @override public void ontabreselected(tab tab, fragmenttransaction ft) { // todo auto-generated method stub } }
if know problem, me, please.
i have work in things here project use swipe tab , navigation drawer @ once
this tab_layout.xml
<android.support.design.widget.tablayout android:id="@+id/tabs" app:tabgravity="fill" app:tabmode="fixed" 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: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>
this mainactivity
import android.os.bundle; import android.support.design.widget.navigationview; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.support.v4.widget.drawerlayout; import android.support.v7.app.actionbardrawertoggle; import android.support.v7.app.appcompatactivity; import android.view.menuitem; public class mainactivity extends appcompatactivity{ drawerlayout mdrawerlayout; navigationview mnavigationview; fragmentmanager mfragmentmanager; fragmenttransaction mfragmenttransaction; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); /** *setup drawerlayout , navigationview */ mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawerlayout); mnavigationview = (navigationview) findviewbyid(r.id.shitstuff) ; /** * lets inflate first fragment * here , inflating tabfragment first fragment */ mfragmentmanager = getsupportfragmentmanager(); mfragmenttransaction = mfragmentmanager.begintransaction(); mfragmenttransaction.replace(r.id.containerview,new tabfragment()).commit(); /** * setup click events on navigation view items. */ mnavigationview.setnavigationitemselectedlistener(new navigationview.onnavigationitemselectedlistener() { @override public boolean onnavigationitemselected(menuitem menuitem) { mdrawerlayout.closedrawers(); if (menuitem.getitemid() == r.id.nav_item_sent) { fragmenttransaction fragmenttransaction = mfragmentmanager.begintransaction(); fragmenttransaction.replace(r.id.containerview,new sentfragment()).commit(); } if (menuitem.getitemid() == r.id.nav_item_inbox) { fragmenttransaction xfragmenttransaction = mfragmentmanager.begintransaction(); xfragmenttransaction.replace(r.id.containerview,new tabfragment()).commit(); } return false; } }); /** * setup drawer toggle of toolbar */ android.support.v7.widget.toolbar toolbar = (android.support.v7.widget.toolbar) findviewbyid(r.id.toolbar); actionbardrawertoggle mdrawertoggle = new actionbardrawertoggle(this,mdrawerlayout, toolbar,r.string.app_name, r.string.app_name); mdrawerlayout.setdrawerlistener(mdrawertoggle); mdrawertoggle.syncstate(); } }
and tabfragment
import android.os.bundle; import android.support.annotation.nullable; import android.support.design.widget.tablayout; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmentpageradapter; import android.support.v4.view.viewpager; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; public class tabfragment extends fragment { public static tablayout tablayout; public static viewpager viewpager; public static int int_items = 3 ; @nullable @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 apater view pager */ viewpager.setadapter(new myadapter(getchildfragmentmanager())); /** * , workaround , * setupwithviewpager dose't works 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 primaryfragment(); case 1 : return new socialfragment(); case 2 : return new updatesfragment(); } return null; } @override public int getcount() { return int_items; } /** * method returns title of tab according position. */ @override public charsequence getpagetitle(int position) { switch (position){ case 0 : return "primary"; case 1 : return "social"; case 2 : return "updates"; } return null; } } }
this work me , simple.
Comments
Post a Comment