Chinaunix首页 | 论坛 | 博客
  • 博客访问: 54211
  • 博文数量: 48
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 260
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-12 11:48
文章分类
文章存档

2016年(48)

我的朋友

分类: Java

2016-11-10 16:36:55

完美解决左侧抽屉菜单和viewpager不能兼容左右滑动的问题,可进行参考。

WeMall-Client/res/layout/wemall_main_ui.xml


 

 

点击(此处)折叠或打开

  1. import android.view.KeyEvent;
  2. import android.view.Menu;
  3. import android.view.MenuItem;
  4. import android.view.MotionEvent;
  5. import android.view.View;
  6. import android.view.View.OnClickListener;
  7. import android.view.View.OnTouchListener;
  8. import android.view.ViewGroup;
  9. import android.view.Window;
  10. import android.view.inputmethod.InputMethodManager;
  11. @@ -105,6 +107,8 @@ public class MainUIMain extends FragmentActivity implements
  12. menudata = new ArrayList<HashMap<String, Object>>();
  13. menuadapter = new MenuAdapter(this, menudata);
  14. menulistview.setAdapter(menuadapter);
  15.  
  16. // 初始化ViewPager,菜单数据
  17. InitViewPager();
  18. @@ -206,8 +210,9 @@ public class MainUIMain extends FragmentActivity implements
  19. public void onPageScrollStateChanged(int arg0) {
  20. // 在这里判断vpage是否滑到了商品页面,如果滑到了商品页面并且继续向右拉动屏幕,则展现菜单列表//
  21. if (this.viewPager.getCurrentItem() == 0 && arg0 == 1) {
  22. System.out.println("滑到了最左边且在基于往右侧滑动");
  23.  //slideMenu.openMenu();

  24. } else {

  25. }
  26. }

 WeMall-Client/src/cn/edu/zzu/wemall/ui/SlideMenu.java

点击(此处)折叠或打开

  1. package cn.edu.zzu.wemall.ui;

  2. import cn.edu.zzu.wemall.R;
  3. import android.annotation.SuppressLint;
  4. import android.content.Context;
  5. import android.graphics.Canvas;
  6. import android.support.v4.view.ViewPager;
  7. import android.util.AttributeSet;
  8. import android.view.MotionEvent;
  9. import android.view.VelocityTracker;
  10. import android.view.View;
  11. import android.view.ViewConfiguration;
  12. import android.view.ViewGroup;
  13. import android.widget.Scroller;

  14. /*
  15.  * 侧边栏类
  16.  *
  17.  */
  18. public class SlideMenu extends ViewGroup {
  19.     public static final int SCREEN_MENU = 0;
  20.     public static final int SCREEN_MAIN = 1;
  21.     private static final int SCREEN_INVALID = -1;

  22.     private int mCurrentScreen;
  23.     private int mNextScreen = SCREEN_INVALID;

  24.     private Scroller mScroller;
  25.     private VelocityTracker mVelocityTracker;
  26.     private int mTouchSlop;

  27.     private float mLastMotionX;
  28.     private float mLastMotionY;

  29.     private final static int TOUCH_STATE_REST = 0;
  30.     private final static int TOUCH_STATE_SCROLLING = 1;
  31.     private static final int SNAP_VELOCITY = 1000;

  32.     public int mTouchState = TOUCH_STATE_REST;
  33.     private boolean mLocked;
  34.     private boolean mAllowLongPress;

  35.     public SlideMenu(Context context) {
  36.         this(context, null, 0);
  37.     }

  38.     public SlideMenu(Context context, AttributeSet attrs) {
  39.         this(context, attrs, 0);
  40.     }


  41.     public SlideMenu(Context context, AttributeSet attrs, int defStyle) {
  42.         super(context, attrs, defStyle);

  43.         mScroller = new Scroller(getContext());
  44.         mCurrentScreen = SCREEN_MAIN;
  45.         mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
  46.     }

  47.     @Override
  48.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  49.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  50.         measureViews(widthMeasureSpec, heightMeasureSpec);
  51.     }

  52.     public void measureViews(int widthMeasureSpec, int heightMeasureSpec) {
  53.         View menuView = getChildAt(0);
  54.         menuView.measure(menuView.getLayoutParams().width + menuView.getLeft()
  55.                 + menuView.getRight(), heightMeasureSpec);

  56.         View contentView = getChildAt(1);
  57.         contentView.measure(widthMeasureSpec, heightMeasureSpec);
  58.     }

  59.     @Override
  60.     protected void onLayout(boolean changed, int l, int t, int r, int b) {
  61.         int childCount = getChildCount();
  62.         if (childCount != 2) {
  63.             throw new IllegalStateException(
  64.                     "The childCount of SlidingMenu must be 2");
  65.         }

  66.         View menuView = getChildAt(0);
  67.         final int width = menuView.getMeasuredWidth();
  68.         menuView.layout(-width, 0, 0, menuView.getMeasuredHeight());

  69.         View contentView = getChildAt(1);
  70.         contentView.layout(0, 0, contentView.getMeasuredWidth(),
  71.                 contentView.getMeasuredHeight());
  72.     }

  73.     @Override
  74.     protected void onFinishInflate() {
  75.         super.onFinishInflate();
  76.         View child;
  77.         for (int i = 0; i < getChildCount(); i++) {
  78.             child = getChildAt(i);
  79.             child.setFocusable(true);
  80.             child.setClickable(true);
  81.         }
  82.     }

  83.     @Override
  84.     public boolean onInterceptTouchEvent(MotionEvent ev) {
  85.         if (mLocked) {
  86.             return true;
  87.         }

  88.         final int action = ev.getAction();
  89.         if ((action == MotionEvent.ACTION_MOVE)
  90.                 && (mTouchState != TOUCH_STATE_REST)) {
  91.             return true;
  92.         }

  93.         final float x = ev.getX();
  94.         final float y = ev.getY();

  95.         switch (action) {
  96.         case MotionEvent.ACTION_MOVE:
  97.             //在这里做点文章,侧滑菜单和viewpager的滑动兼容,头疼。。。
  98.             /**
  99.              * 设定条件,如果当前页面在商品页面,往右侧拉动屏幕,显示菜单,往左则收回菜单,再往左拉
  100.              * 则切换到购物车
  101.              * 如果当前页面在购物车或者个人中心,则无论左右拉,都不理会菜单栏!,直接切换viewpager
  102.              */
  103.             ViewPager viewPager = (ViewPager) findViewById(R.id.vPager);
  104.             if(viewPager.getCurrentItem()>0){ //如果当前页面不在商品页面,则忽略滑动出现菜单
  105.                 break;
  106.             }

  107.             final int xDiff = (int) Math.abs(x - mLastMotionX);
  108.             final int yDiff = (int) Math.abs(y - mLastMotionY);
  109.             //菜单没显示且往右滑动时break
  110.             if(isMainScreenShowing()&&(x - mLastMotionX)<0){
  111.                 break;
  112.             }
  113.             final int touchSlop = mTouchSlop;
  114.             boolean xMoved = xDiff > touchSlop;
  115.             boolean yMoved = yDiff > touchSlop;

  116.             if (xMoved || yMoved) {

  117.                 if (xMoved) {
  118.                     // Scroll if the user moved far enough along the X axis
  119.                     mTouchState = TOUCH_STATE_SCROLLING;
  120.                     enableChildrenCache();
  121.                 }
  122.                 // Either way, cancel any pending longpress
  123.                 if (mAllowLongPress) {
  124.                     mAllowLongPress = false;
  125.                     // Try canceling the long press. It could also have been
  126.                     // scheduled
  127.                     // by a distant descendant, so use the mAllowLongPress flag
  128.                     // to block
  129.                     // everything
  130.                     final View currentScreen = getChildAt(mCurrentScreen);
  131.                     currentScreen.cancelLongPress();
  132.                 }
  133.             }
  134.             break;

  135.         case MotionEvent.ACTION_DOWN:
  136.             
  137.             // Remember location of down touch
  138.             mLastMotionX = x;
  139.             mLastMotionY = y;
  140.             mAllowLongPress = true;
  141.             mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST
  142.                     : TOUCH_STATE_SCROLLING;
  143.             break;

  144.         case MotionEvent.ACTION_CANCEL:
  145.         case MotionEvent.ACTION_UP:
  146.             // Release the drag
  147.             clearChildrenCache();
  148.             mTouchState = TOUCH_STATE_REST;
  149.             mAllowLongPress = false;
  150.             break;
  151.         }

  152.         /*
  153.          * The only time we want to intercept motion events is if we are in the
  154.          * drag mode.
  155.          */
  156.         return mTouchState != TOUCH_STATE_REST;
  157.     }

  158.     void enableChildrenCache() {
  159.         final int count = getChildCount();
  160.         for (int i = 0; i < count; i++) {
  161.             final View layout = (View) getChildAt(i);
  162.             layout.setDrawingCacheEnabled(true);
  163.         }
  164.     }

  165.     void clearChildrenCache() {
  166.         final int count = getChildCount();
  167.         for (int i = 0; i < count; i++) {
  168.             final View layout = (View) getChildAt(i);
  169.             layout.setDrawingCacheEnabled(false);
  170.         }
  171.     }

  172.     @SuppressLint("ClickableViewAccessibility")
  173.     @Override
  174.     public boolean onTouchEvent(MotionEvent ev) {
  175.         if (mLocked) {
  176.             return true;
  177.         }

  178.         if (mVelocityTracker == null) {
  179.             mVelocityTracker = VelocityTracker.obtain();
  180.         }
  181.         mVelocityTracker.addMovement(ev);

  182.         final int action = ev.getAction();
  183.         final float x = ev.getX();
  184.         switch (action) {
  185.         case MotionEvent.ACTION_DOWN:
  186.             /*
  187.              * If being flinged and user touches, stop the fling. isFinished
  188.              * will be false if being flinged.
  189.              */
  190.             if (!mScroller.isFinished()) {
  191.                 mScroller.abortAnimation();
  192.             }

  193.             // Remember where the motion event started
  194.             mLastMotionX = x;
  195.             break;
  196.         case MotionEvent.ACTION_MOVE:
  197.             if (mTouchState == TOUCH_STATE_SCROLLING) {
  198.                 // Scroll to follow the motion event
  199.                 final int deltaX = (int) (mLastMotionX - x);
  200.                 mLastMotionX = x;
  201.                 if (deltaX < 0) {
  202.                     if (deltaX + getScrollX() >= -getChildAt(0).getWidth()) {
  203.                         scrollBy(deltaX, 0);
  204.                     }

  205.                 } else if (deltaX > 0) {
  206.                     
  207.                     final int availableToScroll = getChildAt(
  208.                             getChildCount() - 1).getRight()
  209.                             - getScrollX() - getWidth();

  210.                     if (availableToScroll > 0) {
  211.                         scrollBy(Math.min(availableToScroll, deltaX), 0);
  212.                     }
  213.                 }
  214.             }
  215.             break;
  216.         case MotionEvent.ACTION_UP:
  217.             if (mTouchState == TOUCH_STATE_SCROLLING) {
  218.                 final VelocityTracker velocityTracker = mVelocityTracker;
  219.                 velocityTracker.computeCurrentVelocity(1000);
  220.                 int velocityX = (int) velocityTracker.getXVelocity();

  221.                 if (velocityX > SNAP_VELOCITY && mCurrentScreen == SCREEN_MAIN) {
  222.                     // Fling hard enough to move left
  223.                     snapToScreen(SCREEN_MENU);
  224.                 } else if (velocityX < -SNAP_VELOCITY
  225.                         && mCurrentScreen == SCREEN_MENU) {
  226.                     // Fling hard enough to move right
  227.                     snapToScreen(SCREEN_MAIN);
  228.                 } else {
  229.                     snapToDestination();
  230.                 }

  231.                 if (mVelocityTracker != null) {
  232.                     mVelocityTracker.recycle();
  233.                     mVelocityTracker = null;
  234.                 }
  235.             }
  236.             mTouchState = TOUCH_STATE_REST;
  237.             break;
  238.         case MotionEvent.ACTION_CANCEL:
  239.             mTouchState = TOUCH_STATE_REST;
  240.         }

  241.         return true;
  242.     }

  243.     @Override
  244.     public void computeScroll() {
  245.         if (mScroller.computeScrollOffset()) {
  246.             scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
  247.         } else if (mNextScreen != SCREEN_INVALID) {
  248.             mCurrentScreen = Math.max(0,
  249.                     Math.min(mNextScreen, getChildCount() - 1));
  250.             mNextScreen = SCREEN_INVALID;
  251.             clearChildrenCache();
  252.         }
  253.     }

  254.     @Override
  255.     public void scrollTo(int x, int y) {
  256.         super.scrollTo(x, y);
  257.         postInvalidate();
  258.     }

  259.     @Override
  260.     protected void dispatchDraw(Canvas canvas) {
  261.         final int scrollX = getScrollX();
  262.         super.dispatchDraw(canvas);
  263.         canvas.translate(scrollX, 0);
  264.     }

  265.     @Override
  266.     public boolean dispatchUnhandledMove(View focused, int direction) {
  267.         if (direction == View.FOCUS_LEFT) {
  268.             if (getCurrentScreen() > 0) {
  269.                 snapToScreen(getCurrentScreen() - 1);
  270.                 return true;
  271.             }
  272.         } else if (direction == View.FOCUS_RIGHT) {
  273.             if (getCurrentScreen() < getChildCount() - 1) {
  274.                 snapToScreen(getCurrentScreen() + 1);
  275.                 return true;
  276.             }
  277.         }
  278.         return super.dispatchUnhandledMove(focused, direction);
  279.     }

  280.     protected void snapToScreen(int whichScreen) {

  281.         enableChildrenCache();

  282.         whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
  283.         boolean changingScreens = whichScreen != mCurrentScreen;

  284.         mNextScreen = whichScreen;

  285.         View focusedChild = getFocusedChild();
  286.         if (focusedChild != null && changingScreens
  287.                 && focusedChild == getChildAt(mCurrentScreen)) {
  288.             focusedChild.clearFocus();
  289.         }

  290.         final int newX = (whichScreen - 1) * getChildAt(0).getWidth();
  291.         final int delta = newX - getScrollX();
  292.         mScroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta) * 2);
  293.         invalidate();
  294.     }

  295.     protected void snapToDestination() {
  296.         if (getScrollX() == 0) {
  297.             return;
  298.         }
  299.         final int screenWidth = getChildAt(0).getWidth();
  300.         final int whichScreen = (screenWidth + getScrollX() + (screenWidth / 2))
  301.                 / screenWidth;
  302.         snapToScreen(whichScreen);
  303.     }

  304.     public int getCurrentScreen() {
  305.         return mCurrentScreen;
  306.     }

  307.     public boolean isMainScreenShowing() {
  308.         return mCurrentScreen == SCREEN_MAIN;
  309.     }

  310.     public void openMenu() {
  311.         mCurrentScreen = SCREEN_MENU;
  312.         snapToScreen(mCurrentScreen);
  313.     }

  314.     public void closeMenu() {
  315.         mCurrentScreen = SCREEN_MAIN;
  316.         snapToScreen(mCurrentScreen);
  317.     }

  318.     public void unlock() {
  319.         mLocked = false;
  320.     }

  321.     public void lock() {
  322.         mLocked = true;
  323.     }

  324. }


点击(此处)折叠或打开

  1. package cn.edu.zzu.wemall.ui;

  2. import cn.edu.zzu.wemall.R;
  3. import android.annotation.SuppressLint;
  4. import android.content.Context;
  5. import android.graphics.Canvas;
  6. import android.support.v4.view.ViewPager;
  7. import android.util.AttributeSet;
  8. import android.view.MotionEvent;
  9. import android.view.VelocityTracker;
  10. import android.view.View;
  11. import android.view.ViewConfiguration;
  12. import android.view.ViewGroup;
  13. import android.widget.Scroller;

  14. /*
  15.  * 侧边栏类
  16.  *
  17.  */
  18. public class SlideMenu extends ViewGroup {
  19.     public static final int SCREEN_MENU = 0;
  20.     public static final int SCREEN_MAIN = 1;
  21.     private static final int SCREEN_INVALID = -1;

  22.     private int mCurrentScreen;
  23.     private int mNextScreen = SCREEN_INVALID;

  24.     private Scroller mScroller;
  25.     private VelocityTracker mVelocityTracker;
  26.     private int mTouchSlop;

  27.     private float mLastMotionX;
  28.     private float mLastMotionY;

  29.     private final static int TOUCH_STATE_REST = 0;
  30.     private final static int TOUCH_STATE_SCROLLING = 1;
  31.     private static final int SNAP_VELOCITY = 1000;

  32.     public int mTouchState = TOUCH_STATE_REST;
  33.     private boolean mLocked;
  34.     private boolean mAllowLongPress;

  35.     public SlideMenu(Context context) {
  36.         this(context, null, 0);
  37.     }

  38.     public SlideMenu(Context context, AttributeSet attrs) {
  39.         this(context, attrs, 0);
  40.     }


  41.     public SlideMenu(Context context, AttributeSet attrs, int defStyle) {
  42.         super(context, attrs, defStyle);

  43.         mScroller = new Scroller(getContext());
  44.         mCurrentScreen = SCREEN_MAIN;
  45.         mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop();
  46.     }

  47.     @Override
  48.     protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  49.         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  50.         measureViews(widthMeasureSpec, heightMeasureSpec);
  51.     }

  52.     public void measureViews(int widthMeasureSpec, int heightMeasureSpec) {
  53.         View menuView = getChildAt(0);
  54.         menuView.measure(menuView.getLayoutParams().width + menuView.getLeft()
  55.                 + menuView.getRight(), heightMeasureSpec);

  56.         View contentView = getChildAt(1);
  57.         contentView.measure(widthMeasureSpec, heightMeasureSpec);
  58.     }

  59.     @Override
  60.     protected void onLayout(boolean changed, int l, int t, int r, int b) {
  61.         int childCount = getChildCount();
  62.         if (childCount != 2) {
  63.             throw new IllegalStateException(
  64.                     "The childCount of SlidingMenu must be 2");
  65.         }

  66.         View menuView = getChildAt(0);
  67.         final int width = menuView.getMeasuredWidth();
  68.         menuView.layout(-width, 0, 0, menuView.getMeasuredHeight());

  69.         View contentView = getChildAt(1);
  70.         contentView.layout(0, 0, contentView.getMeasuredWidth(),
  71.                 contentView.getMeasuredHeight());
  72.     }

  73.     @Override
  74.     protected void onFinishInflate() {
  75.         super.onFinishInflate();
  76.         View child;
  77.         for (int i = 0; i < getChildCount(); i++) {
  78.             child = getChildAt(i);
  79.             child.setFocusable(true);
  80.             child.setClickable(true);
  81.         }
  82.     }

  83.     @Override
  84.     public boolean onInterceptTouchEvent(MotionEvent ev) {
  85.         if (mLocked) {
  86.             return true;
  87.         }

  88.         final int action = ev.getAction();
  89.         if ((action == MotionEvent.ACTION_MOVE)
  90.                 && (mTouchState != TOUCH_STATE_REST)) {
  91.             return true;
  92.         }

  93.         final float x = ev.getX();
  94.         final float y = ev.getY();

  95.         switch (action) {
  96.         case MotionEvent.ACTION_MOVE:
  97.             //在这里做点文章,侧滑菜单和viewpager的滑动兼容,头疼。。。
  98.             /**
  99.              * 设定条件,如果当前页面在商品页面,往右侧拉动屏幕,显示菜单,往左则收回菜单,再往左拉
  100.              * 则切换到购物车
  101.              * 如果当前页面在购物车或者个人中心,则无论左右拉,都不理会菜单栏!,直接切换viewpager
  102.              */
  103.             ViewPager viewPager = (ViewPager) findViewById(R.id.vPager);
  104.             if(viewPager.getCurrentItem()>0){ //如果当前页面不在商品页面,则忽略滑动出现菜单
  105.                 break;
  106.             }

  107.             final int xDiff = (int) Math.abs(x - mLastMotionX);
  108.             final int yDiff = (int) Math.abs(y - mLastMotionY);
  109.             //菜单没显示且往右滑动时break
  110.             if(isMainScreenShowing()&&(x - mLastMotionX)<0){
  111.                 break;
  112.             }
  113.             final int touchSlop = mTouchSlop;
  114.             boolean xMoved = xDiff > touchSlop;
  115.             boolean yMoved = yDiff > touchSlop;

  116.             if (xMoved || yMoved) {

  117.                 if (xMoved) {
  118.                     // Scroll if the user moved far enough along the X axis
  119.                     mTouchState = TOUCH_STATE_SCROLLING;
  120.                     enableChildrenCache();
  121.                 }
  122.                 // Either way, cancel any pending longpress
  123.                 if (mAllowLongPress) {
  124.                     mAllowLongPress = false;
  125.                     // Try canceling the long press. It could also have been
  126.                     // scheduled
  127.                     // by a distant descendant, so use the mAllowLongPress flag
  128.                     // to block
  129.                     // everything
  130.                     final View currentScreen = getChildAt(mCurrentScreen);
  131.                     currentScreen.cancelLongPress();
  132.                 }
  133.             }
  134.             break;

  135.         case MotionEvent.ACTION_DOWN:
  136.             
  137.             // Remember location of down touch
  138.             mLastMotionX = x;
  139.             mLastMotionY = y;
  140.             mAllowLongPress = true;
  141.             mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST
  142.                     : TOUCH_STATE_SCROLLING;
  143.             break;

  144.         case MotionEvent.ACTION_CANCEL:
  145.         case MotionEvent.ACTION_UP:
  146.             // Release the drag
  147.             clearChildrenCache();
  148.             mTouchState = TOUCH_STATE_REST;
  149.             mAllowLongPress = false;
  150.             break;
  151.         }

  152.         /*
  153.          * The only time we want to intercept motion events is if we are in the
  154.          * drag mode.
  155.          */
  156.         return mTouchState != TOUCH_STATE_REST;
  157.     }

  158.     void enableChildrenCache() {
  159.         final int count = getChildCount();
  160.         for (int i = 0; i < count; i++) {
  161.             final View layout = (View) getChildAt(i);
  162.             layout.setDrawingCacheEnabled(true);
  163.         }
  164.     }

  165.     void clearChildrenCache() {
  166.         final int count = getChildCount();
  167.         for (int i = 0; i < count; i++) {
  168.             final View layout = (View) getChildAt(i);
  169.             layout.setDrawingCacheEnabled(false);
  170.         }
  171.     }

  172.     @SuppressLint("ClickableViewAccessibility")
  173.     @Override
  174.     public boolean onTouchEvent(MotionEvent ev) {
  175.         if (mLocked) {
  176.             return true;
  177.         }

  178.         if (mVelocityTracker == null) {
  179.             mVelocityTracker = VelocityTracker.obtain();
  180.         }
  181.         mVelocityTracker.addMovement(ev);

  182.         final int action = ev.getAction();
  183.         final float x = ev.getX();
  184.         switch (action) {
  185.         case MotionEvent.ACTION_DOWN:
  186.             /*
  187.              * If being flinged and user touches, stop the fling. isFinished
  188.              * will be false if being flinged.
  189.              */
  190.             if (!mScroller.isFinished()) {
  191.                 mScroller.abortAnimation();
  192.             }

  193.             // Remember where the motion event started
  194.             mLastMotionX = x;
  195.             break;
  196.         case MotionEvent.ACTION_MOVE:
  197.             if (mTouchState == TOUCH_STATE_SCROLLING) {
  198.                 // Scroll to follow the motion event
  199.                 final int deltaX = (int) (mLastMotionX - x);
  200.                 mLastMotionX = x;
  201.                 if (deltaX < 0) {
  202.                     if (deltaX + getScrollX() >= -getChildAt(0).getWidth()) {
  203.                         scrollBy(deltaX, 0);
  204.                     }

  205.                 } else if (deltaX > 0) {
  206.                     
  207.                     final int availableToScroll = getChildAt(
  208.                             getChildCount() - 1).getRight()
  209.                             - getScrollX() - getWidth();

  210.                     if (availableToScroll > 0) {
  211.                         scrollBy(Math.min(availableToScroll, deltaX), 0);
  212.                     }
  213.                 }
  214.             }
  215.             break;
  216.         case MotionEvent.ACTION_UP:
  217.             if (mTouchState == TOUCH_STATE_SCROLLING) {
  218.                 final VelocityTracker velocityTracker = mVelocityTracker;
  219.                 velocityTracker.computeCurrentVelocity(1000);
  220.                 int velocityX = (int) velocityTracker.getXVelocity();

  221.                 if (velocityX > SNAP_VELOCITY && mCurrentScreen == SCREEN_MAIN) {
  222.                     // Fling hard enough to move left
  223.                     snapToScreen(SCREEN_MENU);
  224.                 } else if (velocityX < -SNAP_VELOCITY
  225.                         && mCurrentScreen == SCREEN_MENU) {
  226.                     // Fling hard enough to move right
  227.                     snapToScreen(SCREEN_MAIN);
  228.                 } else {
  229.                     snapToDestination();
  230.                 }

  231.                 if (mVelocityTracker != null) {
  232.                     mVelocityTracker.recycle();
  233.                     mVelocityTracker = null;
  234.                 }
  235.             }
  236.             mTouchState = TOUCH_STATE_REST;
  237.             break;
  238.         case MotionEvent.ACTION_CANCEL:
  239.             mTouchState = TOUCH_STATE_REST;
  240.         }

  241.         return true;
  242.     }

  243.     @Override
  244.     public void computeScroll() {
  245.         if (mScroller.computeScrollOffset()) {
  246.             scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
  247.         } else if (mNextScreen != SCREEN_INVALID) {
  248.             mCurrentScreen = Math.max(0,
  249.                     Math.min(mNextScreen, getChildCount() - 1));
  250.             mNextScreen = SCREEN_INVALID;
  251.             clearChildrenCache();
  252.         }
  253.     }

  254.     @Override
  255.     public void scrollTo(int x, int y) {
  256.         super.scrollTo(x, y);
  257.         postInvalidate();
  258.     }

  259.     @Override
  260.     protected void dispatchDraw(Canvas canvas) {
  261.         final int scrollX = getScrollX();
  262.         super.dispatchDraw(canvas);
  263.         canvas.translate(scrollX, 0);
  264.     }

  265.     @Override
  266.     public boolean dispatchUnhandledMove(View focused, int direction) {
  267.         if (direction == View.FOCUS_LEFT) {
  268.             if (getCurrentScreen() > 0) {
  269.                 snapToScreen(getCurrentScreen() - 1);
  270.                 return true;
  271.             }
  272.         } else if (direction == View.FOCUS_RIGHT) {
  273.             if (getCurrentScreen() < getChildCount() - 1) {
  274.                 snapToScreen(getCurrentScreen() + 1);
  275.                 return true;
  276.             }
  277.         }
  278.         return super.dispatchUnhandledMove(focused, direction);
  279.     }

  280.     protected void snapToScreen(int whichScreen) {

  281.         enableChildrenCache();

  282.         whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
  283.         boolean changingScreens = whichScreen != mCurrentScreen;

  284.         mNextScreen = whichScreen;

  285.         View focusedChild = getFocusedChild();
  286.         if (focusedChild != null && changingScreens
  287.                 && focusedChild == getChildAt(mCurrentScreen)) {
  288.             focusedChild.clearFocus();
  289.         }

  290.         final int newX = (whichScreen - 1) * getChildAt(0).getWidth();
  291.         final int delta = newX - getScrollX();
  292.         mScroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta) * 2);
  293.         invalidate();
  294.     }

  295.     protected void snapToDestination() {
  296.         if (getScrollX() == 0) {
  297.             return;
  298.         }
  299.         final int screenWidth = getChildAt(0).getWidth();
  300.         final int whichScreen = (screenWidth + getScrollX() + (screenWidth / 2))
  301.                 / screenWidth;
  302.         snapToScreen(whichScreen);
  303.     }

  304.     public int getCurrentScreen() {
  305.         return mCurrentScreen;
  306.     }

  307.     public boolean isMainScreenShowing() {
  308.         return mCurrentScreen == SCREEN_MAIN;
  309.     }

  310.     public void openMenu() {
  311.         mCurrentScreen = SCREEN_MENU;
  312.         snapToScreen(mCurrentScreen);
  313.     }

  314.     public void closeMenu() {
  315.         mCurrentScreen = SCREEN_MAIN;
  316.         snapToScreen(mCurrentScreen);
  317.     }

  318.     public void unlock() {
  319.         mLocked = false;
  320.     }

  321.     public void lock() {
  322.         mLocked = true;
  323.     }

  324. }

 原文详情地址:http://git.oschina.net/zzunet/wemall-doraemon/commit/e8f303df5663dc69fe47bb9623222149d40e3956

wemall doraemonAndroid app商城详情地址:http://www.koahub.com/home/product/55

wemall官网地址:http://www.wemallshop.com

wemall 开源微商城 ,微信商城,商城源码,三级分销,微生鲜,微水果,微外卖,微订餐---专业的o2o系统

 

 

 

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