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

Android 抽屜效果實作:SlidingDrawer 用法與進階應用

編輯:關於 Android 程式設計

SlidingDrawer 這個類別如今已被標示為不建議使用,但如果你的效果需求簡單,它其實仍然相當好用。它最大的優點是極簡:只支援上下或左右滑動,幾行程式就能做出抽屜;缺點是初始方向無法透過屬性指定,得靠 orientation 與版面結構自行安排。

基本用法:handle 與 content

SlidingDrawer 必須同時指定兩個子 View:android:handle 是使用者拖曳的「把手」,android:content 則是抽屜拉開後顯示的內容。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#000099">

    <SlidingDrawer
        android:id="@+id/slidingdrawer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:content="@+id/content"
        android:handle="@+id/handle"
        android:orientation="vertical" >

        <ImageView
            android:id="@id/handle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/lo19" />

        <LinearLayout
            android:id="@id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#008800">
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="隱藏的內容" />
        </LinearLayout>
    </SlidingDrawer>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="bottom textView"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:gravity="center_vertical|center_horizontal" />
</LinearLayout>

寫完配置檔後,Activity 中不寫任何程式碼就能滑動了。

需要的話可以掛上監聽器

若想針對抽屜的開啟、關閉、滑動過程做處理,可用以下三組 listener:

SlidingDrawer sd = (SlidingDrawer) findViewById(R.id.slidingdrawer);

sd.setOnDrawerOpenListener(new OnDrawerOpenListener() {
    public void onDrawerOpened() {
        // 抽屜已完全開啟
    }
});

sd.setOnDrawerCloseListener(new OnDrawerCloseListener() {
    public void onDrawerClosed() {
        // 抽屜已完全關閉
    }
});

sd.setOnDrawerScrollListener(new OnDrawerScrollListener() {
    public void onScrollEnded() {
        // 滑動結束
    }
    public void onScrollStarted() {
        // 開始滑動
    }
});

進階範例:把 App 圖示放進抽屜

下面這個例子會把裝置上所有應用程式的圖示,以縮小的方式排列在抽屜中,做法類似早期的桌面啟動器。

版面配置

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://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/lo13"
            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/lo19" />
    </SlidingDrawer>

</RelativeLayout>

Activity 實作

package com.example.t;

import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
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 MainActivity extends Activity {

    private GridView gv;
    private SlidingDrawer sd;
    private ImageView iv;
    private List<ResolveInfo> apps;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_2);
        loadApps();
        gv = (GridView) findViewById(R.id.allApps);
        sd = (SlidingDrawer) findViewById(R.id.sliding);
        iv = (ImageView) findViewById(R.id.imageViewIcon);
        gv.setAdapter(new GridAdapter());

        // 開抽屜時,把把手圖換成朝下的樣子
        sd.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
            @Override
            public void onDrawerOpened() {
                iv.setImageResource(R.drawable.lo19);
            }
        });

        // 關抽屜時還原把手圖
        sd.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
            @Override
            public void onDrawerClosed() {
                iv.setImageResource(R.drawable.lo13);
            }
        });
    }

    // 取得所有可啟動的應用程式
    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 int getCount() {
            return apps.size();
        }

        public Object getItem(int position) {
            return apps.get(position);
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView;
            if (convertView == null) {
                imageView = new ImageView(MainActivity.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;
        }
    }
}

這裡的關鍵是 queryIntentActivities():以 ACTION_MAIN 搭配 CATEGORY_LAUNCHER 查詢,就能拿到所有出現在桌面啟動器中的 App 清單,再逐一用 loadIcon() 取出圖示。BaseAdapter 中也示範了 convertView 的重用寫法,避免每次捲動都重新建立 View。

該不該繼續用 SlidingDrawer?

SlidingDrawer 在 API 17 之後被標記為 deprecated,官方建議改用 DrawerLayout(支援 Direction/Navigation Drawer 設計規範)或 BottomSheet。若只是內部工具、原型或需要極簡的抽屜互動,SlidingDrawer 依然可以快速交付;但若是面向一般使用者的正式產品,請優先採用 DrawerLayout,才能獲得手勢衝突處理、系統狀態列配色與無障礙支援等好處。

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