十年网站开发经验 + 多家企业客户 + 靠谱的建站团队
量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决
一、概述
专注于为中小企业提供成都网站建设、网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业康巴什免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了成百上千家企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
抽屉控件,官方已不建议用;但在某些需求下直接使用这个控件还是相当方便的。
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。
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); } }