I have a ViewPager and a HorizontalScrollView. I would to move HorizontalScrollView in the same position of ViewPager, programmatically, so I would they behave in this way: when I scroll ViewPager to page 1, HorizontalScrollView moves automatically to position 1. I've tried using ViewPager.OnPageChangeListener
and smoothScrollTo (int x, int y)
, but I don't understand how to use the last method: in particularly, I don't understand what does it mean X and Y in this case. Here the code:
public class MyClass extends AppCompatActivity implements ViewPager.OnPageChangeListener {
ViewPager viewPager;
HorizontalScrollView hscroll;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
viewPager = (ViewPager) findViewById(R.id.pager);
hscroll = (HorizontalScrollView) findViewById(R.id.horizontalScrollView);
viewPager.setOnPageChangeListener(this);
...
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
hscroll.smoothScrollTo(position-1, position);
Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
}
@Override
public void onPageScrollStateChanged(int state) {
}
Any idea? Every help would be greatly appreciated, thanks in advance :)