歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> 【Android】抽屜控件 SlidingDrawer 的簡單示例

【Android】抽屜控件 SlidingDrawer 的簡單示例

日期:2017/3/1 10:34:19   编辑:Linux編程
【0】先看一段官方描述:

public class

SlidingDrawer

extends ViewGroup
java.lang.ObjectAndroid.view.Viewandroid.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:

[html]
  1. <SlidingDrawer
  2. android:id="@+id/drawer"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:handle="@+id/handle"
  6. android:content="@+id/content">
  7. <ImageView
  8. android:id="@id/handle"
  9. android:layout_width="88dip"
  10. android:layout_height="44dip" />
  11. <GridView
  12. android:id="@id/content"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent" />
  15. </SlidingDrawer>

【1】簡單的說就是:

SlidingDrawer是一個可以實現抽取式顯示的控件,可以垂直,水平拉取,每個抽屜都包含兩個東西:一個是拉手(handle),一個是抽屜裡面的東西(content),SlidingDrawer需要放置在另外的一個layout之上,這意味著SlidingDrawer只能放置在 FrameLayout or a RelativeLayout裡面。SlidingDrawer需要設置寬高為match_parent,而且在SlidingDrawer裡面必須設置handle與content(顯然,不然缺少任何一個都不叫“抽屜”啊)


【2】一些重要的屬性與方法:


XML Attributes Attribute Name Related Method Description 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 void animateClose() Closes the drawer with an animation.關閉動畫
void animateOpen() Opens the drawer with an animation.打開動畫
void animateToggle() Toggles the drawer open and close with an animation.
void close() Closes the drawer immediately. View getContent() Returns the content of the drawer.獲取抽屜的內容
View getHandle() Returns the handle of the drawer.獲取抽屜的把手
boolean isMoving() Indicates whether the drawer is scrolling or flinging.判斷是否在移動
boolean isOpened() Indicates whether the drawer is currently fully opened.是否打開
void lock() Locks the SlidingDrawer so that touch events are ignores.鎖死抽屜
boolean onInterceptTouchEvent(MotionEvent event) Implement this method to intercept all touch screen motion events.
boolean onTouchEvent(MotionEvent event) Implement this method to handle touch screen motion events.
void open() Opens the drawer immediately.打開抽屜
void setOnDrawerCloseListener(SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener) Sets the listener that receives a notification when the drawer becomes close.設置關閉抽屜監聽
void setOnDrawerOpenListener(SlidingDrawer.OnDrawerOpenListener onDrawerOpenListener) Sets the listener that receives a notification when the drawer becomes open.設置打開抽屜監聽
void setOnDrawerScrollListener(SlidingDrawer.OnDrawerScrollListener onDrawerScrollListener) Sets the listener that receives a notification when the drawer starts or ends a scroll.設置拉動抽屜監聽
void toggle() Toggles the drawer open and close.切換打開和關閉的抽屜。
void unlock() Unlocks the SlidingDrawer so that touch events are processed.解鎖

【4】Demo演示:

[html]
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <merge
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent" >
  6. <ImageView
  7. android:id="@+id/OrginView"
  8. android:layout_width="wrap_content"
  9. android:layout_height="wrap_content"
  10. android:src="@drawable/orginview"
  11. android:contentDescription="@string/image_org"/>
  12. <SlidingDrawer
  13. android:id="@+id/SlidingDrawer"
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent"
  16. android:handle="@+id/handle"
  17. android:content="@+id/content" >
  18. <ImageView
  19. android:id="@id/handle"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:src="@drawable/handleview"
  23. android:contentDescription="@string/image_handle" />
  24. <ImageView
  25. android:id="@id/content"
  26. android:layout_width="wrap_content"
  27. android:layout_height="wrap_content"
  28. android:src="@drawable/contentview"
  29. android:contentDescription="@string/image_content" />
  30. </SlidingDrawer>
  31. </merge>

這裡使用到了<merge>標簽,詳細使用方法請看;http://developer.android.com/resources/articles/layout-tricks-merge.html

Copyright © Linux教程網 All Rights Reserved