Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Item click incorrect position #21

Open
dhiren8391 opened this issue Feb 12, 2019 · 8 comments
Open

Item click incorrect position #21

dhiren8391 opened this issue Feb 12, 2019 · 8 comments

Comments

@dhiren8391
Copy link

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

@CWrecker
Copy link

CWrecker commented Feb 12, 2019

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
Copy link
Author

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

@CWrecker
Copy link

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
Copy link
Author

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

@CWrecker
Copy link

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

@dhiren8391
Copy link
Author

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

@CWrecker
Copy link

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
Copy link

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

Stack Exchange

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants