TabLayout selected tab gravity

后端 未结 2 644
执念已碎
执念已碎 2021-01-11 12:19

TabLayout is set up with ViewPager, has a lot of tabs, MODE_SCROLLABLE and keyline app:tabContentStart=\"72dp\".

相关标签:
2条回答
  • 2021-01-11 12:25

    Unfortunately the method calculateScrollXForTab() of TabLayout is private and then not replaceable by subclasses. Either way you can copy the source TabLayout in your project, then possibly extending it with your class, and change the method calculateScrollXForTab() like this:

    private int calculateScrollXForTab(int position, float positionOffset) {
       if (mMode == MODE_SCROLLABLE) {
          View final selectedChild = mTabStrip.getChildAt(position);
          // LoG.i ("scrollTo" String.valueOf ((int) selectedChild.getLeft()));
          return (int) selectedChild.getLeft();
       }
       return 0;
    }
    

    This returns the value of the limit on the left of the tab selected, then scrolling is forced to that value. The extreme right tabs will remain fixed in position if selected because the scroll clamps the scrolling to the bounds of the child.

    I tried it and it works, although I had to solve two problems with one CompatTextView not found in android.support.v7.internal.widget and method setupWithViewPager() that does not exist in the source available to me (I think a matter of version)

    0 讨论(0)
  • 2021-01-11 12:28

    If I understood your question well, you can subtract the first tab position from the selected tab position. Then, you just set the different as a position for this selected tab.

    0 讨论(0)
提交回复
热议问题