0

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

}

image
  • 59
  • 1
  • 14

1 Answers1

0

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();
    }
Nitin Joshi
  • 128
  • 1
  • 10
  • it works nick.i will show to my tl and i will back dont go please. – image Apr 07 '14 at 07:28
  • she was busy so said to wait 2 minutes. – image Apr 07 '14 at 07:31
  • nick so thanks.how can i improve more knowlege in doing like this. – image Apr 07 '14 at 07:32
  • wc, keep on practicing dude.. you'll get it with xp – Nitin Joshi Apr 07 '14 at 07:34
  • hi nick, it was fine and i got other task.i will upload that class and i will ask u dont go it is some related to ble.please dont go wait for 1 minute – image Apr 07 '14 at 07:40
  • nick how can i upload my 2 java classes to ask question – image Apr 07 '14 at 07:50
  • you can simply add your code to you question, or if they are too long try unloading your code or file to some online sharing sites – Nitin Joshi Apr 07 '14 at 07:58
  • nick see this http://stackoverflow.com/posts/22906710/edit in this i pasted half of my device scan activity – image Apr 07 '14 at 08:02
  • wait nick i am pasting.devicecontrolactivity – image Apr 07 '14 at 08:21
  • hi nick check this link i uploaded both devicescan activity and device controlactivity http://stackoverflow.com/questions/22906710/how-to-add-small-circle-connecting-pop-up-menu-when-i-click-on-item – image Apr 07 '14 at 08:55
  • reply nick i got other question. – image Apr 11 '14 at 05:09
  • nick please check this http://stackoverflow.com/questions/23005637/how-to-fill-the-color-of-image-according-to-percentage-of-textview-in-android – image Apr 11 '14 at 07:15
  • nick u will not help me? r u busy. – image Apr 11 '14 at 07:24
  • hi nick,where r u please check this linkhttp://stackoverflow.com/questions/23005637/how-to-fill-the-color-of-image-according-to-percentage-of-textview-in-android – image Apr 11 '14 at 08:47
  • hi nick,if u free check this link http://stackoverflow.com/questions/23052877/how-to-add-progress-bar-or-look-like-similar-one-in-android – image Apr 14 '14 at 06:02
  • hi nick,please check this link http://stackoverflow.com/questions/23053164/how-to-save-toggle-button-state-to-database-and-if-user-edits-then-edited-state – image Apr 14 '14 at 08:47
  • hi nick did u forgot me?say – image Apr 14 '14 at 08:53
  • hi nick, please check this link it is urgent task for me.http://stackoverflow.com/questions/23076353/how-to-notify-characteristics-for-every-5secs-and-update-in-android – image Apr 15 '14 at 07:08
  • hi nick, please help me,once check this http://stackoverflow.com/questions/23901986/spinner-not-displaying-telugu-font – image May 28 '14 at 05:36