android 实现表格上下滑,左右滑,并且标题也跟着滑

来源:互联网 发布:seo团队 编辑:程序博客网 时间:2024/05/16 12:46

android 实现表格上下滑,左右滑,并且标题也跟着滑

效果图

这里写图片描述

思路

图片

这里写图片描述
滑动方式主要是两个RecyclerView联动和HorizontalScrollView随RecyclerView滑动。
关键是把滑动事件交给SingleOrientationView去处理。在SingleOrientationView的dispatchTouchEvent方法中,将手指滑动的路径模拟成单一方向(水平或竖直)的滑动事件,将此事件交给RecyclerView去处理。

SingleOrientationView的代码如下:

public class SingleOrientationView extends FrameLayout {    private int mScaledTouchSlop;    private float startX;    private float startY;    private float currentX;    private float currentY;    /**     * 0: 默认   1:水平    2:竖直     * */    private int type = 0;    private View[] mViews;    public SingleOrientationView(Context context) {        super(context);        initTouchSlop(context);    }    public SingleOrientationView(Context context, AttributeSet attributeSet) {        super(context, attributeSet);        initTouchSlop(context);    }    public SingleOrientationView(Context context, AttributeSet attributeSet, int defStyleAttr) {        super(context, attributeSet, defStyleAttr);        initTouchSlop(context);    }    @RequiresApi(api = 21)    public SingleOrientationView(Context context, AttributeSet attributeSet, int defStyleAttr, int defStyleRes) {        super(context, attributeSet, defStyleAttr, defStyleRes);        initTouchSlop(context);    }    private void initTouchSlop(Context context) {        this.mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();    }    private MotionEvent getMotionEvent(MotionEvent motionEvent) {        if (type == 1)            return MotionEvent.obtain(motionEvent.getDownTime(),                    motionEvent.getEventTime(), motionEvent.getAction(), motionEvent.getX(), currentY, motionEvent.getMetaState());        else            return MotionEvent.obtain(motionEvent.getDownTime(),                    motionEvent.getEventTime(), motionEvent.getAction(), currentX, motionEvent.getY(), motionEvent.getMetaState());    }    public boolean dispatchTouchEvent(MotionEvent motionEvent) {        MotionEvent targetMotionEvent = null;        switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {            case MotionEvent.ACTION_DOWN:                startX = motionEvent.getX();                startY = motionEvent.getY();                targetMotionEvent = motionEvent;                break;            case MotionEvent.ACTION_MOVE:                if(type == 0){                    currentX = motionEvent.getX();                    currentY = motionEvent.getY();                    float f1 = currentX - startX;                    float f2 = currentY - startY;                    if(Math.abs(f1) > mScaledTouchSlop || Math.abs(f2) > mScaledTouchSlop){                        if (Math.abs(f1) > Math.abs(f2)){                            type = 1;                            targetMotionEvent = getMotionEvent(motionEvent);                        } else {                            type = 2;                            targetMotionEvent = getMotionEvent(motionEvent);                        }                    }else {                        targetMotionEvent = motionEvent;                    }                }else if(type == 1){                    targetMotionEvent = getMotionEvent(motionEvent);                }else {                    targetMotionEvent = getMotionEvent(motionEvent);                }                break;            case MotionEvent.ACTION_UP:                type = 0;                targetMotionEvent = motionEvent;                break;            case MotionEvent.ACTION_CANCEL:                type = 0;                targetMotionEvent = motionEvent;                break;            default:        }        for(int i = 0; i < mViews.length; i++){            mViews[i].dispatchTouchEvent(targetMotionEvent);        }        return true;    }    public void SyncViews(View[] views) {        this.mViews = views;    }}

下载地址:

链接: https://pan.baidu.com/s/1pL6tp83 密码: dmx4

阅读全文
0 0
原创粉丝点击