Android-ViewPagerIndicator自定义tab数量
来源:互联网 发布:混沌战域美人进阶数据 编辑:程序博客网 时间:2024/05/16 11:34
在上篇Android-ViewPagerIndicator简单集成的基础上,我们想开发类似网易新闻tab标签的功能,可以添加删除tab。
查看TabPageIndicator的源码我们可以看到有一个notifyDataSetChanged()方法。
public void notifyDataSetChanged() { mTabLayout.removeAllViews(); PagerAdapter adapter = mViewPager.getAdapter(); IconPagerAdapter iconAdapter = null; if (adapter instanceof IconPagerAdapter) { iconAdapter = (IconPagerAdapter)adapter; } final int count = adapter.getCount(); for (int i = 0; i < count; i++) { CharSequence title = adapter.getPageTitle(i); if (title == null) { title = EMPTY_TITLE; } int iconResId = 0; if (iconAdapter != null) { iconResId = iconAdapter.getIconResId(i); } addTab(i, title, iconResId); } if (mSelectedTabIndex > count) { mSelectedTabIndex = count - 1; } setCurrentItem(mSelectedTabIndex); requestLayout(); }
我们理解一下:
mTabLayout.removeAllViews();
mTabLayout是什么?
mTabLayout是IcsLinearLayout类型的,IcsLinearLayout是什么东西?
察看源码就很容易看出来,IcsLinearLayout其实是放tab的容器,继承LinearLayout,tab都放在这里面展示。里面封装了tab的样式:字体大小,颜色,间隔,分割线的样式等等。不是很难就不介绍了。
notifyDataSetChanged的第一行就是mTabLayout.removeAllViews();清空所有的tab。
继续看代码,PagerAdapter adapter = mViewPager.getAdapter();获取了mViewPager的适配器,这个适配器我们一般都是关联着一个fragment的列表。
继续
for (int i = 0; i < count; i++) {
CharSequence title =adapter.getPageTitle(i);
if (title ==null) {
title = EMPTY_TITLE;
}
int iconResId = 0;
if (iconAdapter !=null) {
iconResId =iconAdapter.getIconResId(i);
}
addTab(i, title, iconResId);
}
会将适配器中的每个item都拿出来构建一个tab加入到mTabLayout中。
最后刷新页面。
主体思路:清空所有的tab,重新加载tab,到了这里我们就知道怎么实现类似网易新闻tab的功能了吧 。
- Android-ViewPagerIndicator自定义tab数量
- 自定义tab指示符--ViewPagerIndicator
- Android之自定义ViewPagerIndicator
- Android Tab实现之Viewpager+ViewpagerIndicator
- Android ViewPager+ViewPagerIndicator实现Tab主界面
- 安卓新闻客户端(三) 自定义ViewPagerIndicator tab指示符
- 自定义ViewPagerIndicator
- 自定义ViewPagerIndicator
- 自定义ViewPagerIndicator
- 自定义ViewPagerIndicator
- 自定义ViewPagerIndicator
- Android 使用Fragment,ViewPagerIndicator 仿网易新闻客户端Tab标签
- android自定义View之(五)------ViewPagerIndicator
- Android 自定义控件ViewPager 指示器 ViewPagerIndicator
- <Android 基础(三十七)> 自定义ViewPagerIndicator
- Android: 自定义Tab样式
- Android: 自定义Tab样式
- Android: 自定义Tab样式
- 二十大数据可视化工具点评
- Unity3D 多渠道打包资料
- stringstream的清空
- jquery ajax的data参数
- android多用户
- Android-ViewPagerIndicator自定义tab数量
- golang 生成随机数,时间种子改进型
- Substitution failure is not an error
- HDU 4035 Maze 树形概率DP 2011年成都网络赛E题
- iOS代码行数统计
- 那么你呢?
- jenkins+ant+svn持续集成环境搭建
- Android获取图片资源的4种方式
- WARNING: inbound connection timed out (ORA-3136)