Android笔记:SlidingDrawer
一、概述
抽屉控件,官方已不建议用;但在某些需求下直接使用这个控件还是相当方便的。
<SlidingDrawer android:id="@+id/drawer" android:layout_width="match_parent" android:layout_height="match_parent" android:handle="@+id/handle" android:content="@+id/content"> <ImageView android:id="@+id/handle" android:layout_width="88dip" android:layout_height="44dip" /> <GridView android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" /> </SlidingDrawer>
1.XML文件中的属性
属性名称 | 描述 |
android:allowSingleTap | 是否可通过单击handle打开或关闭抽屉。 默认是true。(如果是false,用户必须通过拖动,滑动或者使用轨迹球。) |
android:animateOnClick | 顾名思义,点击的时候是否有动画。默认是true。 |
android:bottomOffset | “手柄”距离SlidingDrawer底部的额外距离 。 |
android:content | SlidingDrawer的内容。 |
android:handle | SlidingDrawer的“手柄”。 |
android:orientation | SlidingDrawer的方向。 |
android:topOffset | “手柄”距离SlidingDrawer顶部的额外距离 。 |
2.一些重要的方法:
void setOnDrawerCloseListener (SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener)
设置一个监听器,用来接收当抽屉被关闭时候的通知。
void setOnDrawerOpenListener (SlidingDrawer.OnDrawerOpenListener onDrawerOpenListener)
Since: API Level 3
设置一个监听器,用来接收当抽屉被打开的时候的通知。
void setOnDrawerScrollListener (SlidingDrawer.OnDrawerScrollListener onDrawerScrollListener)
设置一个监听器,用来接收当抽屉处于正在打开或者正在结束的滚动时候的通知。
animateClose():
使用动画关闭抽屉。
animateOpen ():
使用动画打开抽屉
getContent():
获取内容
isMoving():
指示SlidingDrawer是否在移动。
isOpened():
指示SlidingDrawer是否已全部打开
lock():
屏蔽触摸事件。
unlock():
解除屏蔽触摸事件。
toggle():
切换打开和关闭的抽屉SlidingDrawer。
3.一个简单的例子:
public class SlidingDrawerDemoActivity extends Activity { private SlidingDrawer myDrawer; private ImageView myImageView; private GridView myGridView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); myDrawer = (SlidingDrawer) findViewById(R.id.drawer); myImageView = (ImageView)findViewById(R.id.handle); myGridView = (GridView)findViewById(R.id.content); myDrawer.setOnDrawerOpenListener(newSlidingDrawer.OnDrawerOpenListener() { @Override public void onDrawerOpened() { myImageView.setImageResource(R.drawable.down); } }); myDrawer.setOnDrawerCloseListener(newSlidingDrawer.OnDrawerCloseListener() { @Override public void onDrawerClosed() { myImageView.setImageResource(R.drawable.up); } }); } }
二、可监听按钮点击事件的自定义SlidingDrawer
import android.content.Context; import android.graphics.Rect; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.SlidingDrawer; /** * 自定义SlidingDrawer:可监听按钮点击事件 * @author zeng * */ public class ClickableSlidingDrawer extends SlidingDrawer { private ViewGroup mHandleLayout; private final Rect mHitRect = new Rect(); public ClickableSlidingDrawer(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public ClickableSlidingDrawer(Context context, AttributeSet attrs) { super(context, attrs); } @Override protected void onFinishInflate() { super.onFinishInflate(); View handle = getHandle(); if (handle instanceof ViewGroup) { mHandleLayout = (ViewGroup) handle; } } @Override public boolean onInterceptTouchEvent(MotionEvent event) { if (mHandleLayout != null) { int childCount = mHandleLayout.getChildCount(); int handleClickX = (int) (event.getX() - mHandleLayout.getX()); int handleClickY = (int) (event.getY() - mHandleLayout.getY()); Rect hitRect = mHitRect; for (int i = 0; i < childCount; i++) { View childView = mHandleLayout.getChildAt(i); childView.getHitRect(hitRect); if (hitRect.contains(handleClickX, handleClickY)) { return false; } } } return super.onInterceptTouchEvent(event); } }
三、控制SlidingDrawer在屏幕低端,而不会填满整个屏幕,同时handle按钮可点击的自定义SlidingDrawer
注:解决SlidingDrawer的高度设置为wrap_content时无法做到非全屏的问题。
import android.content.Context; import android.graphics.Rect; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.SlidingDrawer; /** * 自定义SlidingDrawer,控制SlidingDrawer在屏幕低端,而不会填满整个屏幕,同时handle按钮可点击 * @author zeng * */ public class ClickableWrapSlidingDrawer extends SlidingDrawer { private ViewGroup mHandleLayout; private final Rect mHitRect = new Rect(); private boolean mVertical; private int mTopOffset; public ClickableWrapSlidingDrawer(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); int orientation = attrs.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL); mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0); mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL); } public ClickableWrapSlidingDrawer(Context context, AttributeSet attrs) { super(context, attrs); int orientation = attrs.getAttributeIntValue("android", "orientation", ORIENTATION_VERTICAL); mTopOffset = attrs.getAttributeIntValue("android", "topOffset", 0); mVertical = (orientation == SlidingDrawer.ORIENTATION_VERTICAL); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); final View handle = getHandle(); final View content = getContent(); measureChild(handle, widthMeasureSpec, heightMeasureSpec); if (mVertical) { int height = heightSpecSize - handle.getMeasuredHeight() - mTopOffset; content.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(height, heightSpecMode)); heightSpecSize = handle.getMeasuredHeight() + mTopOffset + content.getMeasuredHeight(); widthSpecSize = content.getMeasuredWidth(); if (handle.getMeasuredWidth() > widthSpecSize) widthSpecSize = handle.getMeasuredWidth(); } else { int width = widthSpecSize - handle.getMeasuredWidth() - mTopOffset; getContent().measure(MeasureSpec.makeMeasureSpec(width, widthSpecMode), heightMeasureSpec); widthSpecSize = handle.getMeasuredWidth() + mTopOffset + content.getMeasuredWidth(); heightSpecSize = content.getMeasuredHeight(); if (handle.getMeasuredHeight() > heightSpecSize) heightSpecSize = handle.getMeasuredHeight(); } setMeasuredDimension(widthSpecSize, heightSpecSize); } @Override protected void onFinishInflate() { super.onFinishInflate(); View handle = getHandle(); if (handle instanceof ViewGroup) { mHandleLayout = (ViewGroup) handle; } } @Override public boolean onInterceptTouchEvent(MotionEvent event) { if (mHandleLayout != null) { int childCount = mHandleLayout.getChildCount(); int handleClickX = (int) (event.getX() - mHandleLayout.getX()); int handleClickY = (int) (event.getY() - mHandleLayout.getY()); Rect hitRect = mHitRect; for (int i = 0; i < childCount; i++) { View childView = mHandleLayout.getChildAt(i); childView.getHitRect(hitRect); if (hitRect.contains(handleClickX, handleClickY)) { return false; } } } return super.onInterceptTouchEvent(event); } }
本文出自 “天空没有痕迹但我飞过” 博客,请务必保留此出处http://glblong.blog.51cto.com/3058613/1548354
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。