TabLayout versus pagerTabStrip for convenience and

2019-07-07 16:28发布

问题:

When using a ViewPager, is there any advantage to using TabLayout instead of PagerTabStrip or vice versa? Or are they equivalent? Is one more convenient than the other? Does one offer greater functionality? Again all questions are asked, and thus all answers are expected, in the context of ViewPager. Thanks.

回答1:

TabLayout is a material concept that replaced the deprecated ActionBar tabs in Android 5.0. It extends HorizontalScrollView, so you can keep adding tabs horizontally which can include text, icons, or custom views and scroll through them linearly without paging.

TabLayout provides the setupWithViewPager(ViewPager viewPager) method to attach to a ViewPager instead of being part of the ViewPager like the PagerTabStrip.

A PagerTabStrip is more of an indictor for the current page of a ViewPager, and "it is intended to be used as a child view of a ViewPager widget". The scrolling for it does not act like the TabLayout since each tab is part of the page instead of individually horizontally scrollable.