sayyam / carouselview

A simple library to add carousel view in android app.
Apache License 2.0
1.15k stars 261 forks source link

getCurrentItem #19

Closed rawlen closed 8 years ago

rawlen commented 8 years ago

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!

sayyam commented 8 years ago

What do you want to achieve ?

rawlen commented 8 years ago

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.

sayyam commented 8 years ago

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;
        }
    };
rawlen commented 8 years ago

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