Chinaunix首页 | 论坛 | 博客
  • 博客访问: 257070
  • 博文数量: 49
  • 博客积分: 110
  • 博客等级: 民兵
  • 技术积分: 510
  • 用 户 组: 普通用户
  • 注册时间: 2013-01-13 00:59
个人简介

make it run,make it better,make it fast. https://github.com/liulanghaitun

文章分类

全部博文(49)

文章存档

2023年(1)

2022年(2)

2020年(4)

2019年(4)

2017年(15)

2016年(3)

2014年(3)

2013年(14)

分类: Android平台

2022-03-06 10:51:16


点击(此处)折叠或打开

  1. public class SideSwapLayout extends RecyclerView {

  2.     private Scroller mScroller;
  3.     private ViewGroup currentTouchView;
  4.     private ViewGroup lastTouchView;
  5.     private float mLastX;
  6.     private float mLastY;
  7.     private int mMenuWidth;
  8.     private boolean isExpanded = false;
  9.     private int mScaledTouchSlop;

  10.     public SideSwapLayout(Context context){
  11.         super(context);
  12.         init(context);
  13.     }

  14.     public SideSwapLayout(Context context, @Nullable AttributeSet attrs) {
  15.         super(context, attrs);
  16.         init(context);
  17.     }

  18.     private void init(Context context){
  19.         mScroller = new Scroller(context);
  20.         mScaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
  21.     }

  22.     @Override
  23.     public boolean onInterceptTouchEvent(MotionEvent event) {
  24.         float xPosition = event.getX();
  25.         float yPosition = event.getY();
  26.         switch (event.getAction()) {
  27.             case MotionEvent.ACTION_DOWN:
  28.                 if (!mScroller.isFinished()) {
  29.                     mScroller.abortAnimation();
  30.                 }
  31.                 currentTouchView = (ViewGroup) findChildViewUnder(xPosition, yPosition);
  32.                 if (currentTouchView.getChildCount() > 1) {
  33.                     View menuChild = currentTouchView.getChildAt(currentTouchView.getChildCount() - 1);
  34.                     mMenuWidth = menuChild.getWidth();
  35.                 } else {
  36.                     mMenuWidth = 0;
  37.                 }
  38.                 if (isExpanded) {
  39.                     mScroller.startScroll(lastTouchView.getScrollX(),0,-lastTouchView.getScrollX(),0,400);
  40.                     invalidate();
  41.                     isExpanded = false;
  42.                     return true;
  43.                 }
  44.                 break;
  45.         }
  46.         mLastX = xPosition;
  47.         mLastY = yPosition;
  48.         return super.onInterceptTouchEvent(event);
  49.     }

  50.     @Override
  51.     public boolean onTouchEvent(MotionEvent event) {
  52.         float x = event.getX();
  53.         float y = event.getY();
  54.         switch (event.getAction()){
  55.             case MotionEvent.ACTION_MOVE:
  56.                 float dx = mLastX-x;
  57.                 float dy = mLastY-y;
  58.                 if(Math.abs(dx) >= Math.abs(dy) && Math.abs(dx) > mScaledTouchSlop){
  59.                     float realScrollWidth = currentTouchView.getScrollX() + dx;
  60.                     if (realScrollWidth <= 0){
  61.                         currentTouchView.scrollTo(0,0);
  62.                         isExpanded = false;
  63.                         return true;
  64.                     }
  65.                     if (realScrollWidth >= mMenuWidth){
  66.                         currentTouchView.scrollTo(mMenuWidth,0);
  67.                         isExpanded = true;
  68.                         return true;
  69.                     }
  70.                     currentTouchView.scrollBy((int)dx,0);
  71.                 }
  72.                 break;
  73.             case MotionEvent.ACTION_CANCEL:
  74.             case MotionEvent.ACTION_UP:
  75.                 if (!mScroller.isFinished()){
  76.                     mScroller.abortAnimation();
  77.                     lastTouchView.scrollTo(mScroller.getFinalX(),0);
  78.                 }
  79.                 float deltaX;
  80.                 int scrollX = currentTouchView.getScrollX();
  81.                 if(scrollX >= mMenuWidth/2){
  82.                     deltaX = mMenuWidth - scrollX;
  83.                     isExpanded = true;
  84.                 }else {
  85.                     deltaX = -scrollX;
  86.                     isExpanded = false;
  87.                 }
  88.                 lastTouchView = currentTouchView;
  89.                 mScroller.startScroll(scrollX,0, (int) deltaX,0);
  90.                 invalidate();
  91.                 break;
  92.         }
  93.         mLastX = x;
  94.         mLastY = y;
  95.         return super.onTouchEvent(event);
  96.     }

  97.     @Override
  98.     public void computeScroll() {
  99.         if (mScroller.computeScrollOffset()) {
  100.             lastTouchView.scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
  101.             postInvalidate();
  102.         }
  103.     }
  104. }

    点击(此处)折叠或打开

    1. 源码链接:

阅读(876) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~