sayyam/carouselview

getCurrentItem

rawlen opened this issue · 4 comments

setCurrentItem has been very useful thus far, but is a "getter" possible as well, if a solution doesn't already exist? I would like to know which image the user sees on the carousel for a variety of purposes, such as making a button appear on the screen separately from the carousel itself when the user views a particular item.

Thank you!

What do you want to achieve ?

I would like to change elements of the page based on which item in the carousel is currently showing. I can think of a few ways to achieve this, such as syncing two carousels or adding/removing elements to the page based on the current carousel item.

An example: I have text superimposed over the image on the carousel. This text should not be part of the image, because I use the image elsewhere and don't want text on it in those locations. When the carousel scrolls to the next image, I want the text to change.

What I'm describing would use something more like a scroll listener than getCurrentItem, I realized. Sorry for the ambiguity.

You can use setViewListener instead of setImageListener.

        // set ViewListener for custom view
        customCarouselView.setViewListener(viewListener);

    ViewListener viewListener = new ViewListener() {

        @Override
        public View setViewForPosition(int position) {
            View customView = getLayoutInflater().inflate(R.layout.view_custom, null);

            //set text of textview like this. you can also hide it or whatever you intend to do.
            TextView myDynamicTextView = (TextView) customView.findViewById(R.id.myDynamicTextView);

            return customView;
        }
    };

Perfect! Thank you very much! I was able to use this in other places and greatly clean up my code.