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> www.2cto.com
 
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 .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. slidingdrawer );
LoadApps ();
Gv = (GridView) findViewById (R. id. allApps );
Sd = (s0000ingdrawer) findViewById (R. id. sliding );
Iv = (ImageView) findViewById (R. id. imageViewIcon );
Gv. setAdapter (new GridAdapter ());
Sd. setOnDrawerOpenListener (new s0000ingdrawer. OnDrawerOpenListener () // open the drawer
{
@ Override
Public void onDrawerOpened (){
Iv. setImageResource (R. drawable. touch_handler); // responds to Drawer Opening events
// Set the image to the lower
}
});
Sd. setOnDrawerCloseListener (new SlidingDrawer. OnDrawerCloseListener (){
@ Override
Public void onDrawerClosed (){
Iv. setImageResource (R. drawable. touch_handler); // responds to the close drawer event
}
});
}
 
Private void loadApps (){
Intent 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 stub
Return apps. size ();
}
 
Public Object getItem (int position ){
// TODO Auto-generated method stub
Return apps. get (position );
}
 
Public long getItemId (int position ){
// TODO Auto-generated method stub
Return position;
}
 
Public View getView (int position, View convertView, ViewGroup parent ){
// TODO Auto-generated method stub
ImageView 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 Based on listener

From the wangkuifeng0118 Column

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.