完美解決左側抽屜菜單和viewpager不能兼容左右滑動的問題,可進行參考。
WeMall-Client/res/layout/wemall_main_ui.xml
No newline at end of file
WeMall-Client/src/cn/edu/zzu/wemall/ui/MainUIMain.java
import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; import android.view.MotionEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnTouchListener; import android.view.ViewGroup; import android.view.Window; import android.view.inputmethod.InputMethodManager; @@ -105,6 +107,8 @@ public class MainUIMain extends FragmentActivity implements menudata = new ArrayList>(); menuadapter = new MenuAdapter(this, menudata); menulistview.setAdapter(menuadapter); // 初始化ViewPager,菜單數據 InitViewPager(); @@ -206,8 +210,9 @@ public class MainUIMain extends FragmentActivity implements public void onPageScrollStateChanged(int arg0) { // 在這里判斷vpage是否滑到了商品頁面,如果滑到了商品頁面并且繼續向右拉動屏幕,則展現菜單列表// if (this.viewPager.getCurrentItem() == 0 && arg0 == 1) { System.out.println("滑到了最左邊且在基于往右側滑動"); //slideMenu.openMenu(); } else { } }
**WeMall-Client/src/cn/edu/zzu/wemall/ui/SlideMenu.java
package cn.edu.zzu.wemall.ui;**
import cn.edu.zzu.wemall.R; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.VelocityTracker; import android.view.View; import android.view.ViewConfiguration; import android.view.ViewGroup; import android.widget.Scroller; /* * 側邊欄類 * */ public class SlideMenu extends ViewGroup { public static final int SCREEN_MENU = 0; public static final int SCREEN_MAIN = 1; private static final int SCREEN_INVALID = -1; private int mCurrentScreen; private int mNextScreen = SCREEN_INVALID; private Scroller mScroller; private VelocityTracker mVelocityTracker; private int mTouchSlop; private float mLastMotionX; private float mLastMotionY; private final static int TOUCH_STATE_REST = 0; private final static int TOUCH_STATE_SCROLLING = 1; private static final int SNAP_VELOCITY = 1000; public int mTouchState = TOUCH_STATE_REST; private boolean mLocked; private boolean mAllowLongPress; public SlideMenu(Context context) { this(context, null, 0); } public SlideMenu(Context context, AttributeSet attrs) { this(context, attrs, 0); } public SlideMenu(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mScroller = new Scroller(getContext()); mCurrentScreen = SCREEN_MAIN; mTouchSlop = ViewConfiguration.get(getContext()).getScaledTouchSlop(); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); measureViews(widthMeasureSpec, heightMeasureSpec); } public void measureViews(int widthMeasureSpec, int heightMeasureSpec) { View menuView = getChildAt(0); menuView.measure(menuView.getLayoutParams().width + menuView.getLeft() + menuView.getRight(), heightMeasureSpec); View contentView = getChildAt(1); contentView.measure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int childCount = getChildCount(); if (childCount != 2) { throw new IllegalStateException( "The childCount of SlidingMenu must be 2"); } View menuView = getChildAt(0); final int width = menuView.getMeasuredWidth(); menuView.layout(-width, 0, 0, menuView.getMeasuredHeight()); View contentView = getChildAt(1); contentView.layout(0, 0, contentView.getMeasuredWidth(), contentView.getMeasuredHeight()); } @Override protected void onFinishInflate() { super.onFinishInflate(); View child; for (int i = 0; i < getChildCount(); i++) { child = getChildAt(i); child.setFocusable(true); child.setClickable(true); } } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { if (mLocked) { return true; } final int action = ev.getAction(); if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) { return true; } final float x = ev.getX(); final float y = ev.getY(); switch (action) { case MotionEvent.ACTION_MOVE: //在這里做點文章,側滑菜單和viewpager的滑動兼容,頭疼。。。 /** * 設定條件,如果當前頁面在商品頁面,往右側拉動屏幕,顯示菜單,往左則收回菜單,再往左拉 * 則切換到購物車 * 如果當前頁面在購物車或者個人中心,則無論左右拉,都不理會菜單欄!,直接切換viewpager */ ViewPager viewPager = (ViewPager) findViewById(R.id.vPager); if(viewPager.getCurrentItem()>0){ //如果當前頁面不在商品頁面,則忽略滑動出現菜單 break; } final int xDiff = (int) Math.abs(x - mLastMotionX); final int yDiff = (int) Math.abs(y - mLastMotionY); //菜單沒顯示且往右滑動時break if(isMainScreenShowing()&&(x - mLastMotionX)<0){ break; } final int touchSlop = mTouchSlop; boolean xMoved = xDiff > touchSlop; boolean yMoved = yDiff > touchSlop; if (xMoved || yMoved) { if (xMoved) { // Scroll if the user moved far enough along the X axis mTouchState = TOUCH_STATE_SCROLLING; enableChildrenCache(); } // Either way, cancel any pending longpress if (mAllowLongPress) { mAllowLongPress = false; // Try canceling the long press. It could also have been // scheduled // by a distant descendant, so use the mAllowLongPress flag // to block // everything final View currentScreen = getChildAt(mCurrentScreen); currentScreen.cancelLongPress(); } } break; case MotionEvent.ACTION_DOWN: // Remember location of down touch mLastMotionX = x; mLastMotionY = y; mAllowLongPress = true; mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING; break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: // Release the drag clearChildrenCache(); mTouchState = TOUCH_STATE_REST; mAllowLongPress = false; break; } /* * The only time we want to intercept motion events is if we are in the * drag mode. */ return mTouchState != TOUCH_STATE_REST; } void enableChildrenCache() { final int count = getChildCount(); for (int i = 0; i < count; i++) { final View layout = (View) getChildAt(i); layout.setDrawingCacheEnabled(true); } } void clearChildrenCache() { final int count = getChildCount(); for (int i = 0; i < count; i++) { final View layout = (View) getChildAt(i); layout.setDrawingCacheEnabled(false); } } @SuppressLint("ClickableViewAccessibility") @Override public boolean onTouchEvent(MotionEvent ev) { if (mLocked) { return true; } if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } mVelocityTracker.addMovement(ev); final int action = ev.getAction(); final float x = ev.getX(); switch (action) { case MotionEvent.ACTION_DOWN: /* * If being flinged and user touches, stop the fling. isFinished * will be false if being flinged. */ if (!mScroller.isFinished()) { mScroller.abortAnimation(); } // Remember where the motion event started mLastMotionX = x; break; case MotionEvent.ACTION_MOVE: if (mTouchState == TOUCH_STATE_SCROLLING) { // Scroll to follow the motion event final int deltaX = (int) (mLastMotionX - x); mLastMotionX = x; if (deltaX < 0) { if (deltaX + getScrollX() >= -getChildAt(0).getWidth()) { scrollBy(deltaX, 0); } } else if (deltaX > 0) { final int availableToScroll = getChildAt( getChildCount() - 1).getRight() - getScrollX() - getWidth(); if (availableToScroll > 0) { scrollBy(Math.min(availableToScroll, deltaX), 0); } } } break; case MotionEvent.ACTION_UP: if (mTouchState == TOUCH_STATE_SCROLLING) { final VelocityTracker velocityTracker = mVelocityTracker; velocityTracker.computeCurrentVelocity(1000); int velocityX = (int) velocityTracker.getXVelocity(); if (velocityX > SNAP_VELOCITY && mCurrentScreen == SCREEN_MAIN) { // Fling hard enough to move left snapToScreen(SCREEN_MENU); } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen == SCREEN_MENU) { // Fling hard enough to move right snapToScreen(SCREEN_MAIN); } else { snapToDestination(); } if (mVelocityTracker != null) { mVelocityTracker.recycle(); mVelocityTracker = null; } } mTouchState = TOUCH_STATE_REST; break; case MotionEvent.ACTION_CANCEL: mTouchState = TOUCH_STATE_REST; } return true; } @Override public void computeScroll() { if (mScroller.computeScrollOffset()) { scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); } else if (mNextScreen != SCREEN_INVALID) { mCurrentScreen = Math.max(0, Math.min(mNextScreen, getChildCount() - 1)); mNextScreen = SCREEN_INVALID; clearChildrenCache(); } } @Override public void scrollTo(int x, int y) { super.scrollTo(x, y); postInvalidate(); } @Override protected void dispatchDraw(Canvas canvas) { final int scrollX = getScrollX(); super.dispatchDraw(canvas); canvas.translate(scrollX, 0); } @Override public boolean dispatchUnhandledMove(View focused, int direction) { if (direction == View.FOCUS_LEFT) { if (getCurrentScreen() > 0) { snapToScreen(getCurrentScreen() - 1); return true; } } else if (direction == View.FOCUS_RIGHT) { if (getCurrentScreen() < getChildCount() - 1) { snapToScreen(getCurrentScreen() + 1); return true; } } return super.dispatchUnhandledMove(focused, direction); } protected void snapToScreen(int whichScreen) { enableChildrenCache(); whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1)); boolean changingScreens = whichScreen != mCurrentScreen; mNextScreen = whichScreen; View focusedChild = getFocusedChild(); if (focusedChild != null && changingScreens && focusedChild == getChildAt(mCurrentScreen)) { focusedChild.clearFocus(); } final int newX = (whichScreen - 1) * getChildAt(0).getWidth(); final int delta = newX - getScrollX(); mScroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta) * 2); invalidate(); } protected void snapToDestination() { if (getScrollX() == 0) { return; } final int screenWidth = getChildAt(0).getWidth(); final int whichScreen = (screenWidth + getScrollX() + (screenWidth / 2)) / screenWidth; snapToScreen(whichScreen); } public int getCurrentScreen() { return mCurrentScreen; } public boolean isMainScreenShowing() { return mCurrentScreen == SCREEN_MAIN; } public void openMenu() { mCurrentScreen = SCREEN_MENU; snapToScreen(mCurrentScreen); } public void closeMenu() { mCurrentScreen = SCREEN_MAIN; snapToScreen(mCurrentScreen); } public void unlock() { mLocked = false; } public void lock() { mLocked = true; } }
原文詳情地址:http://git.oschina.net/zzunet...
wemall doraemonAndroid app商城詳情地址:http://www.koahub.com/home/pr...
wemall官網地址:http://www.wemallshop.com
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65277.html
摘要:本文分享開發中監聽事件主要代碼,供技術員參考學習。項目中越來越多的用到,下面記錄一下在中監聽的事件。實現事件,判斷當前的是哪一個,是不是所需要的然后在需要監聽事件的中寫一個靜態方法,傳遞與事件即可。 wemall-mobile是基于WeMall的android app商城,只需要在原商城目錄下上傳接口文件即可完成服務端的配置,客戶端可定制修改。本文分享android開發Fragment...
摘要:本文分享開發中監聽事件主要代碼,供技術員參考學習。項目中越來越多的用到,下面記錄一下在中監聽的事件。實現事件,判斷當前的是哪一個,是不是所需要的然后在需要監聽事件的中寫一個靜態方法,傳遞與事件即可。 wemall-mobile是基于WeMall的android app商城,只需要在原商城目錄下上傳接口文件即可完成服務端的配置,客戶端可定制修改。本文分享android開發Fragment...
摘要:是客戶端程序,服務端采用微信商城,不對原商城做任何修改,只需要在原商城目錄下上傳接口文件即可完成服務端的配置,客戶端可隨阿意定制修改。本文分享其中短信監聽接收器,用于自動獲取短信驗證碼,然后自動填寫到驗證碼區域代碼,供技術員參考學習。 wemall doraemon是Android客戶端程序,服務端采用wemall微信商城,不對原商城做任何修改,只需要在原商城目錄下上傳接口文件即可完成...
摘要:本文分享支付寶接口函數簽名驗簽解密等,供技術員參考學習。以下代碼只是為了方便商戶測試而提供的樣例代碼,商戶可以根據自己網站的需要,按照技術文檔編寫并非一定要使用該代碼,該代碼僅供學習和研究支付寶接口使用,只是提供一個參考。 wemall-mobile是基于WeMall的Android app商城,只需要在原商城目錄下上傳接口文件即可完成服務端的配置,客戶端可定制修改。本文分享支付寶接口...
摘要:本文分享支付寶接口函數簽名驗簽解密等,供技術員參考學習。以下代碼只是為了方便商戶測試而提供的樣例代碼,商戶可以根據自己網站的需要,按照技術文檔編寫并非一定要使用該代碼,該代碼僅供學習和研究支付寶接口使用,只是提供一個參考。 wemall-mobile是基于WeMall的Android app商城,只需要在原商城目錄下上傳接口文件即可完成服務端的配置,客戶端可定制修改。本文分享支付寶接口...
閱讀 460·2023-04-25 23:00
閱讀 3490·2021-11-22 13:54
閱讀 1890·2021-10-27 14:14
閱讀 1484·2019-08-30 13:59
閱讀 3509·2019-08-23 16:15
閱讀 1955·2019-08-23 16:06
閱讀 3321·2019-08-23 15:26
閱讀 1252·2019-08-23 13:48