Search code examples
androidswipeviewpage

Android: PagerAdapter remove last view so you can't swipe left


Is there a way to prevent a user swiping to the left in PagerAdapter?

When the user first enter the app they can't swipe to the left because there isn't anything there. So what if everytime you swiped to the right the left image gets destroyed and there will be nothing there like when you ran the app at first.

        @Override
        public Object instantiateItem(ViewGroup container, int position) 
        {
            Context context = MainActivity.this;

            //
            Integer highest_img = mImages.length - 1;
            Integer randomInt = getRanValue(highest_img);
            //

            ImageView imageView = new ImageView(context);
            int padding = context.getResources().getDimensionPixelSize(R.dimen.padding_medium);
            imageView.setPadding(padding, padding, padding, padding);
            imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
            imageView.setImageResource(mImages[randomInt]);

            ((ViewPager) container).addView(imageView, 0);

            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) 
        {
            ((ViewPager) container).removeView((ImageView) object);
        }

Solution

  • I found a better solution for my problem.

    I used the onFling() method with MotionEvent and when somebody swipe i'll just run an animation with an image replace.