Set the main layout file, set an IDfor the root layoutas the content area
Set The Click event to the ListView entry,setonitemclicklistener () method, Parameters: Context
The current Fragment implements the onitemclicklistener interface
Implementing the Onitemclick () method
Switch to determine the index position of the current menu, to instantiate different Fragment Objects according to the index
Call the getactivity () method, get the host activity object, this activity exposes a method, replaces Fragment
Use the keyword instances of judgment getactivity () to get an instance of the mainactivity class
Call the switchmenufragment () method of the host Activity object to toggle the content part of the fragment, parameter: Fragment Object
Host activity Exposure method switchmenufragment (), toggle content area fragment, Parameters: Fragment Object
Getsupportfragmentmanager (). BeginTransaction (). replace (r.id.ll_main_content, fragment). commit ();
Call the Toggle () method of the slidingmenu object to close the left menu
Set default to show the first content of navigation when the app opens for the first time
Mainactivity
Packagecom.qingguow.news;ImportAndroid.os.Bundle;Importandroid.support.v4.app.Fragment;Importandroid.support.v4.app.FragmentTransaction;ImportAndroid.view.Window;ImportCom.jeremyfeinstein.slidingmenu.lib.SlidingMenu;Importcom.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;Importcom.qingguow.news.fragment.IndexFragment;Importcom.qingguow.news.fragment.MenuFragment; Public classMainactivityextendsslidingfragmentactivity {Privateslidingmenu SM; @Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); //Setting the Slide menuSetbehindcontentview (R.layout.menu); Setcontentview (R.layout.activity_main); SM=Getslidingmenu (); Sm.setmode (Slidingmenu.left); Sm.setbehindwidth (200); Sm.setshadowdrawable (R.drawable.shape); Sm.setshadowwidth (5); Sm.settouchmodeabove (Slidingmenu.touchmode_fullscreen); Fragmenttransaction ft=Getsupportfragmentmanager (). BeginTransaction (); //Replace navigation fragemntMenufragment menufragment=Newmenufragment (); Ft.replace (R.id.ll_left_menu, menufragment); //Replace content fragmentIndexfragment index=Newindexfragment (); Ft.replace (r.id.ll_main_content, index). commit (); } /*** Exposure method Toggle Content Area *@paramFragment*/ Public voidswitchmenufragment (Fragment Fragment) {Getsupportfragmentmanager (). BeginTransaction (). Replace (R.id.ll_main_ Content, fragment). commit (); Sm.toggle (); }}
Menufragment.java
Packagecom.qingguow.news.fragment;Importjava.util.ArrayList;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.os.Bundle;Importandroid.support.annotation.Nullable;Importandroid.support.v4.app.Fragment;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListAdapter;ImportAndroid.widget.ListView;Importcom.qingguow.news.MainActivity;ImportCOM.QINGGUOW.NEWS.R; Public classMenufragmentextendsFragmentImplementsOnitemclicklistener {Privateview view; //Loading layout Files@Override PublicView Oncreateview (layoutinflater inflater, @Nullable viewgroup container, @Nullable Bundle savedinstancest ATE) {View= Inflater.inflate (R.layout.list_view,NULL); returnview; } //setting up the ListView data@Override Public voidonactivitycreated (@Nullable Bundle savedinstancestate) {Super. onactivitycreated (savedinstancestate); ListView ListView=(ListView) View.findviewbyid (R.id.lv_menu); List<String> Data=NewArraylist<string>(); Data.add (News); Data.add (Found); Data.add (Attention); ListAdapter Adapter=NewArrayadapter<>(Getactivity (), Android. R.layout.simple_list_item_1, Android. R.ID.TEXT1, data); Listview.setadapter (adapter); Listview.setonitemclicklistener ( This); } /*** Navigation Click*/@Override Public voidOnitemclick (adapterview<?> Parent, view view,intposition,LongID) {Fragment Fragment=NULL; Switch(position) { Case0: Fragment=Newindexfragment (); Break; default: Break; } if(fragment!=NULL) {Activity Activity=getactivity (); if(Activityinstanceofmainactivity) {(mainactivity) activity). Switchmenufragment (fragment); } } }}
[Android] News client implements left navigation click Toggle