ViewPager setCurrentItem(pageId, true) does NOT smoothscroll

前端 未结 13 1149
予麋鹿
予麋鹿 2020-12-02 13:01

I am compiling on SDK 4.03, Samsung Infuse Android 2.2, Support Library for Android 4, and using ViewPager in my app, actual swipe works fine, but when I do



        
13条回答
  •  攒了一身酷
    2020-12-02 13:43

    I had the same problem, but today I've found a simple solution. Maybe it will help you. First, lets suppose we have a ViewPager that filled the whole screen. To switch between pages I've created my own custom View with tabs and put it over the ViewPager. Clicking a tab should scroll smoothly to the appropriate page in ViewPager with setCurrentItem(item, true) - but it scrolls instantly, with no smooth! Then I tried to add a simple button (not custom) over the ViewPager + callback:

    @Override
    public void onClick(View v) {
       viewPager.setCurrentItem(item, true);
    }
    

    After that the smooth scroll stared working. So, the solution was very simple: inside the Button class the internal boolean onTouch(...) listener returns true, so it always CONSUMES touch events! Then the smooth scroll started working for my custom tabs view when I substitued "return false" with "return true" in the internal boolean onTouch(...) listener.

    I hope my success story can help you.

提交回复
热议问题