自定义View页面的滑动切换

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

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


<?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
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

plx_csdn

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值