安卓中如何实现左滑菜单
发布日期:2021-06-29 18:30:24 浏览次数:3 分类:技术文章

本文共 2854 字,大约阅读时间需要 9 分钟。

效果图

特性

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

主要代码

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)}

完整源代码

https://gitee.com/cxyzy1/slideMenu

转载地址:https://cxyxy.blog.csdn.net/article/details/109121643 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:安卓流式布局FlowLayout样例
下一篇:安卓弹出框顶部距离过大(点击弹出框外围时,弹出框未消失)

发表评论

最新留言

不错!
[***.144.177.141]2024年04月25日 13时25分01秒