Android development-slidingdrawer (1)
/*
* Android development-slidingdrawer
* Beijing Android Club group: 167839253
* Created on: 2012-7-26
* Author: blueeagle
* Email: liujiaxiang@gmail.com
*/
Overview
To facilitate operations, aesthetics, and space saving, Android provides controls such as slidingdrawer. Slidingdrawer-A drawer-like control for the moment. The effects of these drawers can be seen everywhere, such as the pull-down effect of the android notification bar, or the pop-up box of a webpage toolbox.
Let's take a look at how the SDK defines: slidingdrawer hides some content outside the screen and allows users to drag the "handle" to display the content on the screen again. Slidingdrawer can be used in both horizontal and vertical directions. A slidingdrawer control consists of two sub-views. One is the "handle" and the other is the drag-and-drop content. Slidingdrawer should be covered in layout, which means that slidingdrawer should only be used in one framelayout or relativelayout. The size of slidingdrawer defines the size of the content to be dragged out. Therefore, slidingdrawer usually uses match_parent to define its size. The slidingdrawer must also define the ID numbers of the "handle" and the View content.
For example, the following layout file is a simple description of slidingdrawer:
<SlidingDrawer android:id="@+id/drawer" android:layout_width="match_parent" android:layout_height="match_parent" android:handle="@+id/handle" android:content="@+id/content"> <ImageView android:id="@+id/handle" android:layout_width="88dip" android:layout_height="44dip" /> <GridView android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" /> </SlidingDrawer>
Attributes in XML files
Attribute name |
Description |
Android: allowsingletap |
Whether to open or close the drawer by clicking handle. The default value is true. (If it is false, you must drag, slide, or use a trackball .) |
Android: animateonclick |
As the name suggests, whether there is an animation when you click. The default value is true. |
Android: bottomoffset |
The extra distance between the "handle" and the bottom of s0000ingdrawer. |
Android: Content |
Content of slidingdrawer. |
Android: handle |
The "handle" of slidingdrawer ". |
Android: Orientation |
The direction of slidingdrawer. |
Android: topoffset |
The extra distance between the "handle" and the s0000ingdrawer top. |
Some important methods:
Void setondrawercloselistener (s0000ingdrawer. ondrawercloselistener)
Set a listener to receive notifications when the drawer is closed.
Void setondraweropenlistener (s0000ingdrawer. ondraweropenlistener)
Since: API level 3
Set a listener to receive notifications when the drawer is opened.
Void setondrawerscrolllistener (slidingdrawer. ondrawerscrolllistener)
Set a listener to receive notifications when the drawer is in an open or rolling state.
Animateclose ():
Close the drawer with animation.
Animateopen ():
Open drawer with animation
Getcontent ():
Get content
Ismoving ():
Indicates whether slidingdrawer is being moved.
Isopened ():
Indicates whether slidingdrawer is enabled.
Lock ():
Mask touch events.
Unlock ():
Unmask touch events.
Toggle ():
Switch the slidingdrawer of the opened and closed drawers.
A simple example
We can reference the XML file provided by the SDK.
Write the following in the Java file:
Public class sshortingdrawerdemoactivity extends activity {private sshortingdrawer mydrawer; private imageview myimageview; private gridview mygridview; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mydrawer = (s0000ingdrawer) findviewbyid (R. id. drawer); myimageview = (imageview) findviewbyid (R. id. handle); mygridview = (G Ridview) findviewbyid (R. id. content); mydrawer. setondraweropenlistener (newsreceivingdrawer. ondraweropenlistener () {@ override public void ondraweropened () {myimageview. setimageresource (R. drawable. down) ;}}); mydrawer. setondrawercloselistener (newsreceivingdrawer. ondrawercloselistener () {@ override public void ondrawerclosed () {myimageview. setimageresource (R. drawable. up) ;}}); mygridview. setadapter (New Imageadapter (this); mygridview. setonitemclicklistener (newonitemclicklistener () {@ override public void onitemclick (adapterview <?> Arg0, viewarg1, int arg2, long arg3) {// todo auto-generated method stub toast. maketext (slidingdrawerdemoactivity. this, "you have selected" + (arg2 + 1) + "image", toast. length_short ). show ();}});}}
We have discussed the imageadapter class before and will not go into details here. The effects of the above Code are as follows:
Of course, you also need to add the following parameters to the XML file of the gridview:
Android: numcolumns ="Auto_fit"
Android: verticalspacing ="10dp"
Android: horizontalspacing ="10dp"
Android: columnwidth ="90dp"
Android: stretchmode ="Columnwidth"
Android: gravity ="Center"
If you change the height attribute of sildingdrawer, for example, Android: layout_height = "100dip", the effect will be changed to as shown in:
If you change the topoffset attribute of sildingdrawer, for example, Android: topoffset = "150dip", the effect will be changed to as shown in:
Summary
The above simple example obviously cannot meet the application development requirements. When writing a program, we often encounter requirements such as from left to right, from top to bottom, controlling the size of slidingdrawer, to complete this operation, we need to customize an slidingdrawer. The custom slidingdrawer inherits a linearlayout, and we will discuss in detail how to customize a slidingdrawer in later chapters.