您好,登錄后才能下訂單哦!
一、概述
抽屜控件,官方已不建議用;但在某些需求下直接使用這個控件還是相當方便的。
<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。
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); } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。