Android列表视图只处理左滑动

时间:2014-11-12 08:16:34

标签: java android android-listview

目前我正在使用http://www.apkbus.com/android-178853-1-1.html中的代码处理listview swipe.It从右向左滑动效果非常好,但我无法使其从左向右滑动。 感谢您的帮助。 以下是示例

中的一小段代码
public boolean onTouch(final View view, MotionEvent event) {
        if (mViewWidth < 2) {
            mViewWidth  =   mListView.getWidth();
            smallWidth  =   mViewWidth/7;
            textwidth2  =   mViewWidth/3;
            textwidth   =   textwidth2;
            largewidth  =   textwidth+textwidth2;
        }

        int tempwidth   =   0;
        if(SwipeType==1)
            tempwidth = smallWidth;
        else
            tempwidth = textwidth2;

         switch (event.getActionMasked()) {
            case MotionEvent.ACTION_DOWN: {
                if (mPaused) {
                    return false;
                }
                Rect rect               =   new Rect();
                int childCount          =   mListView.getChildCount();
                int[] listViewCoords    = new int[2];
                mListView.getLocationOnScreen(listViewCoords);
                int x                   = (int) event.getRawX() - listViewCoords[0];
                int y                   = (int) event.getRawY() - listViewCoords[1];
                ViewGroup child;
                for (int i = 0; i < childCount; i++) {
                    child = (ViewGroup) mListView.getChildAt(i);
                    child.getHitRect(rect);
                    if (rect.contains(x, y)) {
                        mDownView_parent    =   child;
                        mDownView           =   (ViewGroup) child.findViewById(R.id.list_display_view_container);
                        if(mDownView_parent.getChildCount()==1){
                            textheight  =   mDownView_parent.getHeight();
                            if(SwipeType==Dismiss){
                                HalfColor       =   singleColor;
                                HalfDrawable    =   activity.getResources().getDrawable(R.drawable.content_discard);
                            }
                           SetBackGroundforList();
                        }


                        if(old_mDownView!=null && mDownView!=old_mDownView){
                            ResetListItem(old_mDownView);
                            old_mDownView=null;
                            return false;
                        }
                        break;
                    }
                }

                if (mDownView != null) {
                    mDownX              =   event.getRawX();
                    mDownPosition       =   mListView.getPositionForView(mDownView);
                    mVelocityTracker    =   VelocityTracker.obtain();
                    mVelocityTracker.addMovement(event);
                } else {
                    mDownView               =   null;
                }

                //mSwipeDetected              =   false;
                temp_position               =   mListView.pointToPosition((int) event.getX(), (int) event.getY());
                view.onTouchEvent(event);
                return true;
            }

            case MotionEvent.ACTION_UP: {
                if (mVelocityTracker == null) {
                    break;
                }
                float deltaX                =   event.getRawX() - mDownX;
                mVelocityTracker.addMovement(event);
                mVelocityTracker.computeCurrentVelocity(1000); // 1000 by defaut but
                float velocityX             =   mVelocityTracker.getXVelocity();                                            // it was too much
                float absVelocityX          =   Math.abs(velocityX);
                float absVelocityY          =   Math.abs(mVelocityTracker.getYVelocity());
                boolean swipe               =   false;
                boolean swipeRight          =   false;

                if (Math.abs(deltaX) > tempwidth) {
                    swipe               =   true;
                    swipeRight          =   deltaX > 0;

                }else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity && absVelocityY < absVelocityX) {
                    // dismiss only if flinging in the same direction as dragging
                    swipe   = (velocityX < 0) == (deltaX < 0);
                    swipeRight          = mVelocityTracker.getXVelocity() > 0;
                }

                if (deltaX < 0 && swipe) {
                    mListView.setDrawSelectorOnTop(false);

                    if (swipe && !swipeRight && deltaX <= -tempwidth) {
                        FullSwipeTrigger();
                    } else if (deltaX >= -textwidth && SwipeType==Double) {
                        ResetListItem(mDownView);
                    }else {
                        ResetListItem(mDownView);
                    }
                } else {
                    ResetListItem(mDownView);
                }

                mVelocityTracker.recycle();
                mVelocityTracker    =   null;
                mDownX              =   0;
                mDownView           =   null;
                mDownPosition       =   ListView.INVALID_POSITION;
                mSwiping            =   false;
                break;
            }

            case MotionEvent.ACTION_MOVE: {

                float deltaX = event.getRawX() - mDownX;
                if (mVelocityTracker == null || mPaused ||deltaX>0) {
                    break;
                }

                mVelocityTracker.addMovement(event);


                if (Math.abs(deltaX) > mSlop) {
                    mSwiping = true;
                    mListView.requestDisallowInterceptTouchEvent(true);

                    // Cancel ListView's touch (un-highlighting the item)
                    MotionEvent cancelEvent     =   MotionEvent.obtain(event);
                    cancelEvent.setAction(MotionEvent.ACTION_CANCEL |
                            (event.getActionIndex()
                                    << MotionEvent.ACTION_POINTER_INDEX_SHIFT));
                    mListView.onTouchEvent(cancelEvent);
                    cancelEvent.recycle();
                }
                if (mSwiping && deltaX < 0 ) {

                    int width;
                    if(SwipeType==1){
                        width   =   textwidth;
                    }
                    else{
                        width   =   largewidth;
                    }

                    if (-deltaX<width){
                        mDownView.setTranslationX(deltaX);
                        return false;
                    }
                    return false;
                }else if(mSwiping){
                    ResetListItem(mDownView);
                }
                break;
            }
        }
        return false;
    }

0 个答案:

没有答案