Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android SlidingDrawer 滑動式抽屜詳解:屬性、方法與完整範例

編輯:關於 Android 程式設計

SlidingDrawer 的效果其實大家都看過——早期 Android 1.5 模擬器進入應用程式列表時,從畫面下方拉出抽屜的那個互動,就是它。

一、基本概念

SlidingDrawer 的作用是把一部分內容先藏在畫面外,讓使用者透過「把手」把它拉出來。它可以垂直或水平滑動,內部由兩個 View 組成:一個是可拖動的 handle(把手),另一個是被藏起來的 content(內容)。使用時必須在版面檔中明確指定這兩者的 id。

<SlidingDrawer android:layout_width="fill_parent"
  android:layout_height="fill_parent" android:handle="@+id/handle"
  android:content="@+id/content" android:orientation="vertical"
  android:id="@+id/slidingdrawer">
  <ImageButton android:id="@id/handle" android:layout_width="50dip"
   android:layout_height="44dip" android:src="@drawable/up" />
  <LinearLayout android:id="@id/content"
   android:layout_width="fill_parent" android:layout_height="fill_parent"
   android:background="#ffffff">
   <TextView android:text="這是一個滑動式抽屜的示例"
   android:id="@+id/tv"
   android:textSize="18px"
   android:textColor="#000000"
   android:gravity="center_vertical|center_horizontal"
   android:layout_width="match_parent"
   android:layout_height="match_parent"></TextView>
  </LinearLayout>
</SlidingDrawer>

二、重要屬性

  • android:allowSingleTap:是否允許透過點擊把手來開啟或關閉抽屜。
  • android:animateOnClick:使用者按下把手開關時,是否播放動畫。
  • android:content:指定被隱藏內容的 id。
  • android:handle:指定把手的 id。

三、重要方法

  • animateClose():關閉抽屜並播放動畫。
  • close():立即關閉,不做動畫。
  • getContent():取得內容 View。
  • isMoving():判斷抽屜是否正在移動中。
  • isOpened():判斷抽屜是否已完全展開。
  • lock():鎖定,屏蔽觸摸事件。
  • unlock():解除鎖定。
  • toggle():在展開與收合之間切換。
  • setOnDrawerCloseListener():抽屜關閉時的 callbacks。

四、完整範例

1. 版面檔 slidingdrawer.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent" android:layout_height="fill_parent"
  android:orientation="vertical" android:background="@drawable/default_bg">
  <SlidingDrawer android:layout_width="fill_parent"
   android:layout_height="fill_parent" android:handle="@+id/handle"
   android:content="@+id/content" android:orientation="vertical"
   android:id="@+id/slidingdrawer">
   <ImageButton android:id="@id/handle" android:layout_width="50dip"
    android:layout_height="44dip" android:src="@drawable/up" />
   <LinearLayout android:id="@id/content"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:background="#ffffff">
    <TextView android:text="這是一個滑動式抽屜的示例"
    android:id="@+id/tv"
    android:textSize="18px"
    android:textColor="#000000"
    android:gravity="center_vertical|center_horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></TextView>
   </LinearLayout>
  </SlidingDrawer>
</LinearLayout>

2. Java 程式碼

這個範例示範了三個監聽器的搭配:開啟時把把手圖示換成向下箭頭、關閉時換回向上箭頭,拖動過程中則在 TextView 上顯示目前狀態。

package com.wjq;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageButton;
import android.widget.SlidingDrawer;
import android.widget.TextView;

public class SlidingDrawerDemo extends Activity {
    private SlidingDrawer mDrawer;
    private ImageButton imbg;
    private Boolean flag = false;
    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sildingdrawer);
        imbg = (ImageButton) findViewById(R.id.handle);
        mDrawer = (SlidingDrawer) findViewById(R.id.slidingdrawer);
        tv = (TextView) findViewById(R.id.tv);

        mDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
            @Override
            public void onDrawerOpened() {
                flag = true;
                imbg.setImageResource(R.drawable.down);
            }
        });

        mDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
            @Override
            public void onDrawerClosed() {
                flag = false;
                imbg.setImageResource(R.drawable.up);
            }
        });

        mDrawer.setOnDrawerScrollListener(new SlidingDrawer.OnDrawerScrollListener() {
            @Override
            public void onScrollEnded() {
                tv.setText("結束拖動");
            }

            @Override
            public void onScrollStarted() {
                tv.setText("開始拖動");
            }
        });
    }
}

補充一點:SlidingDrawer 在後續的 Android 版本中已被標示為 deprecated,新專案可以改用 LayoutTransition、BottomSheet 或自訂的 TranslateAnimation 來達到類似效果;維護舊專案時,上面的寫法仍然完全適用。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved