CarouselPicker
CarouselPicker copied to clipboard
Item click incorrect position
When I implement click listener on each item, it gives incorrect position. Any one has solution?
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
@CWrecker, I am not looking for onPageChangeListener. I want to implement click on each carousel items.
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;
You have some misunderstanding. I want to update viewpager position on item click. And you are saying to implement page change listener.
so you want to make a a different function (a method) run when one of the items is clicked on?
Yes. View pager position should be change on item click.
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