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关闭时调用

  setOnDrawerOpenListener(SlidingDrawer.OnDrawerOpenListener onDrawerOpenListener):SlidingDrawer打开时调用

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

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


package com.loaderman.slidingdrawerdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SlidingDrawer; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity { private SlidingDrawer mDrawer;
private ImageView mImageView;
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawer = (SlidingDrawer) findViewById(R.id.slidingdraw);
mImageView = (ImageView) findViewById(R.id.handle);
mListView = (ListView) findViewById(R.id.content);
mListView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,getData()));
//监听抽屉打开
mDrawer.setOnDrawerOpenListener(new SlidingDrawer.OnDrawerOpenListener() {
@Override
public void onDrawerOpened() { }
});
//抽屉关闭监听
mDrawer.setOnDrawerCloseListener(new SlidingDrawer.OnDrawerCloseListener() {
@Override
public void onDrawerClosed() { }
}); }
private List<String> getData(){ List<String> data = new ArrayList<String>();
data.add("测试数据1");
data.add("测试数据2");
data.add("测试数据3");
data.add("测试数据4");
data.add("测试数据5");
return data;
} }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SlidingDrawer
android:layout_alignParentBottom="true"
android:id="@+id/slidingdraw"
android:layout_width="match_parent"
android:layout_height="300dp"
android:handle="@+id/handle"
android:content="@+id/content"> <!--必须设置handle和content-->
<ListView
android:id="@id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<ImageView android:id="@id/handle"
android:src="@drawable/drawer_bg"
android:layout_width="88dip"
android:layout_height="44dip"
android:contentDescription="TODO"/>
</SlidingDrawer> </RelativeLayout>

效果图:

使用SlidingDrawer(滑动式抽屉)实现抽屉效果-LMLPHP

05-22 12:55