Android drawer implementation

Source: Internet
Author: User

Today, the drawer effect is achieved on the mobile phone. It is actually very simple, but the effect is very cool.

First, set the XML layout file under layout.

<? XML version = "1.0" encoding = "UTF-8"?> <Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <slidingdrawer Android: Id = "@ + ID/sliding" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: content = "@ + ID/allapps" Android: handle = "@ + ID/imageviewicon" Android: orientation = "vertical"> <gridview Android: Id = "@ + ID/allapps" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: background = "@ drawable/BK" Android: columnwidth = "60dp" Android: gravity = "center" Android: horizontalspacing = "10dp" Android: numcolumns = "auto_fit" Android: padding = "10dp" Android: stretchmode = "columnwidth" Android: verticalspacing = "10dp"/> <imageview Android: Id = "@ + ID/imageviewicon" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: src = "@ drawable/touch_handler"/> </slidingdrawer> </relativelayout>

Slidingdrawer is an important drawer control, handle is the drag button of the drawer, and content is the content in the drawer.

Then create the chouti activity class:

Import android. app. activity; import android. content. intent; import android. content. PM. resolveinfo; import android. OS. bundle; import android. view. view; import android. view. viewgroup; import android. widget. baseadapter; import android. widget. gridview; import android. widget. imageview; import android. widget. slidingdrawer; public class chouti extends activity {private gridview GV; private slidingdrawer SD; private imageview IV; private list <resolveinfo> Apps;/** called when the activity is first created. * // @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. slidingdrawer); loadapps (); GV = (gridview) findviewbyid (R. id. allapps); SD = (slidingdrawer) findviewbyid (R. id. sliding); IV = (imageview) findviewbyid (R. id. imageviewicon); GV. setadapter (New gridadapter (); SD. setondraweropenlistener (New slidingdrawer. ondraweropenlistener () // open the drawer {@ overridepublic void ondraweropened () {IV. setimageresource (R. drawable. touch_handler); // respond to the drawer opening event //, set the image to the bottom}); SD. setondrawercloselistener (New s0000ingdrawer. ondrawercloselistener () {@ overridepublic void ondrawerclosed () {IV. setimageresource (R. drawable. touch_handler); // response to close drawer event});} private void loadapps () {intent = new intent (intent. action_main, null); intent. addcategory (intent. category_launcher); apps = getpackagemanager (). queryintentactivities (intent, 0);} public class gridadapter extends baseadapter {public gridadapter () {} public int getcount () {// todo auto-generated method stubreturn apps. size ();} public object getitem (INT position) {// todo auto-generated method stubreturn apps. get (position);} public long getitemid (INT position) {// todo auto-generated method stubreturn position;} public view getview (INT position, view convertview, viewgroup parent) {// todo auto-generated method stubimageview imageview = NULL; If (convertview = NULL) {imageview = new imageview (chouti. this); imageview. setscaletype (imageview. scaletype. fit_center); imageview. setlayoutparams (New gridview. layoutparams (50, 50);} else {imageview = (imageview) convertview;} resolveinfo rI = apps. get (position); imageview. setimagedrawable (Ri. activityinfo. loadicon (getpackagemanager (); Return imageview ;}}}

The loadapps method is used to obtain images and text on the main interface.

Then go to the custom adapter.

Check the running effect:

After sliding the imageview button up:

To achieve better results, you can use two slide images, one facing up and the other facing down. Switch the listener accordingly.

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.