1. 程式人生 > >簡單的方法實現側滑功能

簡單的方法實現側滑功能

首先新建一個類

package com.example.util;



import android.content.Context;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;




public class SlidingMenu extends HorizontalScrollView  
{  
    /** 
     * 螢幕寬度 
     */  
    private int mScreenWidth;  
    /** 
     * dp 
     */  
    private int mMenuRightPadding = 50;  
    /** 
     * 選單的寬度 
     */  
    private int mMenuWidth;  
    private int mHalfMenuWidth;  
  
    private boolean once;  
  
    public SlidingMenu(Context context, AttributeSet attrs)  
    {  
        super(context, attrs);  
        //mScreenWidth = ScreenUtils.getScreenWidth(context);
        WindowManager wm=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        mScreenWidth= wm.getDefaultDisplay().getWidth();
    }  
  
    @Override  
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)  
    {  
        /** 
         * 顯示的設定一個寬度 
         */  
        if (!once)  
        {  
            LinearLayout wrapper = (LinearLayout) getChildAt(0);  
            ViewGroup menu = (ViewGroup) wrapper.getChildAt(0);  
            ViewGroup content = (ViewGroup) wrapper.getChildAt(1);  
            // dp to px  
            mMenuRightPadding = (int) TypedValue.applyDimension(  
                    TypedValue.COMPLEX_UNIT_DIP, mMenuRightPadding, content  
                            .getResources().getDisplayMetrics());  
  
            mMenuWidth = mScreenWidth - mMenuRightPadding;  
            mHalfMenuWidth = mMenuWidth / 2;  
            menu.getLayoutParams().width = mMenuWidth;  
            content.getLayoutParams().width = mScreenWidth;  
  
        }  
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);  
  
    }  
  
    @Override  
    protected void onLayout(boolean changed, int l, int t, int r, int b)  
    {  
        super.onLayout(changed, l, t, r, b);  
        if (changed)  
        {  
            // 將選單隱藏  
            this.scrollTo(mMenuWidth, 0);  
            once = true;  
        }  
    }  
  
    @Override  
    public boolean onTouchEvent(MotionEvent ev)  
    {  
        int action = ev.getAction();  
        switch (action)  
        {  
        // Up時,進行判斷,如果顯示區域大於選單寬度一半則完全顯示,否則隱藏  
        case MotionEvent.ACTION_UP:  
            int scrollX = getScrollX();  
            if (scrollX > mHalfMenuWidth)  
                this.smoothScrollTo(mMenuWidth, 0);  
            else  
                this.smoothScrollTo(0, 0);  
            return true;  
        }  
        return super.onTouchEvent(ev);  
    }  
  

}  

然後在佈局檔案中使用

  1. <com.example.util.SlidingMenuxmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:scrollbars="none">
  6.     <LinearLayout
  7.         android:layout_width
    ="wrap_content"
  8.         android:layout_height="wrap_content"
  9.         android:orientation="horizontal">
  10.    <!--選單佈局  -->
  11.         <includelayout="@layout/layout_menu"/>
  12.         <!--主佈局  -->
  13.         <LinearLayout
  14.             android:layout_width="wrap_content"
  15.             android:layout_height
    ="wrap_content"
    >
  16.         </LinearLayout>
  17.     </LinearLayout>
  18. </com.example.util.SlidingMenu>

這樣就可以實現側滑了