Android笔记:SlidingDrawer
一、概述
抽屉控件,官方已不建议用;但在某些需求下直接使用这个控件还是相当方便的。
<SlidingDrawerandroid:id="@+id/drawer"android:layout_width="match_parent"android:layout_height="match_parent"android:handle="@+id/handle"android:content="@+id/content"><ImageViewandroid:id="@+id/handle"android:layout_width="88dip"android:layout_height="44dip"/><GridViewandroid: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.一些重要的方法:
voidsetOnDrawerCloseListener(SlidingDrawer.OnDrawerCloseListeneronDrawerCloseListener)
设置一个监听器,用来接收当抽屉被关闭时候的通知。
voidsetOnDrawerOpenListener(SlidingDrawer.OnDrawerOpenListeneronDrawerOpenListener)
Since:API Level 3
设置一个监听器,用来接收当抽屉被打开的时候的通知。
voidsetOnDrawerScrollListener(SlidingDrawer.OnDrawerScrollListeneronDrawerScrollListener)
设置一个监听器,用来接收当抽屉处于正在打开或者正在结束的滚动时候的通知。
animateClose():
使用动画关闭抽屉。
animateOpen():
使用动画打开抽屉
getContent():
获取内容
isMoving():
指示SlidingDrawer是否在移动。
isOpened():
指示SlidingDrawer是否已全部打开
lock():
屏蔽触摸事件。
unlock():
解除屏蔽触摸事件。
toggle():
切换打开和关闭的抽屉SlidingDrawer。
publicclassSlidingDrawerDemoActivityextendsActivity{privateSlidingDrawermyDrawer;privateImageViewmyImageView;privateGridViewmyGridView;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){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(){@OverridepublicvoidonDrawerOpened(){myImageView.setImageResource(R.drawable.down);}});myDrawer.setOnDrawerCloseListener(newSlidingDrawer.OnDrawerCloseListener(){@OverridepublicvoidonDrawerClosed(){myImageView.setImageResource(R.drawable.up);}});}}
二、可监听按钮点击事件的自定义SlidingDrawer
importandroid.content.Context;importandroid.graphics.Rect;importandroid.util.AttributeSet;importandroid.view.MotionEvent;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.SlidingDrawer;/***自定义SlidingDrawer:可监听按钮点击事件*@authorzeng**/publicclassClickableSlidingDrawerextendsSlidingDrawer{privateViewGroupmHandleLayout;privatefinalRectmHitRect=newRect();publicClickableSlidingDrawer(Contextcontext,AttributeSetattrs,intdefStyle){super(context,attrs,defStyle);}publicClickableSlidingDrawer(Contextcontext,AttributeSetattrs){super(context,attrs);}@OverrideprotectedvoidonFinishInflate(){super.onFinishInflate();Viewhandle=getHandle();if(handleinstanceofViewGroup){mHandleLayout=(ViewGroup)handle;}}@OverridepublicbooleanonInterceptTouchEvent(MotionEventevent){if(mHandleLayout!=null){intchildCount=mHandleLayout.getChildCount();inthandleClickX=(int)(event.getX()-mHandleLayout.getX());inthandleClickY=(int)(event.getY()-mHandleLayout.getY());RecthitRect=mHitRect;for(inti=0;i<childCount;i++){ViewchildView=mHandleLayout.getChildAt(i);childView.getHitRect(hitRect);if(hitRect.contains(handleClickX,handleClickY)){returnfalse;}}}returnsuper.onInterceptTouchEvent(event);}}
三、控制SlidingDrawer在屏幕低端,而不会填满整个屏幕,同时handle按钮可点击的自定义SlidingDrawer
注:解决SlidingDrawer的高度设置为wrap_content时无法做到非全屏的问题。
importandroid.content.Context;importandroid.graphics.Rect;importandroid.util.AttributeSet;importandroid.view.MotionEvent;importandroid.view.View;importandroid.view.ViewGroup;importandroid.widget.SlidingDrawer;/***自定义SlidingDrawer,控制SlidingDrawer在屏幕低端,而不会填满整个屏幕,同时handle按钮可点击*@authorzeng**/publicclassClickableWrapSlidingDrawerextendsSlidingDrawer{privateViewGroupmHandleLayout;privatefinalRectmHitRect=newRect();privatebooleanmVertical;privateintmTopOffset;publicClickableWrapSlidingDrawer(Contextcontext,AttributeSetattrs,intdefStyle){super(context,attrs,defStyle);intorientation=attrs.getAttributeIntValue("android","orientation",ORIENTATION_VERTICAL);mTopOffset=attrs.getAttributeIntValue("android","topOffset",0);mVertical=(orientation==SlidingDrawer.ORIENTATION_VERTICAL);}publicClickableWrapSlidingDrawer(Contextcontext,AttributeSetattrs){super(context,attrs);intorientation=attrs.getAttributeIntValue("android","orientation",ORIENTATION_VERTICAL);mTopOffset=attrs.getAttributeIntValue("android","topOffset",0);mVertical=(orientation==SlidingDrawer.ORIENTATION_VERTICAL);}@OverrideprotectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec){intwidthSpecMode=MeasureSpec.getMode(widthMeasureSpec);intwidthSpecSize=MeasureSpec.getSize(widthMeasureSpec);intheightSpecMode=MeasureSpec.getMode(heightMeasureSpec);intheightSpecSize=MeasureSpec.getSize(heightMeasureSpec);finalViewhandle=getHandle();finalViewcontent=getContent();measureChild(handle,widthMeasureSpec,heightMeasureSpec);if(mVertical){intheight=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{intwidth=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);}@OverrideprotectedvoidonFinishInflate(){super.onFinishInflate();Viewhandle=getHandle();if(handleinstanceofViewGroup){mHandleLayout=(ViewGroup)handle;}}@OverridepublicbooleanonInterceptTouchEvent(MotionEventevent){if(mHandleLayout!=null){intchildCount=mHandleLayout.getChildCount();inthandleClickX=(int)(event.getX()-mHandleLayout.getX());inthandleClickY=(int)(event.getY()-mHandleLayout.getY());RecthitRect=mHitRect;for(inti=0;i<childCount;i++){ViewchildView=mHandleLayout.getChildAt(i);childView.getHitRect(hitRect);if(hitRect.contains(handleClickX,handleClickY)){returnfalse;}}}returnsuper.onInterceptTouchEvent(event);}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。