Recently want to get a double navigation function, see a lot of information, finally is the realization of the function, this side even to give yourself a few notes it!
Let's take a look at the effect
Then it starts to come true!
Bottom navigation bar I choose to use fragmenttabhost+fragment to achieve, this method I think very good, code volume is not much
The first is the beginning of the Activity_main.xml
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= "${relativepackage}.${activityclass}" > <framelayout android:id= "@+id/main_ View " android:layout_width=" match_parent " android:layout_height=" match_parent " android:layout_ Above= "@+id/main_tab" android:layout_alignparentleft= "true" android:layout_alignparenttop= "true" > </FrameLayout> <view android:id= "@+id/main_tab" android:layout_width= "Match_ Parent " android:layout_height=" 50DP " android:layout_alignparentbottom=" true " android:layout_ Alignparentleft= "true" class= "Android.support.v4.app.FragmentTabHost"/></relativelayout>
Where I was directly pulling the view so was formed by the Fragmenttabhost
You can also write directly in the XML file
<android.support.v4.view. Fragmenttabhost >
</Android.support.v4.view. Fragmenttabhost>
This XML file has a view plus a tab view to show fragmentation, and tab to place the number of buttons at the bottom
Again, it's tab_foot.xml.
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" Match_parent " android: Background= "#F6F6F6" android:gravity= "center" android:orientation= "vertical" > <imageview android:id= "@+id/foot_iv" android:layout_width= "wrap_content" android:layout_height= "wrap_content " android:src=" @drawable/home1 "/> <textview android:id=" @+id/foot_tv " android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:layout_margintop= "3DP " android:text= "Home" android:textcolor= "@color/tab_color"/></linearlayout>
This is the XML file for the layout settings of each bottom button
Displays the effect.
Again, it's mainactivity code.
Package Com.gjn.mynavigation;import Android.os.bundle;import Android.support.v4.app.fragmentactivity;import Android.support.v4.app.fragmenttabhost;import Android.view.layoutinflater;import Android.view.View;import Android.view.window;import Android.widget.imageview;import Android.widget.tabwidget;import Android.widget.textview;import Android.widget.tabhost.ontabchangelistener;import Android.widget.TabHost.TabSpec; public class Mainactivity extends Fragmentactivity implements Ontabchangelistener {private Fragmenttabhost mtabhost;@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (r.layout.activity_main);// Initialize Fragmenttabhostinithost ();//Initialize bottom navigation bar inittab ();//Default Check Mtabhost.ontabchanged (Tabdb.gettabstxt () [0]);} private void InitTab () {string[] tabs = Tabdb.gettabstxt (); for (int i = 0; i < tabs.length; i++) {//New Tabspectabspec TA Bspec = Mtabhost.newtabspec (Tabdb.gettabstxt () [i]);//Set VIEWVIEW view = Layoutinflater.from (this). Inflate (R.layout.tabs_foot, NULL);((TextView) View.findviewbyid (R.ID.FOOT_TV)). SetText (Tabdb.gettabstxt () [i]);((ImageView) View.findviewbyid (R.id.foot_iv)). Setimageresource (Tabdb.gettabsimg ( ) [i]); Tabspec.setindicator (view);//Join Tabspecmtabhost.addtab (Tabspec,tabdb.getframgent () [I],null];}} /*** * Initialize host */private void Inithost () {mtabhost = (fragmenttabhost) Findviewbyid (r.id.main_tab);//Call the Setup method Set Viewmtabhost.setup (this, Getsupportfragmentmanager (), r.id.main_view);//Remove Split Line Mtabhost.gettabwidget (). Setdividerdrawable (null);//Listener Event Mtabhost.setontabchangedlistener (this);} @Overridepublic void ontabchanged (String arg0) {//How many switching interfaces are obtained from the split line tabwidget TABW = Mtabhost.gettabwidget (); for (int i = 0; i < Tabw.getchildcount (); i++) {View v = tabw.getchildat (i); TextView TV = (TextView) V.findviewbyid (R.ID.FOOT_TV); ImageView IV = (ImageView) V.findviewbyid (R.ID.FOOT_IV);//Modify the current interface button color Picture if (i = = Mtabhost.getcurrenttab ()) { Tv.settextcolor (Getresources (). GetColor (R.colOr.tab_light_color)); Iv.setimageresource (Tabdb.gettabsimglight () [i]);} Else{tv.settextcolor (Getresources (). GetColor (R.color.tab_color)); Iv.setimageresource (tabdb.gettabsimg () [i]);}}}Where the Tabdb class is used to set the navigation bar of the data and picture when the resource is switched
The following is the Tabdb class
Package Com.gjn.mynavigation;public class Tabdb {/*** * get all bottom items */public static string[] Gettabstxt () {string[] tabs = {"Top Page "," Trade "," place "," my "};return tabs; /*** * Get all fragments */public static class[] Getframgent () {class[] cls = {Onefm.class,twofm.class,threefm.class,fourfm.class}; return CLS;} /*** * Get all pre-clicked pictures */public static int[] Gettabsimg () {int[] img = {R.DRAWABLE.HOME1,R.DRAWABLE.GLOD1,R.DRAWABLE.XC1, R.drawable.user1};return img;} /*** * get all clicked pictures */public static int[] Gettabsimglight () {int[] img = {R.DRAWABLE.HOME2,R.DRAWABLE.GLOD2,R.DRAWABLE.XC2 , R.drawable.user2};return img;}}
At this, the bottom navigation bar is fully implemented.
--------------------------------------------------------------------------------------------------------------- -----------
Now to implement the top navigation bar, see a lot of the last use of Radiogroup+viewpager to implement
The first one is to design an XML layout for the first fragment
Fm_one.xml
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" >
Set the top navigation bar and show view
After that, the layout of each item in the navigation barTab_rb.xml
<?xml version= "1.0" encoding= "Utf-8"? ><radiobutton xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" wrap_content " android:layout_height=" Wrap_content " android: background= "@drawable/tab_rb_selector" android:button= "@null" android:paddingbottom= "10DP" Android :p addingleft= "15DP" android:paddingright= "15DP" android:paddingtop= "10DP" android:text= "Today" > </RadioButton>
where you set the selector file to control the status of clicks and not clickedTab_rb_selector.xml
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android > <!--click <item android:state_checked= "true" > <layer-list > < Item > <shape android:shape= "Rectangle" > <stroke android:width= "5DP" android:color= "@color/tab _light_color "/> </shape> </item> <item android:bottom=" 5DP "> <shape Android:shape= "Rectangle" > <solid android:color= "#fff"/> </shape> </item> </layer-list> </item><!--default--><item > <shape > <solid Android:color= "#fafafa"/> </shape></item></selector>
Set the display state of the click and default
Finally to implement the ONEFM class
Package Com.gjn.mynavigation;import Java.util.arraylist;import Java.util.list;import android.os.bundle;import Android.support.annotation.nullable;import Android.support.v4.app.fragment;import Android.support.v4.view.viewpager;import Android.support.v4.view.viewpager.onpagechangelistener;import Android.util.displaymetrics;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.horizontalscrollview;import Android.widget.radiobutton;import Android.widget.radiogroup;import Android.widget.radiogroup.layoutparams;import Android.widget.radiogroup.oncheckedchangelistener;public class Onefm extends Fragment implements Onpagechangelistener {private View view;private radiogroup rg_;private viewpager vp_;private horizontalscrollview hv_; Private list<fragment> newslist = new arraylist<fragment> ();p rivate onefmadapter adapter; @Overridepublic View Oncreateview (Layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle SAVedinstancestate) {if (view = = null) {//Initialize Viewview = Inflater.inflate (R.layout.fm_one, container,false); rg_ = ( Radiogroup) View.findviewbyid (R.ID.ONE_RG); vp_ = (Viewpager) View.findviewbyid (r.id.one_view); hv_ = ( Horizontalscrollview) View.findviewbyid (R.ID.ONE_HV);//Set Radiogroup Click event Rg_.setoncheckedchangelistener (new Oncheckedchangelistener () {@Overridepublic void oncheckedchanged (radiogroup group, int id) {Vp_.setcurrentitem (id);}}); /Initialize top navigation bar inittab (inflater);//Initialize Viewpagerinitview ();} /* * Bottom navigation bar after switching because no top settings are destroyed cause if no reset view * Causes bottom toggle after switch back to top page data disappears etc Bug * the following settings re-create the view each time */viewgroup parent = (ViewGroup) view.g Etparent (); if (parent! = null) {Parent.removeview (view);} return view;} /*** * Initialize Viewpager */private void Initview () {listThere are two data classes and one fragment classData classes
Htab.java
Package com.gjn.mynavigation;/*** * Header Tab Property * */public class Htab {private String name;public htab (String name) {super (); t His.setname (name);} Public String GetName () {return name;} public void SetName (String name) {this.name = name;}}
Htabdb.java
Package Com.gjn.mynavigation;import Java.util.arraylist;import Java.util.list;public class Htabdb {private static Final listFragment class
Onefm1.java
Package Com.gjn.mynavigation;import Android.os.bundle;import Android.support.annotation.nullable;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.textview;public class OneFm1 extends Fragment {private String name;@ overridepublic void Setarguments (Bundle args) {name = args.getstring ("name");} @Overridepublic View Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle Savedinstancestate) {View view = Inflater.inflate (R.layout.fragment, Container,false);((TextView) View.findviewbyid ( R.id.fm_text)). SetText (name); return view;}}
This adds the top navigation bar to the first fragment and implements the toggle function.
Finally put Fragment.xml, is the most default display page of each fragment
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:gravity= "Center" android:orientation= "vertical" > <textview android:id= "@+id/fm_text" android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Large text" android: Textappearance= "? Android:attr/textappearancelarge"/></linearlayout>
Summarize:It's a note-taking record! A period of time has not been updated, because just graduated broke a time, walk and stop to leave some of their own records, afraid of their own later to write forget.
: Click to open link
android-implementation of the top + bottom Dual navigation interface function