Android Slide Menu Drawerlayout (drawer layout) Implementation

Source: Internet
Author: User

Application Scenarios:because the side-slip menu has a better user experience effect, so more apps use the slide-down Drawer menu list, such as NetEase client, Baidu AV, Iqiyi art and so on. At this point, the slide-by menu has more usage requirements.
Knowledge Point Introduction:There are many ways to implement the side-by-Side menu function, if open source project Slidingmenu, for Https://github.com/jfeinstein10/SlidingMenu. The open source project relies on another open source project, Actionbarsherlock, for Https://github.com/JakeWharton/ActionBarSherlock. This way of realization, the reader can usually have time to study on their own. This example mainly introduces the support provided by Android-support-v4.jar, Android.support.v4.widget.DrawerLayout, which is compatible with the lower version of the system.
How to use:First step: Create a new test project Drawerlayout.
The second step: write the related layout file, the main interface layout file activity_main.xml.
<linearlayout 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 "Android:orien tation= "Vertical" tools:context= ". Mainactivity "> <android.support.v4.widget.drawerlayout android:id=" @+id/drawer_layout "Android:lay         Out_width= "Match_parent" android:layout_height= "match_parent" android:background= "@android: Color/white" >            <framelayout android:id= "@+id/fragment_layout" android:layout_width= "Match_parent" android:layout_height= "Match_parent" android:background= "@android: Color/white" > <textvie W android:layout_width= "Match_parent" android:layout_height= "Wrap_content" an droid:gravity= "center" android:text= "Main content interface"/> </FrameLayout> <!--androi D:layout_gravity= "Start" side-slip menu on the left android:layout_gravity= "start" side-slip menu on the right--<linearlayout and            Roid:id= "@+id/menu_layout" android:layout_width= "180DP" android:layout_height= "Match_parent" android:layout_gravity= "Start" android:background= "@android: Color/white" android:orientation= "ve Rtical "> <imageview android:layout_width=" match_parent "Android:layout_hei            ght= "wrap_content" android:gravity= "center" android:src= "@drawable/ic_launcher"/>                <textview android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:gravity= "center" android:text= "side-pull Menu" android:textcolor= "@android: Color            /black "/> <!--android:cachecolorhint=" #FFFFFF "fix android ListView scrolling Item background black problem--      <listview          Android:id= "@+id/menu_listview" android:layout_width= "Match_parent" android:layout _height= "Match_parent" android:cachecolorhint= "#FFFFFF" > </ListView> </linea Rlayout> </android.support.v4.widget.DrawerLayout></LinearLayout>
the layout file for the fragment class Layout_first.xml,layout_next.xml. "Layout_first.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" >    <textview         android:layout_width= "match_parent"        android:layout_height= " 20DP "        android:text=" firstfragment "        android:id=" @+id/texttextview "/>    <linearlayout android: Layout_width= "Match_parent"        android:layout_height= "wrap_content"        android:orientation= "vertical" >        <listview android:layout_height= "wrap_content"            android:layout_width= "Wrap_content"            android: Cachecolorhint= "#FFFFFF"            android:id= "@+id/firstfragmentlistview" >        </ListView>    </ Linearlayout></linearlayout>

"Layout_next.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" >    <textview         android:layout_height= "wrap_content"        android:layout_width= " Match_parent "        android:text=" Nextfragment "/></linearlayout>

The listitem layout file for the ListView is Main_listitem.xml.
"Main_listitem.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" >    <textview android:layout_width= "match_parent"        android:layout_height= "40DP"        android:id= "@+id/menu"        android:textsize= "20DP"        android:gravity= "center_vertical"        android:text = "Loading ..."/></linearlayout>
The second step: write the relevant Java files, Firstfragment.java, Nextfragment, Mainactivity.java, Menulistviewadapter.java, Databuiltutils.java.
"Firstfragment.java"
Import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.listview;import Android.widget.textview;import Android.widget.toast;public class FirstFragment    Extends Fragment{private TextView TextView; Private ListView ListView;        @Override public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {        View layout = inflater.inflate (R.layout.layout_first, NULL);        TextView = (TextView) Layout.findviewbyid (R.id.texttextview); Textview.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View arg0) {Toast.maketext (        Getactivity (), "Firstfragment", Toast.length_short). Show ();});        ListView = (ListView) Layout.findviewbyid (R.id.firstfragmentlistview); Menulistviewadapter adapter = new Menulistviewadapter (getactivity (), Databuiltutils.getfirstmaplisT ());        Listview.setadapter (adapter);    return layout; }}
"Nextfragment.java"
Import Android.os.bundle;import Android.support.v4.app.fragment;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;public class Nextfragment extends fragment{    @Override    Public View Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {        View layout = Inflater.inflate (r.layout.layout_next, null);        return layout;    }}

"Mainactivity.java"
Import Java.util.arraylist;import java.util.hashmap;import Android.os.bundle;import Android.app.activity;import Android.content.context;import Android.support.v4.app.fragment;import android.support.v4.app.FragmentActivity; Import Android.support.v4.app.fragmenttransaction;import Android.support.v4.widget.drawerlayout;import Android.view.menu;import Android.view.view;import Android.widget.adapterview;import Android.widget.ArrayAdapter; Import Android.widget.linearlayout;import Android.widget.listview;import Android.widget.relativelayout;import android.widget.adapterview.onitemclicklistener;/** * Drawer effect * Drawerlayout is associated with fragment * drawerlayout: * 1. The general content layer uses Framelayout * 2.slidingLayout to set a layout_grative property * document recommended using android:layout_gravity= "Start" */public class  Mainactivity extends fragmentactivity{private drawerlayout mdrawer_layout;                            Drawer-style layout private linearlayout mmenu_layout; @Override protected void OnCreate (Bundle savedinstancestate) {supEr.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Mdrawer_layout = (drawerlayout) Findviewbyid (r.id.drawer_layout);        Mmenu_layout = (linearlayout) Findviewbyid (r.id.menu_layout);        ListView Menu_listview = (ListView) Mmenu_layout.findviewbyid (R.id.menu_listview);        arraylist
"Menulistviewadapter.java"
Import Java.util.hashmap;import java.util.list;import Java.util.map;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.textview;public class Menulistviewadapter extends BaseAdapter { Private list"Databuiltutils.java"

Import Java.util.arraylist;import Java.util.hashmap;public class Databuiltutils {public static Arraylist



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.