如何通过点击和最后一个屏幕点击它来获取屏幕应该是以前的活动

时间:2014-04-04 11:25:53

标签: android

如何通过点击和最后一个屏幕点击它来获取屏幕应该是之前的活动,以下是我的代码。我想通过点击而不是滚动修改此代码。所以请一些人帮助我。这工作正常,但我需要当我点击它应该移动到下一个,但在这里我通过滚动。

         public class RealViewSwitcher extends ViewGroup {
         public static interface OnScreenSwitchListener {
         void onScreenSwitched(int screen);
     }
private static final int SNAP_VELOCITY = 1000;
private static final int INVALID_SCREEN = -1;
private Scroller mScroller;
private VelocityTracker mVelocityTracker;
private final static int TOUCH_STATE_REST = 0;
private final static int TOUCH_STATE_SCROLLING = 1;
private int mTouchState = TOUCH_STATE_REST;
private float mLastMotionX;
private int mTouchSlop,mMaximumVelocity,mCurrentScreen,mNextScreen = INVALID_SCREEN;
private boolean mFirstLayout = true;
private OnScreenSwitchListener mOnScreenSwitchListener;
public RealViewSwitcher(Context context) {
    super(context);
    init();
}

public RealViewSwitcher(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

private void init() {
    mScroller = new Scroller  (getContext() );
    final ViewConfiguration configuration = ViewConfiguration.get(getContext());
    mTouchSlop = configuration.getScaledTouchSlop();
    mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
}
          protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    final int width = MeasureSpec.getSize(widthMeasureSpec);
    final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
         final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        final int count = getChildCount();
    for (int i = 0; i < count; i++) {
        getChildAt(i).measure(widthMeasureSpec, heightMeasureSpec);
    }

    if (mFirstLayout) {
        scrollTo(mCurrentScreen * width, 0);
        mFirstLayout = false;
    }
}
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    int childLeft = 0;
    final int count = getChildCount();
    for (int i = 0; i < count; i++) {
        final View child = getChildAt(i);
        if (child.getVisibility() != View.GONE) {
            final int childWidth = child.getMeasuredWidth();
            child.layout(childLeft, 0, childLeft + childWidth, child.getMeasuredHeight());
            childLeft += childWidth;
        }
    }
}

@Override
public boolean onTouchEvent(MotionEvent ev) {
    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 (!mScroller.isFinished()) {
            mScroller.abortAnimation();
        }
        mLastMotionX = x;
        mTouchState = mScroller.isFinished() ? TOUCH_STATE_REST : TOUCH_STATE_SCROLLING;
        break;
    case MotionEvent.ACTION_MOVE:
        final int xDiff = (int) Math.abs(x - mLastMotionX);
        boolean xMoved = xDiff > mTouchSlop;
        if (xMoved) {
            mTouchState = TOUCH_STATE_SCROLLING;
        }
        if (mTouchState == TOUCH_STATE_SCROLLING) {
            final int deltaX = (int) (mLastMotionX - x);
            mLastMotionX = x;
            final int scrollX = getScrollX();
            if (deltaX < 0) {
                if (scrollX > 0) {
                    scrollBy(Math.max(-scrollX, deltaX), 0);
                }
            } else if (deltaX > 0) {
                final int availableToScroll = getChildAt(getChildCount() - 1).getRight() - scrollX - 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, mMaximumVelocity);
            int velocityX = (int) velocityTracker.getXVelocity();

            if (velocityX > SNAP_VELOCITY && mCurrentScreen > 0) {
                snapToScreen(mCurrentScreen - 1);
            } else if (velocityX < -SNAP_VELOCITY && mCurrentScreen < getChildCount() - 1) { 
                snapToScreen(mCurrentScreen + 1);
            } else {
                snapToDestination();
            }
            if (mVelocityTracker != null) {
                mVelocityTracker.recycle();
                mVelocityTracker = null;
            }
        }

        mTouchState = TOUCH_STATE_REST;

        break;
    case MotionEvent.ACTION_CANCEL:
        mTouchState = TOUCH_STATE_REST;
    }
    return true;
}
private void snapToDestination() {
    final int screenWidth = getWidth();
    final int whichScreen = (getScrollX() + (screenWidth / 2)) / screenWidth;
    snapToScreen(whichScreen);
}
private void snapToScreen(int whichScreen) {
    if (!mScroller.isFinished())
        return;
    whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
    mNextScreen = whichScreen;
    final int newX = whichScreen * getWidth();
    final int delta = newX - getScrollX();
    mScroller.startScroll(getScrollX(), 0, delta, 0, Math.abs(delta) * 2);
    invalidate();
}
public void computeScroll() {
    if (mScroller.computeScrollOffset()) {
        scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
        postInvalidate();
    } else if (mNextScreen != INVALID_SCREEN) {
        mCurrentScreen = Math.max(0, Math.min(mNextScreen, getChildCount() - 1));
        if (mOnScreenSwitchListener != null)
            mOnScreenSwitchListener.onScreenSwitched(mCurrentScreen);
        mNextScreen = INVALID_SCREEN;
    }
}
public int getCurrentScreen() {
    return mCurrentScreen;
}
public void setCurrentScreen(int currentScreen) {
    mCurrentScreen = Math.max(0, Math.min(currentScreen, getChildCount() - 1));
    scrollTo(mCurrentScreen * getWidth(), 0);
    invalidate();
}
public void setOnScreenSwitchListener(OnScreenSwitchListener onScreenSwitchListener) {      mOnScreenSwitchListener = onScreenSwitchListener;
}

}

1 个答案:

答案 0 :(得分:0)

onTouchEvent功能,内部切换案例MotionEvent.ACTION_UP中,请在if条件后添加此内容

  else {
      switchToScreen(mCurrentScreen + 1);
  }

将此功能添加到您的班级

    private void switchToScreen(int whichScreen) {
       if (!mScroller.isFinished())
             mScroller.abortAnimation();
        whichScreen = Math.max(0, Math.min(whichScreen, getChildCount() - 1));
        mNextScreen = whichScreen;
        final int newX = whichScreen * getWidth();
        mScroller.setFinalX(newX);
        invalidate();
    }