何通过点击和最后一个屏幕上点击来获得它的屏幕应previous活

何通过点击和最后一个屏幕上点击来获得它的屏幕应previous活

本文介绍了如何通过点击和最后一个屏幕上点击来获得它的屏幕应previous活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过点击和最后一个屏幕点击屏幕得到它应该为previous活动,以下是我的codeI希望通过单击而不是scrolling.so请有人帮忙修改此code作为我。这工作正常,但是当我点击它应该移动到下一个我需要的,但在这里我得到滚动。

how to get screens by clicking and by last screen clicking it should to previous activity and following is my code.i want to modify this code as by clicking instead of scrolling.so please some one help me. this works fine but i need when i click on it should move to next,but here i got by scrolling.

         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;
}

}

推荐答案

在你的 onTouchEvent 功能,内置开关的情况下 MotionEvent.ACTION_UP ,请添加后,这是你的,如果条件

In your onTouchEvent function, inside switch case MotionEvent.ACTION_UP, please add this after your if condition

  else {
      switchToScreen(mCurrentScreen + 1);
  }

这个功能添加到您的类

add this function to your class

    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();
    }

这篇关于如何通过点击和最后一个屏幕上点击来获得它的屏幕应previous活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 05:59