Code Monkey home page Code Monkey logo

slidemenu's Introduction

效果图

特性

支持左滑和右滑显示一个菜单项,尚不支持更灵活的方式.

主要代码

class SlideLayout(context: Context?, attrs: AttributeSet? = null) :
    HorizontalScrollView(context, attrs) {
    private lateinit var mLeftMenu: ViewGroup
    private lateinit var mRightMenu: ViewGroup
    private var mOnMenuSelected: OnMenuSelect? = null

    /**
     * 左侧菜单宽度
     */
    private var mLeftMenuWidth = 0

    /**
     * 右侧菜单宽度
     */
    private var mRightMenuWidth = 0

    /**
     * 滑动超过如下比例,就自动完整显示菜单。
     */
    private var mShowMenuRatio = 0.5
    private var hasMeasured = false

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        /**
         * 显式设置中间内容区域宽度
         */
        if (!hasMeasured) {
            val wrapper = getChildAt(0) as LinearLayout
            val content = wrapper.getChildAt(1) as ViewGroup
            content.layoutParams.width = getScreenWidth()
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
    }

    override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
        super.onLayout(changed, l, t, r, b)
        if (changed) {
            val wrapper = getChildAt(0) as LinearLayout
            mLeftMenu = wrapper.getChildAt(0) as ViewGroup
            mRightMenu = wrapper.getChildAt(2) as ViewGroup
            mLeftMenuWidth = mLeftMenu.width
            mRightMenuWidth = mRightMenu.width
            hasMeasured = true
            //隐藏菜单,显示主体内容
            scrollTo(mLeftMenu.width, 0)
        }
    }

    /**
     * 左右滑动,超过菜单本身宽度一定比例,就完整显示菜单。
     */
    override fun onTouchEvent(ev: MotionEvent): Boolean {
        val action = ev.action
        when (action) {
            MotionEvent.ACTION_UP -> {
                val scrollX = scrollX
                if (scrollX > mLeftMenuWidth + mRightMenuWidth * mShowMenuRatio) {
                    smoothScrollTo(mLeftMenuWidth + mRightMenuWidth, 0)
                    mOnMenuSelected?.onSelected(mRightMenu)
                } else if (scrollX > mLeftMenuWidth * mShowMenuRatio) {
                    smoothScrollTo(mLeftMenuWidth, 0)
                } else {
                    mOnMenuSelected?.onSelected(mLeftMenu)
                    smoothScrollTo(0, 0)
                }
                return true
            }
        }
        return super.onTouchEvent(ev)
    }

    private fun getScreenWidth(): Int {
        val wm = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val point = Point()
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            wm.defaultDisplay.getRealSize(point)
        } else {
            wm.defaultDisplay.getSize(point)
        }
        return point.x
    }

    fun setCallback(onMenuSelect: OnMenuSelect) {
        mOnMenuSelected = onMenuSelect
    }
}

interface OnMenuSelect {
    fun onSelected(view: ViewGroup)
}

slidemenu's People

Contributors

yinxing2008 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.