GoodieBag / CarouselPicker

A Carousel picker library for android which supports both text and icons . :sparkles:
MIT License
708 stars 101 forks source link

Item click incorrect position #21

Open dhiren8391 opened 5 years ago

dhiren8391 commented 5 years ago

When I implement click listener on each item, it gives incorrect position. Any one has solution?

CWrecker commented 5 years ago

ya so, when you add the following (the onlcikc listener)

`carouselPicker.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            //position of the selected item
            switch (position) {
                    case 0:
                        //do smth
                        Toast.makeText(MainActivity.this, "first item selected", Toast.LENGTH_SHORT).show();
                    break;

                    case 1:
                        // do smth else
                        Toast.makeText(MainActivity.this, "second item selected", Toast.LENGTH_SHORT).show();
                    break;
                    //case 3, etc... 

                }

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });`

Note that it starts at case 0 not case 1

dhiren8391 commented 5 years ago

@CWrecker, I am not looking for onPageChangeListener. I want to implement click on each carousel items.

CWrecker commented 5 years ago

yes, that is what this does. the item is selected (Onclick) when it is in the center of the carousel. Give it a try. Put a toast message in one of the cases between Case 0: and Break;

dhiren8391 commented 5 years ago

You have some misunderstanding. I want to update viewpager position on item click. And you are saying to implement page change listener.

CWrecker commented 5 years ago

so you want to make a a different function (a method) run when one of the items is clicked on?

dhiren8391 commented 5 years ago

Yes. View pager position should be change on item click.

CWrecker commented 5 years ago

ok, so when i did it, i could not find a way to directly add Onclick to each button. So used the code above to change a variable, say x, and then based on that variable i used another button (this time with a direct onclick function) to run the method/code i wanted. Let me know if this helps

CWrecker commented 5 years ago

take a look at this StackExchange post if you need to.

Stack Exchange