【Android】抽屉控件 SlidingDrawer 的简单示例


【0】先看一段官方描述:

public class

SlidingDrawer

extends ViewGroup
java.lang.Object
android.view.View
android.view.ViewGroup
android.widget.SlidingDrawer

Class Overview

SlidingDrawer hides content out of the screen and allows the user to drag a handle to bring the content on screen. SlidingDrawer can be used vertically or horizontally. A special widget composed of two children views: the handle, that the users drags, and the content, attached to the handle and dragged with it. SlidingDrawer should be used as an overlay inside layouts. This means SlidingDrawer should only be used inside of a FrameLayout or a RelativeLayout for instance. The size of the SlidingDrawer defines how much space the content will occupy once slid out so SlidingDrawer should usually use match_parent for both its dimensions. Inside an XML layout, SlidingDrawer must define the id of the handle and of the content:

<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>

【1】简单的说就是:

SlidingDrawer是一个可以实现抽取式显示的控件,可以垂直,水平拉取,每个抽屉都包含两个东西:一个是拉手(handle),一个是抽屉里面的东西(content),SlidingDrawer需要放置在另外的一个layout之上,这意味着SlidingDrawer只能放置在 FrameLayout or a RelativeLayout里面。SlidingDrawer需要设置宽高为match_parent,而且在SlidingDrawer里面必须设置handle与content(显然,不然缺少任何一个都不叫“抽屉”啊)


【2】一些重要的属性与方法:


XML Attributes
<nobr><em>Attribute Name</em></nobr> <nobr><em>Related Method</em></nobr> <nobr><em>Description</em></nobr>
android:allowSingleTap Indicates whether the drawer can be opened/closed by a single tap on the handle.
指示是否可以通过handle打开或关闭
android:animateOnClick Indicates whether the drawer should be opened/closed with an animation when the user clicks the handle.
指示是否当使用者按下手柄打开/关闭时是否该有一个动画
android:bottomOffset Extra offset for the handle at the bottom of the SlidingDrawer.
android:content Identifier for the child that represents the drawer's content.指定抽屉的内容
android:handle Identifier for the child that represents the drawer's handle.指定把手handle
android:orientation Orientation of the SlidingDrawer. 水平还是垂直
android:topOffset Extra offset for the handle at the top of the SlidingDrawer.

Public Methods
<nobr>void</nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#animateClose%28%29">animateClose</a></span>()</nobr>
Closes the drawer with an animation.关闭动画
<nobr>void</nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#animateOpen%28%29">animateOpen</a></span>()</nobr>
Opens the drawer with an animation.打开动画
<nobr>void</nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#animateToggle%28%29">animateToggle</a></span>()</nobr>
Toggles the drawer open and close with an animation.
<nobr>void</nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#close%28%29">close</a></span>()</nobr>
Closes the drawer immediately.
<nobr><a href="http://developer.android.com/reference/android/view/View.html">View</a></nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#getContent%28%29">getContent</a></span>()</nobr>
Returns the content of the drawer.获取抽屉的内容
<nobr><a href="http://developer.android.com/reference/android/view/View.html">View</a></nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#getHandle%28%29">getHandle</a></span>()</nobr>
Returns the handle of the drawer.获取抽屉的把手
<nobr>boolean</nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#isMoving%28%29">isMoving</a></span>()</nobr>
Indicates whether the drawer is scrolling or flinging.判断是否在移动
<nobr>boolean</nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#isOpened%28%29">isOpened</a></span>()</nobr>
Indicates whether the drawer is currently fully opened.是否打开
<nobr>void</nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#lock%28%29">lock</a></span>()</nobr>
Locks the SlidingDrawer so that touch events are ignores.锁死抽屉
<nobr>boolean</nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#onInterceptTouchEvent%28android.view.MotionEvent%29">onInterceptTouchEvent</a></span>(<a href="http://developer.android.com/reference/android/view/MotionEvent.html">MotionEvent</a> event)</nobr>
Implement this method to intercept all touch screen motion events.
<nobr>boolean</nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#onTouchEvent%28android.view.MotionEvent%29">onTouchEvent</a></span>(<a href="http://developer.android.com/reference/android/view/MotionEvent.html">MotionEvent</a> event)</nobr>
Implement this method to handle touch screen motion events.
<nobr>void</nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#open%28%29">open</a></span>()</nobr>
Opens the drawer immediately.打开抽屉
<nobr>void</nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#setOnDrawerCloseListener%28android.widget.SlidingDrawer.OnDrawerCloseListener%29">setOnDrawerCloseListener</a></span>(<a href="http://developer.android.com/reference/android/widget/SlidingDrawer.OnDrawerCloseListener.html">SlidingDrawer.OnDrawerCloseListener</a> onDrawerCloseListener)</nobr>
Sets the listener that receives a notification when the drawer becomes close.设置关闭抽屉监听
<nobr>void</nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#setOnDrawerOpenListener%28android.widget.SlidingDrawer.OnDrawerOpenListener%29">setOnDrawerOpenListener</a></span>(<a href="http://developer.android.com/reference/android/widget/SlidingDrawer.OnDrawerOpenListener.html">SlidingDrawer.OnDrawerOpenListener</a> onDrawerOpenListener)</nobr>
Sets the listener that receives a notification when the drawer becomes open.设置打开抽屉监听
<nobr>void</nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#setOnDrawerScrollListener%28android.widget.SlidingDrawer.OnDrawerScrollListener%29">setOnDrawerScrollListener</a></span>(<a href="http://developer.android.com/reference/android/widget/SlidingDrawer.OnDrawerScrollListener.html">SlidingDrawer.OnDrawerScrollListener</a> onDrawerScrollListener)</nobr>
Sets the listener that receives a notification when the drawer starts or ends a scroll.设置拉动抽屉监听
<nobr>void</nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#toggle%28%29">toggle</a></span>()</nobr>
Toggles the drawer open and close.切换打开和关闭的抽屉。
<nobr>void</nobr> <nobr><span class="sympad"><a href="http://developer.android.com/reference/android/widget/SlidingDrawer.html#unlock%28%29">unlock</a></span>()</nobr>
Unlocks the SlidingDrawer so that touch events are processed.解锁

【4】Demo演示:

<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/OrginView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/orginview" android:contentDescription="@string/image_org"/> <SlidingDrawer android:id="@+id/SlidingDrawer" 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="wrap_content" android:layout_height="wrap_content" android:src="@drawable/handleview" android:contentDescription="@string/image_handle" /> <ImageView android:id="@id/content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/contentview" android:contentDescription="@string/image_content" /> </SlidingDrawer> </merge>

这里使用到了<merge>标签,详细使用方法请看;http://developer.android.com/resources/articles/layout-tricks-merge.html

下面是简短的解释:An alternative root element that is not drawn in the layout hierarchy. Using this as theroot element is useful when you know that this layout will be placed into a layoutthat already contains the appropriate parent View to contain the children of the<merge> element. This is particularly useful when you plan to include this layoutin another layout file using<include> andthis layout doesn't require a different ViewGroup container.

public class SlidingDrawerDemoActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override protected void onResume() { // TODO Auto-generated method stub super.onResume(); SlidingDrawer mSlidingDrawer = (SlidingDrawer) this.findViewById(R.id.SlidingDrawer); mSlidingDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() { @Override public void onDrawerOpened() { Toast.makeText(getApplicationContext(),"OnDrawerOpened",Toast.LENGTH_SHORT).show(); } }); mSlidingDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() { @Override public void onDrawerClosed() { Toast.makeText(getApplicationContext(),"onDrawerClosed",Toast.LENGTH_SHORT).show(); } }); mSlidingDrawer.setOnDrawerScrollListener(new SlidingDrawer.OnDrawerScrollListener() { @Override public void onScrollEnded() { Toast.makeText(getApplicationContext(),"onScrollEnded",Toast.LENGTH_SHORT).show(); } @Override public void onScrollStarted() { Toast.makeText(getApplicationContext(),"onScrollStarted",Toast.LENGTH_SHORT).show(); } }); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } }

Run起来的效果图如下:


【Android】抽屉控件 SlidingDrawer 的简单示例【Android】抽屉控件 SlidingDrawer 的简单示例

左边图片是刚启动时显示的画面,右边是点击下拉按钮后出现的抽屉内容:也是一张图片,此时原图被遮住,显示抽屉内容,再点击handle可以关闭抽屉

学习这个时候让我想起了RenRen的Android客户端启动之后就是似乎就是这样的一个抽屉机制,拉出抽泣是消息内容,呵,当然这只是猜测