Android drawerlayout Drawer
Drawerlayout in SupportV4 Lib, like the open source Slidemenu, the Drawerlayout parent class is ViewGroup, and the custom component basically extends the class.
Android.support.v4.widget.DrawerLayout
The following is a simple usage demonstration. Click the button on the top left corner to open the drawer menu.
Click on the corresponding item to toggle the corresponding content, the content is displayed using fragment, here is useless to Actionbar to do the switch
<?xml version= "1.0" encoding= "Utf-8"?><android.support.v4.widget.DrawerLayoutxmlns:android="http://schemas.android.com/ Apk/res/android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools ="Http://schemas.android.com/tools"android:id="@+id/drawer_layout"android: Layout_width="Match_parent"android:layout_height="Match_parent" > <!--Here is the content area -- <linearlayoutandroid:layout_width="Match_parent"android:layout_height ="Wrap_content"android:orientation="vertical" > <linearlayoutandroid:layout_width="Match_parent"android:layout_ Height="55DP"android:background="#9370DB"android:orientation ="Horizontal" > <ImageViewandroid:id="@+id/slide"android:layout_width="48DP" android:layout_height="48DP"android:scaletype="Fitcenter"Android : layout_gravity="center_vertical"android:src = "@drawable/Slide "/> </linearlayout> <framelayoutandroid:id="@+id/fragment_container"android:layout_width ="Match_parent"android:layout_height="Match_parent" > </framelayout> <!--drawer contents -- <listview android:id = "@+id/drawer_content" android:layout_width = "280DP" android:layout_height = "match_parent" android:listselector =" #336699 " android:divider =" # B0C4DE " android:dividerheight =" 1px " android:layout_gravity = "start" android:background =/> </android.support.v4.widget.DrawerLayout>
ImportAndroid.content.res.Configuration;ImportAndroid.os.Bundle;ImportAndroid.support.v4.app.Fragment;Importandroid.support.v4.app.FragmentActivity;ImportAndroid.support.v4.widget.DrawerLayout;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.View;ImportAndroid.view.View.OnClickListener;ImportAndroid.view.ViewGroup;ImportAndroid.widget.AdapterView;ImportAndroid.widget.BaseAdapter;ImportAndroid.widget.ListView;ImportAndroid.widget.TextView; Public class drawerlayoutactivity extends fragmentactivity { PrivateDrawerlayout mdrawerlayout;PrivateListView drawercontent;//Drawer contents @Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (r.layout.drawer_layout); Mdrawerlayout = (drawerlayout) Findviewbyid (r.id.drawer_layout); Drawercontent = (ListView) Findviewbyid (r.id.drawer_content); Mdrawerlayout.setdrawerlistener (NewDemodrawerlistener ()); AddHeader (drawercontent);//listview HeaderDrawercontent.setadapter (NewMyadapter ()); Drawercontent.setonitemclicklistener (NewDraweritemclicklistener ()); Findviewbyid (r.id.slide). Setonclicklistener (NewOnclicklistener () { Public void OnClick(View arg0) {Mdrawerlayout.opendrawer (drawercontent);//Open Drawer Contents} });//Display the default homeGetsupportfragmentmanager (). BeginTransaction (). replace (R.id.fragment_container,NewHomefragment ()). commit (); }Private void AddHeader(ListView MDrawer2) {View view = Layoutinflater.from ( This). Inflate (R.layout.list_header,NULL); Mdrawer2.addheaderview (view); }StaticString titles[] = {"Home","Company","Great location","Settings"}; Class Myadapter extends Baseadapter {@Override Public int GetCount() {returnTitles.length; }@Override PublicObjectGetItem(intP) {returnTITLES[P]; }@Override Public Long Getitemid(intARG0) {return 0; }@Override PublicViewGetView(intP, View Contentview, ViewGroup arg2) {String title = Titles[p]; View v = layoutinflater.from (Getapplicationcontext ()). Inflate (R.layout.item_list,NULL); TextView TextView = (TextView) V.findviewbyid (R.id.text); Textview.settext (title);returnV } }@Override Public void onconfigurationchanged(Configuration newconfig) {Super. onconfigurationchanged (Newconfig); }Private class Draweritemclicklistener implementsListView. Onitemclicklistener { @Override Public void Onitemclick(adapterview<?> parent, view view,intPositionLongID) {Showfragmentbyid (position); Mdrawerlayout.closedrawer (drawercontent); } }//Switch to the corresponding fragment Public void Showfragmentbyid(intPosition) {Fragment F =NULL;Switch(position) { Case 1: F =NewHomefragment (); Break; Case 2: F =NewCompanyfragment (); Break; Case 3: F =NewNearfragment (); Break; Case 4: F =NewSettingfragment (); Break;default: Break; }if(f = =NULL) {return; } getsupportfragmentmanager (). BeginTransaction (). replace (R.id.fragment_container, F). commit (); }//callback function, can do something when open or close Private class Demodrawerlistener implements drawerlayout. Drawerlistener { @Override Public void ondraweropened(View Drawerview) {//Call when open}@Override Public void ondrawerclosed(View Drawerview) {//Call when off}@Override Public void Ondrawerslide(View Drawerview,floatSlideoffset) {//Sliding process}@Override Public void ondrawerstatechanged(intnewstate) {}}@Override Public Boolean Oncreateoptionsmenu(Menu menu) {return Super. Oncreateoptionsmenu (menu); }}
There is only homefragment code written here, and several other things are the same.
Public class homefragment extends Fragment { Public homefragment() { }@Override PublicViewOncreateview(Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {returnInflater.inflate (R.LAYOUT.HOME_FG, container,false); }@Override Public void onviewcreated(View view, Bundle savedinstancestate) {Super. onviewcreated (view, savedinstancestate); TextView TextView = (TextView) View.findviewbyid (R.id.text); Textview.settext ("Home"); }}
Home_fg.xml
<?xml version= "1.0" encoding= "Utf-8"?><relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" android:layout_width="Match_parent"android:layout_height="Match_parent" android:orientation="vertical" > <textview android:id
= "@+id/text" android:layout_width =< Span class= "Hljs-value" > "wrap_content" android:layout_height = "wrap_content" android:layout_margin =" 4DP " android:textsize =" 20SP "
android:textcolor = "#9370DB" android:textstyle = "bold" android:layout_centerinparent =" true "/> </relativelayout>
Android drawerlayout Drawer