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
- android 实现表格上下滑,左右滑,并且标题也跟着滑
- Android实现了复杂表格,表格内容区域可以与上下滑动、左右滚动,并且标题会跟着滚动
- Android 上滑显示底部导航,下滑显示标题bar
- Android 上滑显示底部导航,下滑显示标题bar
- Android实现ScrollView顶部布局上滑缩小,下滑恢复
- Android listView判断上滑还是下滑
- android listview判断上滑还是下滑
- android listview判断上滑还是下滑
- Android 监测是否能够上滑,下滑
- appium基础操作方法--滑动操作(上滑、下滑、左右滑动)--JAVA篇
- 小程序scroll-view上下滑的内部加左右滑,scroll-x生效的情况
- Android笔记(一):ViewDragHelper实现底部上滑同时底部下滑
- CoordinatorLayout、AppBarLayout实现上滑隐藏图片,下滑显示图片
- CoordinatorLayout、FloatingActionButton实现上滑隐藏FloatingActionButton,下滑显示FloatingActionButton
- axure简单实现移动端下滑和上滑功能
- Android ListView监听上滑、下滑、滑到顶部/底部
- Android ListView监听上滑或下滑动作
- Android WebView上滑隐藏头部,下滑显示头部
- 控制海康摄像头登录预览录像播放控制
- Docker-简介和安装
- 面向对象
- 图的度与握手定理
- WEB 容器、WEB服务和应用服务器的区别与联系
- android 实现表格上下滑,左右滑,并且标题也跟着滑
- 泛型
- lintcode/leetcode由易至难第21题:Encode and Decode TinyURL
- Spring Security4的配置,以及一些吐槽.(一)
- 自定义BaseAdapter适配器
- SAE:如何使用phpExcel
- 基本数据结构
- Java Web基础知识之Servlet容器初始化(无web.xml)
- 读取XML文件DOM4J方法