自定义SlidingMenu组件 - 新闻资讯 - 云南小程序开发|云南软件开发|云南网站建设-昆明葵宇信息科技有限公司

159-8711-8523

云南网建设/小程序开发/软件开发

知识

不管是网站,软件还是小程序,都要直接或间接能为您产生价值,我们在追求其视觉表现的同时,更侧重于功能的便捷,营销的便利,运营的高效,让网站成为营销工具,让软件能切实提升企业内部管理水平和效率。优秀的程序为后期升级提供便捷的支持!

您当前位置>首页 » 新闻资讯 » 技术分享 >

自定义SlidingMenu组件

发表时间:2020-10-19

发布人:葵宇科技

浏览次数:16


在网上进修了自定义slidingmenu组件,这里记录下个中的关键点。
SlidingMenu其实是一个HorizontalScrollView,琅绫擎有两个构造,经由过程重写几个办法达到侧滑的效不雅。
起首道理是在LinearLayout外嵌套了HorizontalScrollView,SlidingMenu持续HorizontalScrollView。
重写onMeasure(int widthMeasureSpec, int heightMeasureSpec),onLayout(boolean changed, int l, int t, int r, int b),onTouchEvent(MotionEvent ev)办法
下面是代码
package com.example.slidingmenudemo.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;

import com.example.slidingmenudemo.R;
import com.example.slidingmenudemo.utils.ScreenUtils;
import com.nineoldandroids.view.ViewHelper;

public class SlidingMenu extends HorizontalScrollView {

	// 屏幕的宽度
	private int mScreenWidth;
	// 菜单的宽度
	private int mMenuWidth;
	// 一半菜单的宽度
	private int mHalfMenuWidth;
	// 菜单距离右侧的宽度(像素)
	private int mMenuRightPadding;
	// 菜单距离右侧的宽度(dp)
	private float mDefaultMenuRightPaddingDp = 150f;
	// 是否第一次加载
	private boolean firstIn = true;
	private boolean isOpen = false;
	private ViewGroup menu;
	private ViewGroup content;

	public SlidingMenu(Context context, AttributeSet attrs) {
		super(context, attrs);
		mScreenWidth = ScreenUtils.getScreenWidth(context);

		TypedArray typedArray = context.obtainStyledAttributes(attrs,
				R.styleable.SlidingMenu);
		int n = typedArray.getIndexCount();
		for (int i = 0; i < n; i++) {
			int attr = typedArray.getIndex(i);
			switch (attr) {
			case R.styleable.SlidingMenu_rightPadding:
				mMenuRightPadding = typedArray.getDimensionPixelOffset(attr,
						ScreenUtils.dp2px(context, mDefaultMenuRightPaddingDp));
				break;
			}
		}
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// 如不雅是第一次加载,要对每个构造(Menu,Content)的宽度从新设定(留意都是像素为单位)
		if (firstIn) {
			LinearLayout wrapper = (LinearLayout) getChildAt(0);
			menu = (ViewGroup) wrapper.getChildAt(0);
			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);
			firstIn = false;
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		ev.getAction();
		switch (ev.getAction()) {
		case MotionEvent.ACTION_UP:
			if (getScrollX() > mHalfMenuWidth) {
				// 打开菜单
				this.smoothScrollTo(mMenuWidth, 0);
				isOpen = true;
			} else {
				// 封闭菜单
				this.smoothScrollTo(0, 0);
				isOpen = false;
			}
			return true;

		}
		return super.onTouchEvent(ev);
	}

	// 打开Menu
	public void openMenu() {
		if (!isOpen) {
			this.smoothScrollTo(0, 0);
			isOpen = true;
		}
	}

	// 打开Menu
	public void closeMenu() {
		if (isOpen) {
			this.smoothScrollTo(mMenuWidth, 0);
			isOpen = false;
		}
	}

	// 切换菜单状况
	public void toggle() {
		if (isOpen) {
			this.smoothScrollTo(mMenuWidth, 0);
			isOpen = false;
		} else {
			this.smoothScrollTo(0, 0);
			isOpen = true;
		}
	}
}

attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>

    <declare-styleable name="SlidingMenu">
        <attr name="rightPadding" format="dimension"></attr>
    </declare-styleable>

</resources>

构造main.xml
<com.example.slidingmenudemo.widget.SlidingMenu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:menu="http://schemas.android.com/apk/res/com.example.slidingmenudemo"
    android:id="@+id/menu"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:scrollbars="none"
    menu:rightPadding="150dp" >

    <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:background="@drawable/qq" >
        </LinearLayout>
    </LinearLayout>

</com.example.slidingmenudemo.widget.SlidingMenu>

要留意的几点:
1.代码的操作都是用的px,所以要把dp转成像素进行计算,这里谢了
2.HorizontalScrollView的smoothScrollTo(mMenuWidth, 0)办法跳到开端content构造中
3.重写onMeasure()中对menu和content构造的宽度都进行了从新的设定,这里menu距离右侧距离是一个自定义属性
创建自定义组件的步调:
一.写构造
二写java类,一般名字就是自定义组件的名字,在这里写对构造一一些器械操作
三.如不雅须要自定义属性,先在attr.xml中声明一个类似这种的属性参数,写清跋扈每个属性的名字和类型
<declare-styleable name="SlidingMenu">
        <attr name="rightPadding" format="dimension"></attr>
    </declare-styleable>
四.在自定义组件中获取构造文件中写的自定义属性
<pre name="code" class="java">		TypedArray typedArray = context.obtainStyledAttributes(attrs,
				R.styleable.SlidingMenu);
		int n = typedArray.getIndexCount();
		for (int i = 0; i < n; i++) {
			int attr = typedArray.getIndex(i);
			switch (attr) {
			case R.styleable.SlidingMenu_rightPadding:
				mMenuRightPadding = typedArray.getDimensionPixelOffset(attr,
						ScreenUtils.dp2px(context, mDefaultMenuRightPaddingDp));
				break;
			}
		}


这个代码很固定,记住吧,或者慢慢来。



四.构造中记得加上自定义属性的定名空间
xmlns:menu="http://schemas.android.com/apk/res/com.example.slidingmenudemo"

源代码根本上都在膳绫擎了,有过有须要项目代码的,请留下邮箱,我看到后会发以前。

相关案例查看更多