SlidingMenu重写HorizontalScrollView实现菜单侧滑的效果

来源:互联网 发布:什么是软件销售 编辑:程序博客网 时间:2024/05/01 17:37

HorizontalScrollView类概述

用 于布局的容器,可以放置让用户使用滚动条查看的视图层次结构,允许视图结构比手机的屏幕大。HorizontalScrollView是一种 FrameLayout(框架布局),其子项被滚动查看时是整体移动的,并且子项本身可以是一个有复杂层次结构的布局管理器。一个常见的应用是子项在水平 方向中,用户可以滚动显示顶层水平排列的子项(items)。
详细介绍:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2013/0227/921.html

主程序:

package com.example.ui;import android.content.Context;import android.content.res.TypedArray;import android.util.AttributeSet;import android.util.TypedValue;import android.view.MotionEvent;import android.view.ViewGroup;import android.widget.HorizontalScrollView;import android.widget.LinearLayout;import com.example.intelligentkitchen.R;import com.example.utils.ScreenUtils;public class SlidingMenu extends HorizontalScrollView {    /**     * 屏幕宽度     */    private int mScreenWidth;    /**     * dp     */    private int mMenuRightPadding;    /**     * 菜单的宽度     */    private int mMenuWidth;    private int mHalfMenuWidth;    private boolean isOpen;    private boolean once;    public SlidingMenu(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public SlidingMenu(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        mScreenWidth = ScreenUtils.getScreenWidth(context);        TypedArray a = context.getTheme().obtainStyledAttributes(attrs,                R.styleable.SlidingMenu, defStyle, 0);        int n = a.getIndexCount();        for (int i = 0; i < n; i++) {            int attr = a.getIndex(i);            switch (attr) {            case R.styleable.SlidingMenu_rightPadding:                // 默认50                mMenuRightPadding = a.getDimensionPixelSize(attr,                        (int) TypedValue.applyDimension(                                TypedValue.COMPLEX_UNIT_DIP, 50f,                                getResources().getDisplayMetrics()));                break;            }        }        a.recycle();    }    public SlidingMenu(Context context) {        this(context, null, 0);    }    @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);            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);                isOpen = false;            } else {                this.smoothScrollTo(0, 0);                isOpen = true;            }            return true;        }        return super.onTouchEvent(ev);    }    /**     * 打开菜单     */    public void openMenu() {        if (isOpen)            return;        this.smoothScrollTo(0, 0);        isOpen = true;    }    /**     * 关闭菜单     */    public void closeMenu() {        if (isOpen) {            this.smoothScrollTo(mMenuWidth, 0);            isOpen = false;        }    }    /**     * 切换菜单状态     */    public void toggle() {        if (isOpen) {            closeMenu();        } else {            openMenu();        }    }}

布局类:
使用该自定义控件的xml

<com.example.ui.SlidingMenu xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    xmlns:zhy="http://schemas.android.com/apk/res/com.example.intelligentkitchen"    android:id="@+id/id_menu"    android:layout_width="wrap_content"    android:layout_height="fill_parent"    android:scrollbars="none"    zhy:rightPadding="100dp" >    <LinearLayout        android:layout_width="wrap_content"        android:layout_height="fill_parent"        android:orientation="horizontal" >        <include layout="@layout/layout_menu" />        <LinearLayout            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:orientation="vertical" >            <include layout="@layout/top_search" />            <fragment                android:id="@+id/titles"                android:layout_width="fill_parent"                android:layout_height="fill_parent"                class="com.example.myfragment.foodFragment" />        </LinearLayout>    </LinearLayout></com.example.ui.SlidingMenu>
0 0
原创粉丝点击