Android筆記:SlidingDrawer

一、概述

專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)康巴什免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了成百上千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

 抽屜控件,官方已不建議用;但在某些需求下直接使用這個(gè)控件還是相當(dāng)方便的。


<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打開或關(guān)閉抽屜。 默認(rèn)是true。(如果是false,用戶必須通過拖動(dòng),滑動(dòng)或者使用軌跡球。)

android:animateOnClick

顧名思義,點(diǎn)擊的時(shí)候是否有動(dòng)畫。默認(rèn)是true。

android:bottomOffset

“手柄”距離SlidingDrawer底部的額外距離 。

android:content

SlidingDrawer的內(nèi)容。

android:handle

SlidingDrawer的“手柄”。

android:orientation

SlidingDrawer的方向。

android:topOffset

“手柄”距離SlidingDrawer頂部的額外距離 。

 

2.一些重要的方法:

void setOnDrawerCloseListener (SlidingDrawer.OnDrawerCloseListener onDrawerCloseListener)

設(shè)置一個(gè)監(jiān)聽器,用來接收當(dāng)抽屜被關(guān)閉時(shí)候的通知。

void setOnDrawerOpenListener (SlidingDrawer.OnDrawerOpenListener onDrawerOpenListener)

Since: API Level 3

設(shè)置一個(gè)監(jiān)聽器,用來接收當(dāng)抽屜被打開的時(shí)候的通知。

void setOnDrawerScrollListener (SlidingDrawer.OnDrawerScrollListener onDrawerScrollListener)

設(shè)置一個(gè)監(jiān)聽器,用來接收當(dāng)抽屜處于正在打開或者正在結(jié)束的滾動(dòng)時(shí)候的通知。

animateClose():

使用動(dòng)畫關(guān)閉抽屜。

animateOpen ():

使用動(dòng)畫打開抽屜

getContent():

獲取內(nèi)容

isMoving():

指示SlidingDrawer是否在移動(dòng)。

isOpened():

指示SlidingDrawer是否已全部打開

lock():

屏蔽觸摸事件。

unlock():

解除屏蔽觸摸事件。

toggle():

切換打開和關(guān)閉的抽屜SlidingDrawer。

3.一個(gè)簡單的例子:

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);
                    }
                });    
    }
}

二、可監(jiān)聽按鈕點(diǎn)擊事件的自定義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:可監(jiān)聽按鈕點(diǎn)擊事件
 * @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在屏幕低端,而不會(huì)填滿整個(gè)屏幕,同時(shí)handle按鈕可點(diǎn)擊的自定義SlidingDrawer


注:解決SlidingDrawer的高度設(shè)置為wrap_content時(shí)無法做到非全屏的問題。

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在屏幕低端,而不會(huì)填滿整個(gè)屏幕,同時(shí)handle按鈕可點(diǎn)擊
 * @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);
    }
    
}

網(wǎng)站標(biāo)題:Android筆記:SlidingDrawer
URL標(biāo)題:http://bm7419.com/article38/jjsipp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT商城網(wǎng)站、小程序開發(fā)、定制開發(fā)、外貿(mào)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)公司