自定义View页面的滑动切换

来源:互联网 发布:python for 无限循环 编辑:程序博客网 时间:2024/06/06 17:47

本文为博主原创--未经博主允许不得转载--谢谢合作!

效果图(左中右共三个个页面,左右页面默认隐藏,随手指滑动出来....)


<?xml version="1.0" encoding="utf-8"?><com.nxnresearch02.jieyan.SlidGroup    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"    tools:context="com.nxnresearch02.jieyan.MainActivity"    tools:showIn="@layout/activity_main">    <include layout="@layout/left_layout"/>    <include layout="@layout/right"/>    <include layout="@layout/main_layout"/></com.nxnresearch02.jieyan.SlidGroup>


代码如下:

public class SlidGroup extends ViewGroup {    private View leftView;    private View mainView;    private View rightView;    private int mLeftWidth;    private int mMainWidth;    private int mMainHeight;    public SlidGroup(Context context, AttributeSet attrs) {        super(context, attrs);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        leftView = getChildAt(0);        rightView = getChildAt(1);        mainView = getChildAt(2);        measureChildren(widthMeasureSpec,heightMeasureSpec);        mLeftWidth = leftView.getMeasuredWidth();        mMainWidth = MeasureSpec.getSize(widthMeasureSpec);        mMainHeight = MeasureSpec.getSize(heightMeasureSpec);    }    @Override    protected void onLayout(boolean changed, int l, int t, int r, int b) {        leftView.layout(-mLeftWidth,0,0,mMainHeight);        rightView.layout(mMainWidth,0,mMainWidth+mLeftWidth,mMainHeight);        mainView.layout(0,0,mMainWidth,mMainHeight);    }    float startX = 0;    float startY = 0;    float endX = 0;    float endY = 0;    float moveX = 0;    @Override    public boolean onTouchEvent(MotionEvent event) {        switch (event.getAction()) {            case  MotionEvent.ACTION_DOWN:                startX= event.getX();                startY= event.getY();                break;            case  MotionEvent.ACTION_UP:                int nowX = getScrollX();                Log.v("hhh","getScrollX()"+getScrollX());                if(nowX < -mLeftWidth/2) {                    scrollTo(-mLeftWidth,0);                }else if(nowX > -mLeftWidth/2 && nowX <0) {                    scrollTo(0,0);                }else if(nowX >0 && nowX < mLeftWidth/2) {                    scrollTo(0,0);                }else {                    scrollTo(mLeftWidth,0);                }                break;            case  MotionEvent.ACTION_MOVE:                endX =  event.getX();                endY = event.getY();                moveX = endX-startX;                int outX = (int) (getScrollX()+(-moveX));                if(outX < -mLeftWidth) {                    scrollTo(-mLeftWidth,0);                }else if(outX > mLeftWidth) {                    scrollTo(mLeftWidth,0);                }else {                    scrollBy(-(int) moveX,0);                    startX = endX;                }                break;        }        return true;    }}

scrollto()---只能滑动一次,scrollBy()--在原有的基础上继续滑动.

0 0