安卓_侧滑菜单

来源:互联网 发布:软考中级数据库工程师 编辑:程序博客网 时间:2024/06/05 07:35

这两天每天加班到十点钟,回来都累成狗了,也变懒了,好在深圳的温度还不算冷,昨晚的那个侧滑菜单讲解,由于没有给出案例,今天惦记了一整天,今晚就把简单的实现写出来,

 不说什么了,上一章都讲完了。这里就只讲实现和细节优化有功能,今晚先把基本实现贴出来,周末来优化,麻蛋,下周就放假happy了,明天还要上班.好了,看效果图把


布局文件代码

<?xml version="1.0" encoding="utf-8"?><com.reeman.demo.DragViewGroup xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent">    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@color/colorAccent"        android:orientation="vertical">        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="asdfjhbasdf" />    </LinearLayout>    <RelativeLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:background="@color/colorPrimaryDark">        <TextView            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:text="asdfjhbasdf" />    </RelativeLayout></com.reeman.demo.DragViewGroup>


View代码

package com.reeman.demo;import android.content.Context;import android.support.v4.view.ViewCompat;import android.support.v4.widget.ViewDragHelper;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.widget.FrameLayout;/** * Created by Administrator on 2017/1/13. */public class DragViewGroup extends FrameLayout {    ViewDragHelper mViewDragHelper;    View mMenuView, mMainView;    int mWidth;    public DragViewGroup(Context context) {        this(context, null);    }    public DragViewGroup(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public DragViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initView();    }    @Override    protected void onFinishInflate() {        super.onFinishInflate();        mMenuView = getChildAt(0);        mMainView = getChildAt(1);    }    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        super.onSizeChanged(w, h, oldw, oldh);        mWidth = mMenuView.getMeasuredWidth();    }    @Override    public boolean onInterceptTouchEvent(MotionEvent ev) {        return mViewDragHelper.shouldInterceptTouchEvent(ev);    }    @Override    public boolean onTouchEvent(MotionEvent event) {        mViewDragHelper.processTouchEvent(event);        return true;    }    private void initView() {        mViewDragHelper = ViewDragHelper.create(this, callBack);    }    private ViewDragHelper.Callback callBack = new ViewDragHelper.Callback() {        public boolean tryCaptureView(View child, int pointerId) {            return mMainView == child;        }        //处理水平滑动的事件        @Override        public int clampViewPositionHorizontal(View child, int left, int dx) {            return left;        }        //处理垂直滑动的事件        @Override        public int clampViewPositionVertical(View child, int top, int dy) {            return 0;        }        //拖动结束后调用        @Override        public void onViewReleased(View releasedChild, float xvel, float yvel) {            super.onViewReleased(releasedChild, xvel, yvel);            if (mMainView.getLeft() < 500) {                //关闭菜单                mViewDragHelper.smoothSlideViewTo(mMainView, 0, 0);                ViewCompat.postInvalidateOnAnimation(DragViewGroup.this);            } else {                //打开菜单                mViewDragHelper.smoothSlideViewTo(mMainView, 300, 0);                ViewCompat.postInvalidateOnAnimation(DragViewGroup.this);            }        }    };    @Override    public void computeScroll() {        if (mViewDragHelper.continueSettling(true)) {            ViewCompat.postInvalidateOnAnimation(this);        }    }}
由于没有处理左滑的事件,界面有些奇怪,代码里面注释讲解上一章有记录,代码比较简单,看看就能懂,

















0 0