侧滑菜单学习

参考资料:

①:http://blog.csdn.net/lmj623565791/article/details/39185641 博客讲解

②:http://www.imooc.com/learn/211  视频讲解

一、普通侧滑

使用HorizontalScrollView来实现,自定义控件

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
在该方法里设定子View的宽和高,自己的宽和高

@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
设定显示位置


代码:

package com.example.qq_sliding_menu;

import com.nineoldandroids.view.ViewHelper;

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

public class SldingMenu extends HorizontalScrollView {
	
	/**
	 * 屏幕宽度
	 */
	private int screenWidth;
	
	private ViewGroup menuVG;
	
	private int menuWidth;
	
	private int rightPadding = 50; //dp

	private boolean once;
	
	private static final String TAG = "SldingMenu";
	
	public SldingMenu(Context context, AttributeSet attrs) {

		this(context, attrs, 0);
		Log.e(TAG, "public SldingMenu(Context context, AttributeSet attrs);");
	/*	
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics );
		
		screenWidth = outMetrics.widthPixels;
		rightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightPadding, context.getResources().getDisplayMetrics());
		*/
	}

	
	/**
	 * 当使用自定义的属性时,会调用该构造函数
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public SldingMenu(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		Log.e(TAG, "public SldingMenu(Context context, AttributeSet attrs, int defStyle);");
		
		WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
		DisplayMetrics outMetrics = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(outMetrics );
		
		screenWidth = outMetrics.widthPixels;
		rightPadding = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, rightPadding, context.getResources().getDisplayMetrics());
		
		TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlidingMenu, defStyle, 0);
		rightPadding = (int) ta.getDimension(R.styleable.SlidingMenu_rightPadding, rightPadding);
		Log.e(TAG, "rightPadding-->"+rightPadding);
		ta.recycle();
	}



	public SldingMenu(Context context) {
		this(context, null );
		Log.e(TAG, "public SldingMenu(Context context);");
	}



	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		
		if (!once ) {
			LinearLayout wrapper = (LinearLayout) getChildAt(0);
			menuVG = (ViewGroup) wrapper.getChildAt(0);
			ViewGroup contextVG = (ViewGroup) wrapper.getChildAt(1);
			
			menuWidth = screenWidth - rightPadding;
			menuVG.getLayoutParams().width = menuWidth;
			contextVG.getLayoutParams().width = screenWidth;					
		}
		
	}
	
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		// TODO Auto-generated method stub
		super.onLayout(changed, l, t, r, b);
		
		if (changed ) {
			this.scrollTo(menuWidth, 0);
			once = true;
		}
		
	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent ev) {

		Log.i(TAG, "onTouchEvent");
		
		int action = ev.getAction();
		switch (action ) {
		case MotionEvent.ACTION_UP:
			Log.e(TAG, "action_up");
			int scrollX = this.getScrollX();
			
			Log.e(TAG, "scrollX-->"+scrollX);
			Log.e(TAG, "geteWidth-->"+this.getWidth());
			Log.e(TAG, "getMeaureWidth-->"+this.getMeasuredWidth() );
			
			if (scrollX >= menuWidth/2 ) {
				Log.e(TAG, "show context");
				this.smoothScrollTo(menuWidth, 0);
			} else {
				Log.e(TAG, "show menu");
				this.smoothScrollTo(0, 0);
			}
			return true;
		default:
			break;
		}
		
		return super.onTouchEvent(ev);
	}
	
	
}


二、自定义属性

res/values/attr.xml

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

获取自定义的值:

TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SlidingMenu, defStyle, 0);
rightPadding = (int) ta.getDimension(R.styleable.SlidingMenu_rightPadding, rightPadding);

引入命名空间

 xmlns:slidemenu="http://schemas.android.com/apk/res/com.example.qq_sliding_menu"
xmlns:slidemenu="http://schemas.android.com/apk/res/packageName"


三、抽屉式菜单

	
	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
		
		//Log.e(TAG, "l-->"+l);
		
		ViewHelper.setTranslationX(menuVG, l);
		
	}

四、动画效果

ViewHelper的使用



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值