将ViewPager与导航选项卡关联(Associating ViewPager with navigation tabs)

编程入门 行业动态 更新时间:2024-10-21 15:28:38
将ViewPager与导航选项卡关联(Associating ViewPager with navigation tabs)

有没有办法将ViewPager中的每个片段与某个导航选项卡相关联? 因此,当我们向右/向左翻转时,右侧选项卡将突出显示。

谢谢。

Is there any way to associate each fragment in the ViewPager with a certain navigation tab? So when we flip right/left ,the right tab will be highlighted.

Thank you.

最满意答案

使用Material Design支持库中的TabLayout。

请参阅介绍该库的博客文章中的“选项卡”段落:

但是,如果您使用ViewPager在选项卡之间进行水平分页,则可以直接从PagerAdapter的getPageTitle()创建选项卡,然后使用setupWithViewPager()将两者连接在一起。 这可确保选项卡选择事件更新ViewPager,页面更改将更新选定的选项卡。

Use TabLayout from the Material Design support library.

See the "Tabs" paragraph in the blog post that introduces the library:

However, if you are using a ViewPager for horizontal paging between tabs, you can create tabs directly from your PagerAdapter’s getPageTitle() and then connect the two together using setupWithViewPager(). This ensures that tab selection events update the ViewPager and page changes update the selected tab.

更多推荐

本文发布于:2023-08-07 09:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1463775.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:选项卡   ViewPager   Associating   tabs   navigation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!