The effect is as follows:
Directly on the code, save for later use, the code directory structure is as follows:
Where Basefragment.java is the base class for another 5 fragment, the code is as follows:
PackageCom.xuliugen.newsclient.fragment.base;ImportAndroid.os.Bundle;ImportAndroid.support.v4.app.Fragment;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.TextView;/** * Fragment base class * * @author Xuliugen * */ Public class basefragment extends Fragment { @Override Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); }@Override PublicViewOncreateview(Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {TextView TextView =NewTextView (Getactivity ()); Textview.settext ( This. GetClass (). Getsimplename ());//Gets the class name corresponding to the current object returnTextView; } Public void onactivitycreated(Bundle savedinstancestate) {Super. onactivitycreated (Savedinstancestate); };}
The other 5 fragment code are the same is directly inherited Basefragment.java no implementation, the other 4 omitted
publicclass Fragment1 extends BaseFragment {}
Menufragment.java is used to manage these fragment.
PackageCom.xuliugen.newsclient.fragment;ImportJava.util.ArrayList;ImportJava.util.List;ImportAndroid.os.Bundle;ImportAndroid.support.v4.app.Fragment;ImportAndroid.util.Log;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.AdapterView;ImportAndroid.widget.AdapterView.OnItemClickListener;ImportAndroid.widget.ArrayAdapter;ImportAndroid.widget.ListView;Importcom.xuliugen.newsclient.MainActivity;ImportCOM.XULIUGEN.NEWSCLIENT.R;ImportCom.xuliugen.newsclient.fragment.base.BaseFragment;/** * Inherit fragment to implement your own method * * To populate the data in the side-slide Menu * * Onitemclicklistener:listview inside the Click event * * @author Xuliugen * */ Public class menufragment extends Fragment implements Onitemclicklistener {View view;PrivateString tag ="Menufragment";/** * Load data when activity is created */ @Override Public void onactivitycreated(Bundle savedinstancestate) {Super. onactivitycreated (Savedinstancestate); LOG.I (Tag,"onactivitycreated"); ListView ListView = (ListView) View.findviewbyid (R.id.list_view); arrayadapter<string> adapter =NewArrayadapter<string> (Getactivity (), Android. R.layout.simple_list_item_1, Android. R.id.text1, Inidata ()); Listview.setadapter (adapter);//Set adapterListview.setonitemclicklistener ( This); }/** * Static data of the sidebar * * @return * * PrivateList<string>Inidata() {list<string> List =NewArraylist<string> (); List.add ("Fragment1"); List.add ("Fragment2"); List.add ("Fragment3"); List.add ("Fragment4"); List.add ("FRAGMENT5");returnList }@Override Public void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); LOG.I (Tag,"OnCreate"); }/** * for loading sidebar * / @Override PublicViewOncreateview(Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {LOG.I (tag,"Oncreateview");//Loading interface: Side barView = Layoutinflater.from (Getactivity ()). Inflate (R.layout.list_view,NULL);returnView }/** * Click event Listener in ListView Select Listener Event */ @Override Public void Onitemclick(adapterview<?> parent, view view,intPositionLongID) {Basefragment basefragment =NULL;Switch(position) { Case 0: Basefragment =NewFragment1 (); Break; Case 1: Basefragment =NewFragment2 (); Break; Case 2: Basefragment =NewFragment3 (); Break; Case 3: Basefragment =NewFragment4 (); Break; Case 4: Basefragment =NewFRAGMENT5 (); Break; }/** * Let basefragment replace the interface * / / * * getactivity (). Getsupportfragmentmanager ()//Get Fragment Manager *. BeginTransaction ()//open things. Replac E (R.id.content_frag, Basefragment, * "Home")//replace, replace the data in menufragment with R.id.menu_frag. commit ();//Submit */
//switch selection, equivalent to a callback methodSwitchfragment (basefragment); }Private void switchfragment(Basefragment fragment) {if(Fragment! =NULL) {if(Getactivity ()instanceofmainactivity) {((mainactivity) getactivity ()). Switchfragment (fragment); } } }}
Mainactivity.java is primarily used for initializing slidingfragmentactivity operations, setting parameters, etc.
PackageCom.xuliugen.newsclient;ImportAndroid.os.Bundle;ImportAndroid.view.Window;ImportCom.jeremyfeinstein.slidingmenu.lib.SlidingMenu;Importcom.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity;ImportCom.xuliugen.newsclient.fragment.Fragment1;ImportCom.xuliugen.newsclient.fragment.MenuFragment;ImportCom.xuliugen.newsclient.fragment.base.BaseFragment;/** * Use the swipe menu to first inherit the slidingfragmentactivity, and then the general steps to implement are as follows: * * 1. Get Sliding Menu * * 2. Set the sliding menu to the left or the right to come out * 3. Set the sliding menu out of the content page, display the Remaining Width * * 4. Set the shadow of the slide menu to set the shadow, the shadow needs to be very light at the beginning, slowly darken * 5. Set the width of the shadow * * 6. Set the range of the slide menu * * @author Xuliugen * */ Public class mainactivity extends slidingfragmentactivity {Slidingmenu Slidingmenu;@Override Public void onCreate(Bundle savedinstancestate) {requestwindowfeature (window.feature_no_title);Super. OnCreate (Savedinstancestate); Setbehindcontentview (R.layout.menu);//load initialization of slidingfragmentSetcontentview (r.layout.content);//This is a dynamically loaded page //Set the initial value in content contentsFragment1 fragment1 =NewFragment1 (); Getsupportfragmentmanager ()//. BeginTransaction ()//. replace (R.id.content_frag, fragment1)//. commit ();// //1. Get sliding menuSlidingmenu = Getslidingmenu ();//2. Set the slide menu to the left or the right (left, right, around)Slidingmenu.setmode (Slidingmenu.left);//3. Set the slide menu out of the content page, showing the remaining widthSlidingmenu.setbehindwidthres (R.dimen.slidingmenu_offset);//4. Set the shadow of the slide menu to set the shadow, the shadow needs to be very light at the beginning, slowly darkenSlidingmenu.setshadowdrawable (R.drawable.shadow);//5. Set the width of the shadowSlidingmenu.setshadowwidth (R.dimen.shadow_width);//6. Set the range of the slide menu //Slidingmenu.touchmode_fullscreen: Fullscreen //Slidingmenu.touchmode_margin: Sliding at the edge //Slidingmenu.touchmode_none: Do not slideSlidingmenu.settouchmodeabove (Slidingmenu.touchmode_fullscreen);//Create FragmentmentMenufragment menufragment =NewMenufragment (); Getsupportfragmentmanager ()//Get the Fragment manager. BeginTransaction ()//Open things. replace (R.id.menu_frag, Menufragment,"Menu")//Replace to replace data in menufragment with R.id.menu_frag. commit ();//Submit}/** * Method callback: Call in Menufragment class * exposes the action of replacing the contents of the frame layout * @param fragment */ Public void switchfragment(Basefragment fragment) {Getsupportfragmentmanager ()//. BeginTransaction ()//. replace (R.id.content_frag, fragment)//. commit (); Slidingmenu.toggle ();//Click to switch automatically after clicking}}
The layout file is relatively simple, please refer to the project source code:
Link: Http://pan.baidu.com/s/1Eu6BG Password: uh3s
Android Open source framework for the use of slidingfragment