Question

I am creating swipe in my app. When I am on last image and I swipe from right to left. Its still on same image thats fine. Then I swipe again from right to left. And then from left to right my app it crash. Similar case on first image. I know this is because of the increment in variable. I want if user on last image and he swipe from right to left even many times. And then if he swipe from left to right previous image come. Please help me to solve this. position lies b/w 0 to 8.

Code-

position = i.getExtras().getInt("id");
public void onRightToLeftSwipe(){
            Log.i(logTag, "RightToLeftSwipe!");
            if(position<mThumbId.length){
                position++;
                 changeBackground();
            }
    }

        public void onLeftToRightSwipe(){
            Log.i(logTag, "LeftToRightSwipe!");
            if(position>=0){
                position--;
                changeBackground();
           }
        }

      private void changeBackground(){
        full.setBackgroundResource(mThumbId[position]);
   }

I am getting array index out of bound error length=9 index=9 when I am on last image and swipe from right to left then swipe from left to right.

And I am getting array index out of bound error. length=9 index=-1. When I am on first image and swipe from left to right then swipe right to left.

Edit-

I want if user is on first image and he swipe from left to right image fixed. And when he swipe from right to left next image come.

Was it helpful?

Solution 2

This should do the trick :) I did not test it but it should work.

public void onRightToLeftSwipe(){
            Log.i(logTag, "RightToLeftSwipe!");
            if(position < mThumbId.length - 1){
                position++;
                 changeBackground();
            }
    }

        public void onLeftToRightSwipe(){
            Log.i(logTag, "LeftToRightSwipe!");
            if(position > 0){
                position--;
                changeBackground();
           }
        }

      private void changeBackground(){
        full.setBackgroundResource(mThumbId[position]);
   }

OTHER TIPS

You'll need to change your conditions for incrementing or decrementing position so that it doesn't go below zero or past the last index. The correct condition for left-to-right would be this:

if(position > 0) {
    position--;
    changeBackground();
}

This will ensure that if you're currently on the first image, you don't keep decrementing the position (because you want to stay where you are). It'll only go to the previous image if there are previous images.

Likewise, you want to change your second condition so that you only increase position if the next index is a valid one - you don't want to keep going to the next image if you're at the last one, i.e., position is mThumbId.length - 1. So your modified condition would be this:

if(position < mThumbId.length - 1) {
    position++;
    changeBackground();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top