侧滑菜单(一)

普通侧滑菜单,通过继承自HorizontalScrollView来实现

SidingView.class

/**
 * 
 */
package com.test.sildingmenu.view;

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

/**
 * @author wanggenping
 * 未使用自定义属性
 */
public class SidingView extends HorizontalScrollView {
     private LinearLayout layout;//包含在SidingView内的LinearLayout
     private ViewGroup mMenu;//左侧菜单
     private ViewGroup mContent;//主页面
     private int mScreenWidth;//屏幕宽度
     
     private int mMenuWidth;
     
     
     //dp
     private int mMenuRightPadding;
     
     private boolean once;
     

    /**
     * 未使用自定义属性时调用此方法
     * 
     * @param context
     * @param attrs
     *            (属性)
     */
    public SidingView(Context context, AttributeSet attrs) {
        super(context, attrs);
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        mScreenWidth = outMetrics.widthPixels;
        //dp转换为px
        mMenuRightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 50, context.getResources().getDisplayMetrics());
    }



    /**
     *设置子View的宽和高
     *设置自己的宽和高 
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //由于onMeasure()方法可能被多长调用,所以我们这里只设置一次宽和高,不多长设置
        if(!once){
            layout = (LinearLayout) getChildAt(0);
            //layout中的第一个元素(即:menu)
            mMenu = (ViewGroup) layout.getChildAt(0);
            //layout中的第二个元素(即:主界面)
            mContent = (ViewGroup) layout.getChildAt(1);
            
            mMenuWidth = mMenu.getLayoutParams().width = mScreenWidth - mMenuRightPadding;
            mContent.getLayoutParams().width = mScreenWidth;
            once = true;
        }

        
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
    
    /**
     *通过设置偏移量将menu隐藏
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        if(changed){
            //menu初始状态,隐藏
            this.scrollTo(mMenuWidth, 0);
        }
        
    }
    
    
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        int action = ev.getAction();
        switch (action) {
        case MotionEvent.ACTION_UP:
            //隐藏在左边的宽度
            int scrollX = getScrollX();
            if(scrollX >= mMenuWidth/2){
                //带有动画的滑动
                this.smoothScrollTo(mMenuWidth, 0);
            }else{
                //显示menu
                this.smoothScrollTo(0, 0);
            }
            return true;
            
        }
        return super.onTouchEvent(ev);
    }

}

左侧侧滑菜单的布局文件 view_side_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:gravity="center_vertical">
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/view_side_menu_one_layout"
        android:padding="15dp">
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/view_side_menu_image1"
            android:src="@drawable/icon_base_info"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/view_side_menu_text1"
            android:layout_toRightOf="@+id/view_side_menu_image1"
            android:layout_marginLeft="7dp"
            android:text="第一个item"
            android:layout_centerVertical="true"
            android:textColor="#fff"
            android:textSize="20sp"/>
    </RelativeLayout>
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/view_side_menu_two_layout"
        android:padding="15dp">
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/view_side_menu_image2"
            android:src="@drawable/icon_borrow_description"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/view_side_menu_text2"
            android:layout_toRightOf="@+id/view_side_menu_image2"
            android:layout_marginLeft="7dp"
            android:text="第二个item"
            android:layout_centerVertical="true"
            android:textColor="#fff"
            android:textSize="20sp"/>
    </RelativeLayout>
    <RelativeLayout 
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/view_side_menu_three_layout"
        android:padding="15dp">
        <ImageView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/view_side_menu_image3"
            android:src="@drawable/icon_borrow_user_info"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"/>
        <TextView 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/view_side_menu_text3"
            android:layout_toRightOf="@+id/view_side_menu_image3"
            android:layout_marginLeft="7dp"
            android:text="第三个item"
            android:layout_centerVertical="true"
            android:textColor="#fff"
            android:textSize="20sp"/>
    </RelativeLayout>
    

</LinearLayout>

主界面的布局文件activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="@drawable/main_bg">
    <com.test.sildingmenu.view.SidingView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/activity_main_scrollview"
        android:scrollbars="none">
        <LinearLayout 
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/activity_main_layout"
            android:orientation="horizontal">
            <include 
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/activity_main_side_menu"
                layout="@layout/view_side_menu"/>
            <LinearLayout 
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:id="@+id/activity_main_main_layout"
                android:background="@drawable/loading_bg"
                android:orientation="vertical">
               <Button 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:onClick="toggleMenu"
                android:text="切换菜单"/> 
            </LinearLayout>
            
            
        </LinearLayout>
    </com.test.sildingmenu.view.CustomSidingView >
    
</RelativeLayout>

MainActivity.class

package com.test.sildingmenu;


import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.os.Bundle;

public class MainActivity extends ActionBarActivity {
    private CustomSidingView menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}


你可能感兴趣的:(侧滑菜单(一))