自定义控件之-----SlidingMenu

slidemenu.gif

介绍

SlidingMenu实现类似QQ的侧拉面板效果
继承ViewGroup,完全自定义控件
流程:
1、测量,测量自己的宽高,测量测量孩子的宽高
2、布局,给孩子设置坐标位置

1、继承ViewGroup

        public class SlidingMenu extends ViewGroup {

            public SlidingMenu(Context context, AttributeSet attrs) {
                super(context, attrs);
            }
            
            @Override
            protected void onLayout(boolean changed, int l, int t, int r, int b) {

            }
        
        }

2、布局中使用SlidingMenu



        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
            <com.itheima.slidemenu94.SlidingMenu
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
                <!-- 引入左侧菜单布局 -->
                <include layout="@layout/left"/>
                <!-- 引入正文布局 -->
                <include layout="@layout/content"/>
            </com.itheima.slidemenu94.SlidingMenu>
        </RelativeLayout>

3、左侧菜单布局



        <?xml version="1.0" encoding="utf-8"?>
        <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="240dp"
            android:layout_height="match_parent" >
            <LinearLayout
                android:layout_width="240dp"
                android:layout_height="match_parent"
                android:background="@drawable/menu_bg"
                android:orientation="vertical" >
        
                <TextView
                    style="@style/MenuItemStyle"
                    android:background="#66663333"
                    android:drawableLeft="@drawable/tab_news"
                    android:text="新闻" />
        
                <TextView
                    style="@style/MenuItemStyle"
                    android:drawableLeft="@drawable/tab_read"
                    android:text="订阅" />
        
                <TextView
                    style="@style/MenuItemStyle"
                    android:drawableLeft="@drawable/tab_local"
                    android:text="本地" />
        
                <TextView
                    style="@style/MenuItemStyle"
                    android:drawableLeft="@drawable/tab_ties"
                    android:text="跟帖" />
        
                <TextView
                    style="@style/MenuItemStyle"
                    android:drawableLeft="@drawable/tab_pics"
                    android:text="图片" />
        
                <TextView
                    style="@style/MenuItemStyle"
                    android:drawableLeft="@drawable/tab_ugc"
                    android:text="话题" />
        
                <TextView
                    style="@style/MenuItemStyle"
                    android:drawableLeft="@drawable/tab_vote"
                    android:text="投票" />
        
                <TextView
                    style="@style/MenuItemStyle"
                    android:drawableLeft="@drawable/tab_focus"
                    android:text="聚合阅读" />
            </LinearLayout>
        
        </ScrollView>

        * 样式/slideMenu94/res/values/styles.xml

        <style name="MenuItemStyle">
            <item name="android:layout_width">match_parent</item>
            <item name="android:layout_height">wrap_content</item>
            <item name="android:drawablePadding">15dp</item>
            <item name="android:padding">10dp</item>
            <item name="android:textColor">#fff</item>
            <!-- <item name="android:background">#66663333</item> -->
            <item name="android:textSize">30sp</item>
            <item name="android:clickable">true</item>
            <item name="android:background">@drawable/menu_item_selector</item>
        </style>
    
        * 菜单条目背景选择器/slideMenu94/res/drawable/menu_item_selector.xml

        <?xml version="1.0" encoding="utf-8"?>
        <selector xmlns:android="http://schemas.android.com/apk/res/android" >
            <item android:state_pressed="true" android:drawable="@color/blue"></item>
            <item android:drawable="@android:color/transparent"></item>
        </selector>

4、正文布局



        <?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" >
        
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/top_bar_bg"
                android:gravity="center_vertical"
                android:orientation="horizontal" >
        
                <ImageButton
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/main_back" />
        
                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:background="@drawable/top_bar_divider" />
        
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="20dp"
                    android:gravity="center"
                    android:text="黑马新闻"
                    android:textColor="#fff"
                    android:textSize="30sp" />
            </LinearLayout>
        
            <TextView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="钓鱼岛是中国的,\nxxx世界的"
                android:textColor="#000"
                android:textSize="25sp" />
        
        </LinearLayout>


5、测量自己和孩子的宽高




        // 测量 widthMeasureSpec Activity的宽 heightMeasureSpec Activity的高
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            // 测量自己的宽高
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            // 测量孩子的宽高
            // 获取第一个孩子
            View menu = getChildAt(0);
            menu.measure(menu.getLayoutParams().width, heightMeasureSpec);
            // 获取第二个孩子
            View content = getChildAt(1);
            content.measure(widthMeasureSpec, heightMeasureSpec);
        }

6、布局孩子的位置

 

        // 布局 给孩子设置位置 ,int l, int t, int r, int b 自己的左上点和右下点坐标
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            // 设置左边菜单的位置
            View menu = getChildAt(0);
            menu.layout(-menu.getMeasuredWidth(), 0, 0, b);
            // 设置正文的位置
            View content = getChildAt(1);
            content.layout(l, t, r, b);
        }

7、处理事件,让控件随手指移动



        // 移动到某一个目的地,正数相当于屏幕往右移动
        scrollTo(200, 0);
        // 相当于当前位置,移动一个增量,正数相当于屏幕往右移动
        scrollBy(-200, 0);

        // 处理事件 
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                int moveX = (int) event.getX();
                // 手指移动的距离
                int diffX = downX - moveX;
                // 让整个控件移动手指移动的距离
                scrollBy(diffX, 0);
                // 给downX重新赋值
                downX = moveX;
                break;
            case MotionEvent.ACTION_UP:
                break;
    
            default:
                break;
            }
            return true;// 消费掉事件
        }

8、控制移动时不能操作边界




        case MotionEvent.ACTION_MOVE:
            int moveX = (int) event.getX();
            // 手指移动的距离
            int diffX = downX - moveX;
            // 控制移动的边界
            // 获取当前控件移动的距离,相当于 0,0 原点
            int scrollX = getScrollX()+diffX;
            System.out.println("diffX:" + diffX + ":scrollX:"+scrollX);
            if(scrollX<-getChildAt(0).getMeasuredWidth()){// 如果超出最左边,直接移动到最左边
                scrollTo(-getChildAt(0).getMeasuredWidth(), 0);
            }else if(scrollX>0){// 如果超出最右边,直接移动到0位置
                scrollTo(0, 0);
            }else{
                // 让整个控件移动手指移动的距离
                scrollBy(diffX, 0);
            }
            // 给downX重新赋值
            downX = moveX;
            break;

9、手指抬起时,根据当前控件移动的距离判断展示正文或菜单



        case MotionEvent.ACTION_UP:
            // 手指抬起时,根据当前控件移动的位置,与左侧菜单的一半位置进行比较
            // 如果当前控件移动的位置>左侧菜单的一半位置,左侧菜单展示的只有一小半,让控件展示正文布局
            // 如果当前控件移动的位置<左侧菜单的一半位置,左侧菜单展示的只有一大半,让控件展示菜单布局
            int scrollX2 = getScrollX();
            isShowMenu = scrollX2<-getChildAt(0).getMeasuredWidth()/2;
            showMenu();
            break;

        private void showMenu() {
            if(isShowMenu){// 展示左侧菜单
                scrollTo(-getChildAt(0).getMeasuredWidth(), 0);
            }else{// 展示正文
                scrollTo(0, 0);
            }
        }

10、平滑移动


        private void showMenu() {
            // startX 起点x坐标
            // startY 起点y坐标
            // dx dy 起点到终点的差值
            // duration 从起点移动一个差值 用时
            int startX = getScrollX();
            int dx = 0;
            if(isShowMenu){// 展示左侧菜单
                dx = -getChildAt(0).getMeasuredWidth()-startX;
            }else{// 展示正文
                dx = 0 - startX;
            }
            int duration = Math.abs(dx) * 10; 
            if(duration>1000){
                duration = 1000;
            }
            scroller.startScroll(startX, 0, dx, 0, duration);// 初始化平滑移动的数据
            invalidate();
        }

        // invalidate 会触发computeScroll
        @Override
        public void computeScroll() {
            if (scroller.computeScrollOffset()) {// 开始计算下一个目的地,返回值 true说明移动到终点,false已经移动到终点
                int currX = scroller.getCurrX();// 获取到计算的目的地x坐标
                scrollTo(currX, 0);
                invalidate();
            }
        }

11、提供方法打开或关闭菜单



        // 提供打开关闭菜单方法
        public void toggle(){
            isShowMenu=!isShowMenu;
            showMenu();
        }

12、处理事件冲突



        // 拦截事件
        @Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
            // return super.onInterceptTouchEvent(ev);// 默认return false,不拦截事件
            // 只拦截左右滑动事件
            switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) ev.getX();
                downY = (int) ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                int moveX = (int) ev.getX();
                int moveY = (int) ev.getY();
                
                int diffX = moveX - downX;
                int diffY = moveY - downY;
                if(Math.abs(diffX)>Math.abs(diffY)){// 左右滑动,拦截掉事件
                    return true;
                }
                break;
            }
            return super.onInterceptTouchEvent(ev);
        }

GitHub地址

:https://github.com/zssAndroid/SlideMenu

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 225,015评论 6 522
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 96,373评论 3 404
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 172,253评论 0 368
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 61,069评论 1 300
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 70,079评论 6 400
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 53,550评论 1 314
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 41,911评论 3 429
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 40,892评论 0 279
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 47,435评论 1 324
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 39,465评论 3 346
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 41,586评论 1 355
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 37,178评论 5 351
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 42,920评论 3 340
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 33,356评论 0 25
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 34,493评论 1 277
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 50,151评论 3 381
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 46,663评论 2 366

推荐阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 172,448评论 25 707
  • 我说没有人喜欢漂泊 家 谁都爱 在外面漂久了你会特别渴望温暖 很久没有那种突如其来的幸福能让我开心 生活好像特别平...
    一生都靠浪阅读 514评论 2 0
  • 2017.11.24 周六 【工具】“特别时光”安排和平常不同的特别时光。1.切断电话。2.做双方都喜欢的事情。3...
    岁月莲上写诗阅读 310评论 0 0
  • 20170911 儿说:我要是还上幼儿园多好啊,幼儿园的饭挺好吃的,而且也不用麻烦妈妈早起给我做饭,
    张会新阅读 167评论 0 0