android 实现表格上下滑,左右滑,并且标题也跟着滑
2017-07-03 08:42
423 查看
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 MotionEven 4000 t.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
相关文章推荐
- Android实现了复杂表格,表格内容区域可以与上下滑动、左右滚动,并且标题会跟着滚动
- Android实现Excel表格,且表格能左右、上下滑动
- Android实现Excel表格,且表格能左右、上下滑动
- HorizontalScrollView里嵌套ListView实现上下左右滑动表格
- 布局动画Android ScrollView HorizontalScrollView 实现全方向(上下左右)反弹效果
- android中使用Activity实现监听手指上下左右滑动
- Android 自定义View 实现手势监听,左右滑动,上下滑动
- Android ObjectAnimator 无限循环播放,实现上下左右浮动效果
- Android ScrollViewy与HorizontalScrollView实现上下左右滚动
- android之实现上下左右翻页效果
- Android 自定义RadioButton 实现文字上下左右方向的图片大小设置
- unity3d如何实现角色站到台阶跟着上下左右
- android安卓 Fragment实现页面标题左右同步切换以及点击标题切换页面
- Android上实现各种风格的隐藏菜单,比如左右滑动菜单、上下滑动显示隐藏菜单
- android 动画(Animation)闪烁、左右摇摆,上下晃动等效果实现
- android scrollview 实现上下左右滚动方法
- Android ScrollView HorizontalScrollView 实现全方向(上下左右)反弹效果
- 随窗口大小改变可以上下左右滚动的表格(表格标题固定,IE6)
- android 实现左右上下震动的效果
- Android实现图片自动轮播并且支持手势左右无限滑动