SDview-侧滑菜单实现,代码中有详细说明


示例代码  在我的资源中有  文件名为m

示例代码

/**

 * 2013-9-15 下午6:16:58 Created By niexiaoqiang
 */

package com.xiaoqiang.m;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;

/**
 * 侧滑菜单 1.使用getLinearLayout_left() ,getLinearLayout_right()
 * 得到左侧栏和右侧栏的布局均为linnerlayout,再添加自己需要的控件
 * 2.可以对菜单滑出还是关闭进行监听,使用对起设置监听器。setSDViewListener(SDViewListener sViewListener)
 * 3.要修改侧栏的宽度请使用私有变量leftwidget,对其设置比例,左侧栏目的狂赌为,屏幕狂赌/leftwidget

 * 4.滑动数据由私有变量speed控制,可自行修改,数值越大越慢

 * 5.setLeftVisible可设置菜单隐藏还是现实

 */
public class SDView extends LinearLayout implements OnTouchListener {

    /**
     * 侧栏的权重
     */
    private int leftwidget = 3;
    /**
     * 侧栏的宽度,通过init初始化
     */
    private int left_width = 0;

    private LinearLayout linearLayout_content = null;
    private LinearLayout linearLayout_left = null;
    private LinearLayout linearLayout_right = null;

    private void init(Context context) {
        int screen_width = getCurrentScreenWidth_Height((Activity) context)[0];
        left_width = screen_width / leftwidget;
        linearLayout_content = getLinearLayout(context, getLinLoutParams(screen_width + left_width));
        linearLayout_content.setOrientation(LinearLayout.HORIZONTAL);
        linearLayout_left = getLinearLayout(context, getLinLoutParams(left_width));
        linearLayout_right = getLinearLayout(context, getLinLoutParams(screen_width));
        linearLayout_content.addView(linearLayout_left);
        linearLayout_content.addView(linearLayout_right);
        /****************/
        this.addView(linearLayout_content);
        this.setOnTouchListener(this);
    }

    public LinearLayout getLinearLayout_left() {
        return linearLayout_left;
    }

    public LinearLayout getLinearLayout_right() {
        return linearLayout_right;
    }

    /**
     * 获取LinearLayout实例,默认为垂直方向
     *
     * @param context
     * @return
     */
    private LinearLayout getLinearLayout(Context context, LinearLayout.LayoutParams params) {
        LinearLayout linearLayout = new LinearLayout(context);
        linearLayout.setOrientation(LinearLayout.VERTICAL);
        linearLayout.setLayoutParams(params);
        return linearLayout;
    }

    /**
     * 获取LinearLayout.LayoutParams参数
     *
     * @param width
     *            LayoutParams.FILL_PARENT或者数值
     * @return
     */
    private LinearLayout.LayoutParams getLinLoutParams(int width) {
        LinearLayout.LayoutParams liLayoutParams = new LinearLayout.LayoutParams(width, LayoutParams.MATCH_PARENT);
        return liLayoutParams;
    }

    /**
     * 获取当前屏幕的高宽
     *
     * @param activity
     * @return
     */
    private int[] getCurrentScreenWidth_Height(Activity activity) {
        DisplayMetrics metric = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metric);
        return new int[] { metric.widthPixels, metric.heightPixels };
    }

    /************** 滚动的数据定义 *************/
    private boolean isshow_left = true;
    private int touch_distance = 0;
    private boolean isscrolling = false;
    private SDViewListener sViewListener = null;
    private int downx = 0;
    private int movex = 0;
    private int speed = 3;

    /************** 滚动的数据定义 *************/
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                System.out.println("侧栏,按下");
                downx = (int) event.getX();
                touch_distance = 0;
                isscrolling = true;
                return true;
            case MotionEvent.ACTION_MOVE:
                movex = (int) event.getX();
                // Log.d("movex_", " " + movex);
                if (downx == 0 || isscrolling == false) {
                    downx = movex;
                    touch_distance = 0;
                    isscrolling = true;
                }
                int movelength = (movex - downx) / speed;
                downx = movex;
                touch_distance += movelength;
                move(touch_distance);
                return true;
            case MotionEvent.ACTION_UP:
                // 侧栏显示了为0,没有显示为left_width
                if (isshow_left) {
                    if (touch_distance > 0) {
                        this.setLeftVisible(true);
                        return true;
                    }
                    if (Math.abs(touch_distance) > (left_width / 2)) {
                        this.setLeftVisible(false);
                    } else {
                        this.setLeftVisible(true);
                    }
                } else {
                    if (touch_distance < 0) {
                        this.setLeftVisible(false);
                        return true;
                    }
                    if (Math.abs(touch_distance) > (left_width / 2)) {
                        this.setLeftVisible(true);
                    } else {
                        this.setLeftVisible(false);
                    }
                }
                return true;
            default:
                return true;
        }

    }

    /*
     * (non-Javadoc)
     * @see
     * android.view.ViewGroup#onInterceptTouchEvent(android.view.MotionEvent)
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        // 不中断SDVIEW的ontouch事件,并且本类也执行该事件
        this.onTouch(this, event);
        return false;
    }

    /**
     * 设置侧栏显示还是隐藏
     *
     * @param isShow
     */
    public void setLeftVisible(boolean isShow) {
        touch_distance = 0;
        isscrolling = false;
        if (isShow) {
            isshow_left = true;
            this.scrollTo(0, 0);
            if (null != sViewListener) {
                sViewListener.onLeftchange(left_width);
            }
        } else {
            isshow_left = false;
            this.scrollTo(left_width, 0);
            if (null != sViewListener) {
                sViewListener.onLeftchange(0);
            }
        }
    }

    /**
     * 移动
     *
     * @param distance
     */
    private void move(int touch_distance) {
        // 侧栏显示了为0,没有显示为left_width
        int movetox = 0;
        if (isshow_left) {
            if (touch_distance > 0) {
                return;
            }
            movetox = 0 + Math.abs(touch_distance);
        } else {
            if (touch_distance < 0) {
                return;
            }
            movetox = left_width - Math.abs(touch_distance);
        }
        if (movetox >= left_width || movetox <= 0) {
            return;
        }
        this.scrollTo(movetox, 0);
    }

    /**
     * SDView监听器
     *
     * @author niexiaoq
     *
     */
    public interface SDViewListener {
        public void onLeftchange(int leftmargin);
    }

    /**
     * 设置监听器
     *
     * @param viewListener
     */
    public void setSDViewListener(SDViewListener sViewListener) {
        this.sViewListener = sViewListener;
    }

    /************** 构造函数 ****************/
    public SDView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.init(context);
    }

    public SDView(Context context) {
        super(context);
        this.init(context);
    }

    public SDView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        this.init(context);
    }
    /************** 构造函数 ****************/
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值