Teach you to use Drawlayout to implement the Android slide menu

Source: Internet
Author: User

Now the app more and more focus on user experience, Baidu Video client has a special effect or quite attractive, in the main interface finger swipe to the right, you can display the menu, and the main interface will be hidden most, but still have a small portion of the left side with the menu to show. Similar to the everyday sounds, everyone's client.

There are many ways to implement this side-by-side menu, such as the most commonly used open source Slidingmenu. Another way to achieve this is to have two parts in an activity layout, one for the menu layout and one for the content layout. Two layouts are arranged horizontally, menu layout is left, content layout is right. When initialized, the menu layout is shifted to the left so that it can be completely hidden so that the content layout is fully visible in the activity. You can then change the left offset of the menu layout by listening to the finger swipe events to control the display and hiding of the menu layout.


But these two methods are relatively cumbersome, today we introduce a more simple method. is to use drawlayout directly. For a more detailed introduction to Drawlayout, refer to the API documentation: http://developer.android.com/reference/android/support/v4/widget/DrawerLayout.html.

Start with a new Android project and implement several layouts in turn. Take a look at the two sub-layouts first.


Android:id= "@+id/drawer_layout"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
android:orientation= "Vertical" >


Android:id= "@+id/listview"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:cachecolorhint= "#00000000"
Android:divider= "@android: Color/transparent"/>



    android:id= "@+id/drawer_layout"
    Android:layout_width= "Match_parent"
    android:layout_height= "match_parent"
     android:orientation= "vertical";

        android:id= @+id/ ScrollView1 "
      android:layout_width=" match_parent "
       android:layout_height= "wrap_content";

    
         android:id= "@+id/textview1"
        android:layout_ Width= "Wrap_content"
        android:layout_height= "Wrap_content"
        android:text= "Daily article"
        Android:textappearance= "Android:attr/textappearancelarge";
  

The main interface layout, commented out is the right side of the slide, now implemented is the left side of the slide.

Xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:id= "@+id/drawer_layout"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >

Android:id= "@+id/fragment_layout"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent" >


Android:id= "@+id/menu_layout_left"
Android:layout_width= "150DP"
android:layout_height= "Match_parent"
Android:layout_gravity= "left"
android:background= "#FFFFFF" >

Android:id= "@+id/menu_listview_l"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >







Then create two classes to inherit the fragment and plug in the two sub-layouts.
public class Firstfragment extends Fragment {

@Override
Public View Oncreateview (layoutinflater inflater, ViewGroup container,
Bundle savedinstancestate) {
TODO auto-generated Method Stub
Return inflater.inflate (R.layout.first, NULL);
}
}

Then take a look at the main class:
public class Mainactivity extends Fragmentactivity {


public static final string[] TITLES = {"First", "second"};
Private Drawerlayout mdrawerlayout;
Private Relativelayout mleftlayout;
Private ListView Mleftlistview;


@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Findviewbyid ();
Mleftlistview.setadapter (New Arrayadapter (This,
Android. R.layout.simple_expandable_list_item_1, TITLES));


Listening Menu
Mleftlistview.setonitemclicklistener (New Draweritemclicklistenerleft ());
}

private void Findviewbyid () {
Mdrawerlayout = (drawerlayout) Findviewbyid (r.id.drawer_layout);
Mleftlayout = (relativelayout) Findviewbyid (r.id.menu_layout_left);
Mleftlistview = (ListView) Findviewbyid (r.id.menu_listview_l);
}


public class Draweritemclicklistenerleft implements Onitemclicklistener {

@Override
public void Onitemclick (Adapterview

Teach you to use Drawlayout to implement the Android slide menu

Related Article

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.