sayyam/carouselview

Change images dynamically

vangy1 opened this issue · 3 comments

Is there a way how to change images dynamically in the CarouselView on button click ? Let's say I'm on the second image and then I click on the button under the carousel and it changes all images to the other ones that I'll have in array of their IDs. It moves on to the first one as well.

I tried that and it doesn't work. @sayyam


    int[] redImages = {R.drawable.red1, R.drawable.red2};
    int[] blackImages = {R.drawable.black1, R.drawable.black2};

    carouselView = (CarouselView) findViewById(R.id.carouselView);
    carouselView.setPageCount(2);

    carouselView.setImageListener(blackListener);

    black = findViewById(R.id.blackColor);
    red = findViewById(R.id.redColor);

    black.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            carouselView.setImageListener(blackListener);
        }
    });

    red.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            carouselView.setImageListener(redListener);
        }
    });

    ImageListener blackListener = new ImageListener() {
        @Override
        public void setImageForPosition(int position, ImageView imageView) {
            imageView.setImageResource(blackImages[position]);
        }
    };

    ImageListener redListener = new ImageListener() {
        @Override
        public void setImageForPosition(int position, ImageView imageView) {
            imageView.setImageResource(redImages[position]);
        }
    };

Ok I found a solution. Each time you set Image listener, you have to do a setPageCount() as well. That would be

  red.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            carouselView.setImageListener(redListener);
            carouselView.setPageCount(numberOfImages);
        }
    });