Android Developer Headlines (ii) implement left-slide menu _android

Source: Internet
Author: User

In the previous article to introduce you to the Android developer headlines (a) launch page implementation, interested friends can refer to.

Title: Bring you to achieve the developer's headline (ii) to implement the left-slide menu

Grammar_cjkruby:true

Starting today to mimic the developer headline sideslip menu, the second article in this series, I believe you have seen many apps using this sideslip. Today I'm going to teach you to use Android with Drawerlayout controls.

Drawerlayout is the Supportlibrary package in the implementation of the Sideslip menu effect control, it can be said that Drawerlayout is because of third-party controls such as Menudrawer after the emergence of Google to learn from the product. Drawerlayout is divided into two parts: the side menu and the main content area, while the side menu can be unfolded and hidden by gesture (drawerlayout), the contents of the main content area can be changed with the click of the menu (which requires the user to realize it).

First, to show you the effect of the picture:

Two. Code implementation

1.drawerLayout is actually a layout control, with LinearLayout and other controls is a thing, but drawerlayout with sliding function. As long as the layout in accordance with the provisions of drawerlayout layout, you can have the effect of sideslip. I put a layout file on the side of the Sideslip menu.

<android.support.v4.widget.drawerlayout xmlns:android= "Http://schemas.android.com/apk/res/android" Android:id = "@+id/drawer_layout" android:layout_width= "match_parent" android:layout_height= "Match_parent" > < Relativelayout android:layout_width= "match_parent" android:layout_height= "Match_parent" True ' android:fitssystemwindows= ' true ' > <include android:id= ' @+id/rl_title ' layout= ' @layout/layout_main_ Title "/> <!--the main content view--> <framelayout android:id=" @+id/content_frame "android:layout_width=" Match_parent "android:layout_height=" match_parent "android:layout_below=" @+id/rl_title "android:background=" @ Color/white_normal "> </FrameLayout> </RelativeLayout> <!--the Navigation view--> <
Framelayout android:id= "@+id/left_drawer" android:layout_width= "280DP" android:layout_height= "Match_parent" android:layout_gravity= "Start" > <!--left menu--> <include layout= "@layout/layout_main_left"/> </fRamelayout> </android.support.v4.widget.DrawerLayout>  

Attention matters

The layout code for the main content area should be placed in front of the Sideslip menu layout, which helps Drawerlayout determine who is the Sideslip menu and who is the main content area
The layout of the part of the Sideslip menu (here is ListView) allows you to set the Layout_gravity properties, indicating whether the Sideslip menu is on the left or the right.

2.mainactivity.java Inheritance Fragmentactivity

1. Set the content fragment, set the status bar

2. Handle the left click event, set the selected background in the Click event, and close the left side sideslip menu.

public class Mainactivity extends fragmentactivity{private drawerlayout mdrawerlayout, private relativelayout rlhome, RL
Gift, Rlshare;
private int currentselectitem = r.id.rl_home;//default home page private contentfragment contentfragment; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (
R.layout.activity_main);
Mdrawerlayout = (drawerlayout) Findviewbyid (r.id.drawer_layout);
Findviewbyid (R.id.iv_menu). Setonclicklistener (Clicklistener);
Initleftmenu ()//Initialize left menu contentfragment=new contentfragment (); 
Getsupportfragmentmanager (). BeginTransaction (). Add (R.id.content_frame,contentfragment). commit ();
Setwindowstatus (); private void Initleftmenu () {rlhome = (relativelayout) Findviewbyid (r.id.rl_home); rlgift = (relativelayout) findViewBy
Id (R.id.rl_gift);
Rlshare = (relativelayout) Findviewbyid (R.id.rl_share);
Rlhome.setonclicklistener (Onleftmenuclicklistener);
Rlgift.setonclicklistener (Onleftmenuclicklistener); Rlshare.setonclicklistEner (Onleftmenuclicklistener);
Rlhome.setselected (TRUE); Private Onclicklistener Onleftmenuclicklistener = new Onclicklistener () {@Override public void OnClick (View v) {if (CU Rrentselectitem!= V.getid ()) {//Prevent repeated clicks Currentselectitem=v.getid (); Noitemselect (); switch (V.getid ()) {case R.id.rl_
Home:rlHome.setSelected (TRUE);
Contentfragment.setcontent ("This is the Home page");
Break
Case r.id.rl_gift:rlgift.setselected (TRUE);
Contentfragment.setcontent ("This is a gift redemption");
Break
Case r.id.rl_share:rlshare.setselected (TRUE);
Contentfragment.setcontent ("This is my share");
Break
} mdrawerlayout.closedrawer (Gravity.left);
}
}
};
private void Noitemselect () {rlhome.setselected (false); rlgift.setselected (false); rlshare.setselected (false);  Private Onclicklistener Clicklistener = new Onclicklistener () {@Override public void OnClick (View v) {switch (V.getid ())
{Case r.id.iv_menu://Open left drawer mdrawerlayout.opendrawer (gravity.left); break;}}
}; Set the status bar private void Setwindowstatus () {if (Build.VERSION.SDK_INT >= BUild. Version_codes. KitKat) {//Transparent status bar GetWindow (). Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);//transparent navigation bar GetWindow ().
Addflags (WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
Sets the status bar Color GetWindow (). Setbackgrounddrawableresource (R.color.main_color); }
}
}

3. Left menu item Select the background layout file selector_left_menu_item.xml.

<?xml version= "1.0" encoding= "Utf-8"?> <selector xmlns:android=
"http://schemas.android.com/apk/res/" Android >
<item android:drawable= "@color/menu_left_item_select" android:state_selected= "true"/>
<item android:drawable= "@color/white_normal"/>

4.ContentFragment Display content fragment Here I added a way to set the content, which is used to click on the left side toggle display.

public class Contentfragment extends fragment{
private TextView tvcontent;
@Override public
View Oncreateview (layoutinflater inflater, ViewGroup container,bundle savedinstancestate) {
View Rootview=layoutinflater.from (Getactivity ()). Inflate (r.layout.fragment_content, null);
Tvcontent= (TextView) Rootview.findviewbyid (r.id.tv_content);
return rootview;
}
public void SetContent (String content) {
tvcontent.settext (content);
}

What is the relationship between 5.drawerLayout and fragment?

We see a lot of use of drawerlayout code in the same time the use of fragment, which will cause misunderstanding that the use of drawerlayout must be used fragment, in fact, this is wrong, the use of fragment because the Sideslip menu is clicked, Main content Area If the content is more complex, it will be easier to fill it with fragment, if your main content area is just a simple string, just want to update the contents of the string in different menu clicks, I don't think it is necessary to use fragment. What I do with fragment is to update the contents of the string as simple as this.

The above content is for the developer of Android Development Headlines (ii) to achieve the left-slide menu of all the introduction, hope to help you!

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.