Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在android中向左或向右滑动图像时获取下一个图像_Android_Image_Slider_Swipe - Fatal编程技术网

在android中向左或向右滑动图像时获取下一个图像

在android中向左或向右滑动图像时获取下一个图像,android,image,slider,swipe,Android,Image,Slider,Swipe,我正在使用此链接中的代码制作图像滑块 //This method is called each time user swipes the screen @Override public Object instantiateItem(ViewGroup container, final int position) { TouchImageView imgDisplay; imgDisplay = (TouchImageView) viewLayout.findViewById(R.

我正在使用此链接中的代码制作图像滑块

//This method is called each time user swipes the screen
@Override
public Object instantiateItem(ViewGroup container, final int position)
  {
   TouchImageView imgDisplay;
   imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);

   imgDisplay.setOnTouchListener(new OnTouchListener() {

        float x1, x2, y1, y2;
        String direction;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {

            case (MotionEvent.ACTION_DOWN):
                x1 = event.getX();
                y1 = event.getY();
                break;
            case (MotionEvent.ACTION_MOVE): {
                x2 = event.getX();
                y2 = event.getY();
                float differenceInX = x2 - x1;
                float differenceInY = y2 - y1;

                // Use dx and dy to determine the direction
                if (Math.abs(differenceInX) > Math.abs(differenceInY)) {
                    if (differenceInX > 0) {
                        System.out.println("SWIPING RIGHT");
                            Toast.makeText(_activity, "Right",
                                    Toast.LENGTH_SHORT).show();

                     } 
                   else 
                     {
                        System.out.println("SWIPING LEFT "+position);
                            Toast.makeText(_activity, "Left",
                                    Toast.LENGTH_SHORT).show();
                     }
                  }
               }

            }
            return false;
        }

    });

     BitmapFactory.Options options = new BitmapFactory.Options();
     options.inPreferredConfig = Bitmap.Config.ARGB_8888;
      //_imagePaths is an arraylist that stores all the image paths.
     Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position),options);
     imgDisplay.setImageBitmap(bitmap);
  }
我必须添加以下功能:

如果我向左滑动第一个图像,我会得到第二个图像。但是如果我向右滑动第一个图像,那么我必须得到第二个图像

我会说得更清楚

如果我向左滑动第二个图像,我会得到第三个图像

如果我将第二个图像向右滑动,那么我必须得到第三个图像,而不是第一个图像


目标是在每次我向左或向右滑动图像时都获得下一个图像。

当您使用适配器位置从图像数组中滑动图像时,应使用gallery Widget,因此创建全局整数计数=0;当您向左或向右滑动并使用变量计数(而不是位置)显示图像时,将其递增