Android中SlidingDrawer(滑动式抽屉)效果源码 (十四)

一、简介

   SlidingDrawer隐藏屏外的内容,并允许用户通过handle以显示隐藏内容。它可以垂直或水平滑动,它有俩个View组成,其一 是可以拖动的handle,其二是隐藏内容的View.它里面的控件必须设置布局,在布局文件中必须指定handle和content.

二、重要属性

  android:allowSingleTap:指示是否可以通过handle打开或关闭

  android:animateOnClick:指示是否当使用者按下手柄打开/关闭时是否该有一个动画。

  android:content:隐藏的内容

  android:handle:handle(手柄)

三、重要方法

  animateClose():关闭时实现动画。

  close():即时关闭

  getContent():获取内容

  isMoving():指示SlidingDrawer是否在移动。

  isOpened():指示SlidingDrawer是否已全部打开

  lock():屏蔽触摸事件。

  setOnDrawerCloseListener(SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener):SlidingDrawer关闭时调用

  unlock():解除屏蔽触摸事件。

  toggle():切换打开和关闭的抽屉SlidingDrawer。

 

四、完整实例

package com.itarchy.slidingdrawer;


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


/**
 * 
 * @E-mail: itarchy@163.com
 * @version 创建时间:2015-11-6下午6:20:25
 * @Des:
 */
public class MainActivity extends Activity {


<span style="white-space:pre">	</span>private SlidingDrawer mDrawer;
<span style="white-space:pre">	</span>private ImageButton imbg;
<span style="white-space:pre">	</span>private Boolean flag = false;
<span style="white-space:pre">	</span>private TextView tv;


<span style="white-space:pre">	</span>@SuppressWarnings("deprecation")
<span style="white-space:pre">	</span>@Override
<span style="white-space:pre">	</span>protected void onCreate(Bundle savedInstanceState) {
<span style="white-space:pre">		</span>// TODO Auto-generated method stub
<span style="white-space:pre">		</span>super.onCreate(savedInstanceState);
<span style="white-space:pre">		</span>setContentView(R.layout.activity_main);


<span style="white-space:pre">		</span>imbg = (ImageButton) findViewById(R.id.handle);
<span style="white-space:pre">		</span>mDrawer = (SlidingDrawer) findViewById(R.id.slidingdrawer);
<span style="white-space:pre">		</span>tv = (TextView) findViewById(R.id.tv);


<span style="white-space:pre">		</span>// "打开"抽屉的监听器
<span style="white-space:pre">		</span>mDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
<span style="white-space:pre">			</span>@Override
<span style="white-space:pre">			</span>public void onDrawerOpened() {
<span style="white-space:pre">				</span>flag = true;


<span style="white-space:pre">				</span>Toast.makeText(getApplication(), "抽屉已经打开", 1000).show();
<span style="white-space:pre">			</span>}


<span style="white-space:pre">		</span>});
<span style="white-space:pre">		</span>// "关闭"抽屉的监听器
<span style="white-space:pre">		</span>mDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {


<span style="white-space:pre">			</span>@Override
<span style="white-space:pre">			</span>public void onDrawerClosed() {
<span style="white-space:pre">				</span>flag = false;


<span style="white-space:pre">				</span>Toast.makeText(getApplication(), "抽屉已经关闭", 1000).show();
<span style="white-space:pre">			</span>}


<span style="white-space:pre">		</span>});


<span style="white-space:pre">		</span>// "正在打开"、"正在关闭"抽屉效果监听器
<span style="white-space:pre">		</span>mDrawer.setOnDrawerScrollListener(new SlidingDrawer.OnDrawerScrollListener() {


<span style="white-space:pre">			</span>@Override
<span style="white-space:pre">			</span>public void onScrollEnded() { // 抽屉结束滑动时执行
<span style="white-space:pre">				</span>tv.setText("结束拖动");
<span style="white-space:pre">			</span>}


<span style="white-space:pre">			</span>@Override
<span style="white-space:pre">			</span>public void onScrollStarted() {// 抽屉开始滑动开始时
<span style="white-space:pre">				</span>tv.setText("开始拖动");
<span style="white-space:pre">			</span>}


<span style="white-space:pre">		</span>});


<span style="white-space:pre">	</span>}


}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffbbbbbb" >

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerInParent="true"
        android:text="内容会被抽屉效果遮挡"
        android:gravity="center"
        android:textColor="#FF0000"
        android:textSize="30sp" />

    <SlidingDrawer
        android:id="@+id/slidingdrawer"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:content="@+id/content"
        android:handle="@+id/handle"
        android:orientation="vertical" >

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

        <LinearLayout
            android:id="@id/content"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="#ffffff" >

            <TextView
                android:id="@+id/tv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:ellipsize="marquee"
                android:gravity="center_vertical|center_horizontal"
                android:text="这是一个滑动式抽屉的示例"
                android:textColor="#000000"
                android:textSize="18sp"
                android:textStyle="bold" />
        </LinearLayout>
    </SlidingDrawer>

</RelativeLayout>

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.itarchy.slidingdrawer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.itarchy.slidingdrawer.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值