Late at the end of the week is basically no time to go home to write code, back home to want to sleep, the weekend has a rare time to write a blog, last wrote a slidemenu open source project Import issues, this time the main talk about the use of the problem, slidemenu application of the broad extent, needless to say, Basically is the app standard, about Slidemenu's various use method has many, online various demo also many, wants to go or is in accordance with own actual combat way to write it, passes the pit hoped that everybody basically will not encounter, starts the topic:
Basic Layout
Before writing the layout file, look at the final, red yesterday is the area of the slide, the right picture is triggered by the event on the left:
Layout in Activity_main.xml:
<framelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:id=" @+id/main_frame " android:layout_width=" Match_parent " android:layout_height= "Match_parent" android:paddingbottom= "@dimen/activity_vertical_margin" android: paddingleft= "@dimen/activity_horizontal_margin" android:paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools:context= " Com.example.slidemenu.MainActivity "> <listview android:id=" @+id/list_base " android:layout_ Width= "Match_parent" android:layout_height= "match_parent" > </ListView></FrameLayout>
The menu_frame.xml is mainly used to display the layout on the right, and one is written separately to facilitate later replacement:
<?xml version= "1.0" encoding= "Utf-8"? ><framelayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:id=" @+id /menu_frame "> </FrameLayout>
Slide_menu.xml is used primarily to display the data in the Slidemenu, and finally through the code area
<?xml version= "1.0" encoding= "Utf-8"? ><listview xmlns:android= "http://schemas.android.com/apk/res/ Android " android:id=" @+id/list_slide " android:layout_width=" match_parent " android:layout_height=" Match_parent "></ListView>
The layout file in List_item, and then the custom adapter will be used:
<?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= "Horizontal" > <imageview android:id= "@+id/list_image" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content "/> </LinearLayout>
Example Demo
First mainactivity import Open source project package (if the import has a problem can refer to my previous article) after inheriting slidingfragmentactivity;
To customize the class for instantiating Slide_menu.xml:
public class Mymenufragment extends Fragment implements Onitemclicklistener {private View view;private string[] Listarrst Rings=new string[]{"Eagle", "Heron", "Flamingo"}; @Overridepublic void OnCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stubsuper.oncreate (savedinstancestate);} @Overridepublic View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {//TODO Auto-generated Method Stubview=layoutinflater.from (Getactivity ()). Inflate (R.layout.slide_menu, null); return view;} @Overridepublic void onactivitycreated (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.onactivitycreated (savedinstancestate); ListView listview= (ListView) View.findviewbyid (R.id.list_slide); Listview.setadapter (New arrayadapter<string > (Getactivity (), Android. R.layout.simple_list_item_1, listarrstrings)); Listview.setonitemclicklistener (this);} @Overridepublic void Onitemclick (adapterview<?> parent, view view, int Position,long ID) {//TODO auto-generatedMethod Stubif (Getactivity () instanceof mainactivity) {((mainactivity) getactivity ()). Changeimage (position);}}
Called in the method in OnCreate:
Super.oncreate (savedinstancestate); Setbehindcontentview (r.layout.menu_frame); Setcontentview (R.layout.activity_ Main); slidingmenu = Getslidingmenu (); Slidingmenu.settouchmodeabove (Slidingmenu.touchmode_fullscreen); Slidingmenu.setmode (slidingmenu.left_right); Slidingmenu.setbehindwidth ; Mymenufragment menufragment = new Mymenufragment (); Getsupportfragmentmanager (). BeginTransaction (). Replace ( R.id.menu_frame, Menufragment, "MyMenu"). commit ();
Mymenufragment if the callback is called by the Mainactivity method:
Public void changeimage (int index) {listview view= (ListView) Findviewbyid (r.id.list_base), switch (index) { Case 0:view.setadapter (New Mylistadapter (this, r.drawable.eagle)); Break;case 1:view.setadapter (New Mylistadapter ( This, R.drawable.heron)); Break;case 2:view.setadapter (New Mylistadapter (this, R.drawable.flamingo)); Break;default: Break;}}
Finally, the custom mylistadapter:
public class Mylistadapter extends baseadapter{private int resourceID; private Layoutinflater inflater;public Mylistadap ter (Context Context,int resId) {inflater=layoutinflater.from (context); resourceid=resid;} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn 3;} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn null;} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn 0;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {//TODO auto-generated method Stubview vie W=null;if (convertview==null) { view=inflater.inflate (r.layout.list_item,null);} Else{view=convertview;} ImageView imageview= (ImageView) View.findviewbyid (r.id.list_image); Imageview.setimageresource (ResourceID); return View;} }
I just set the left slide, in fact, or set the right slide, set the left sliding through the Setsecondarymenu settings;
Finally see the Flamingo bar, foreigners or more love animals, the above are open source project Slidemenu in the picture:
Android Slidemenu Example Demo