zoukankan      html  css  js  c++  java
  • 28 自定义View侧滑栏

    这里写图片描述
    这里写图片描述

    ScrollMenuView.java

    package com.qf.sxy.customview03.widget;
    
    import android.content.Context;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.widget.HorizontalScrollView;
    import android.widget.LinearLayout;
    
    /**
     * Created by sxy on 2016/9/29.
     * 侧滑菜单   左右滚动
     */
    public class ScrollMenuView extends HorizontalScrollView {
    
        //ScrollView容器  菜单  内容
        private LinearLayout container;
        //菜单
        private LinearLayout menuLayout;
        //内容
        private LinearLayout contentLayout;
    
        //获取屏幕宽度
        private int mScreenWidth =0;
        //菜单展示出来  距离右边屏幕的距离
        private int menuRightPadding = 200;
        //计算出菜单的宽度
        private int menuWidth = 0;
    
        //标记  是否测量过宽度   测量过 不用再次去测
        private boolean isMeasure = false;
    
        private boolean isOpen = false;
    
    
        public ScrollMenuView(Context context) {
            super(context);
        }
    
        public ScrollMenuView(Context context, AttributeSet attrs) {
            super(context, attrs);
            //获取整个屏幕宽度
            mScreenWidth = getResources().getDisplayMetrics().widthPixels;
        }
    
    
        /**
         * 测量自身容器大小
         * @param widthMeasureSpec
         * @param heightMeasureSpec
         */
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    
            if(!isMeasure){
    
                //获取第一个子元素  容器Layout
                container = (LinearLayout) getChildAt(0);
                //获取菜单
                menuLayout = (LinearLayout) container.getChildAt(0);
                //获取 内容
                contentLayout = (LinearLayout) container.getChildAt(1);
                //得到菜单的宽度
                menuWidth = mScreenWidth-menuRightPadding;
                //设置菜单的宽度
                menuLayout.getLayoutParams().width = menuWidth;
                //设置内容宽度大小
                contentLayout.getLayoutParams().width = mScreenWidth;
                //标记测量过了
                isMeasure = true;
            }
    
    
    
        }
    
        /**
         * 子View的位置
         */
        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
            super.onLayout(changed, l, t, r, b);
            //如果改变让ScrollView进行滑动   默认状态  隐藏
            if(changed){
                scrollTo(menuWidth,0);
            }
    
        }
    
    
        @Override
        public boolean onTouchEvent(MotionEvent ev) {
            switch (ev.getAction()){
                case MotionEvent.ACTION_UP://抬起
    
                    //ScrollView滑动距离
                    int scrollX = getScrollX();
    
                    //x轴滑动的距离 大于菜单宽度1/2 关闭菜单
                    if(scrollX>menuWidth/2){
                        smoothScrollTo(menuWidth,0);
                        Log.e("fmy","关闭");
                        isOpen = true;
                    }else{
                        //x轴滑动的距离 小于菜单宽度1/2  打开菜单
                        smoothScrollTo(0,0);
                        Log.e("fmy","开启");
                        isOpen = false;
                    }
                    //Up事件自身处理
                    return true;
            }
            //Down   move的事件
            //交给父类处理
            return super.onTouchEvent(ev);
        }
    
        //开关
        public void toggle(){
            Log.e("AAA","==>"+isOpen);
            if(isOpen){
                 closeContent();
            }else{
                openContent();
            }
    
        }
    
        //打开内容
        private void openContent() {
            if(isOpen){
                return;
            }
            Log.e("fmy","关闭");
            smoothScrollTo(menuWidth,0);
            isOpen = true;
        }
    
        //打开菜单
        private void closeContent() {
            if(!isOpen){
                return;
            }
            Log.e("fmy","开启");
            smoothScrollTo(0,0);
            isOpen = false;
        }
    }
    

    MainActivity.java

    package com.qf.sxy.customview03;
    
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    
    import com.qf.sxy.customview03.widget.ScrollMenuView;
    
    public class MainActivity extends AppCompatActivity {
    
        private ScrollMenuView scrollMenuView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            scrollMenuView = ((ScrollMenuView) findViewById(R.id.scrollMenuView));
    
        }
    
        //开关
        public void Myclick(View view) {
            scrollMenuView.toggle();
        }
    }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <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"
        tools:context="com.qf.sxy.customview03.MainActivity">
    
        <com.qf.sxy.customview03.widget.ScrollMenuView
            android:id="@+id/scrollMenuView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
    
           >
          "<!--  android:scrollbars="none-->
            <LinearLayout
                android:id="@+id/container"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent">
                <!--菜单-->
                <LinearLayout
                    android:id="@+id/menu_layout"
                    android:layout_width="wrap_content"
                    android:orientation="vertical"
                    android:background="@mipmap/img_frame_background"
                    android:layout_height="match_parent">
    
                </LinearLayout>
                <!--内容-->
                <LinearLayout
                    android:id="@+id/content_layout"
                    android:orientation="vertical"
                    android:background="@mipmap/qq"
                    android:layout_width="wrap_content"
                    android:layout_height="match_parent">
                    <Button
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="开关"
                        android:onClick="Myclick"/>
    
                </LinearLayout>
    
            </LinearLayout>
    
    
        </com.qf.sxy.customview03.widget.ScrollMenuView>
    </RelativeLayout>
    
  • 相关阅读:
    复习总结
    python 之Tornado
    MySQL 同一Windows系统上安装多个数据库
    CSS 轻松搞定元素(标签)居中问题
    Linux 解决Deepin深度系统无法在root用户启动Google Chrome浏览器的问题
    Django Windows+IIS+wfastcgi 环境下部署
    Django RestFramework(DRF)类视图
    php+ajax实现拖动滚动条分批加载请求加载数据
    Jquery+php鼠标滚动到页面底部自动加载更多内容,使用分页
    jQuery+ajax实现滚动到页面底部自动加载图文列表效果
  • 原文地址:https://www.cnblogs.com/muyuge/p/6152164.html
Copyright © 2011-2022 走看看