前言:TitlePageIndicator这个就是效果比较好。

    一:定义布局文件simple_titles:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <com.viewpagerindicator.TitlePageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip" /> <android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" /> </LinearLayout>

    二:代码中使用:

        setContentView(R.layout.simple_titles);

        mAdapter = new TestFragmentAdapter(getSupportFragmentManager());

        mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter); mIndicator = (TitlePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);

      其中的mAdapter在定义的时候需要实现IconPagerAdapter中的getPageTitle方法

  protected static final String[] CONTENT = new String[] { "This", "Is", "A", "Test", };
/**
* 定义tittle标题
*/
@Override
public CharSequence getPageTitle(int position) {
return TestFragmentAdapter.CONTENT[position % CONTENT.length];
}

    三:可修改的属性:

    <declare-styleable name="TitlePageIndicator">

        <!-- 距离左侧和右侧的距离 -->
<attr name="clipPadding" format="dimension" />
<!-- 底边线和底边指示的颜色 -->
<attr name="footerColor" format="color" />
<!-- 底边线的高度 -->
<attr name="footerLineHeight" format="dimension" />
<!-- 指示样式选择,尖角还条形 -->
<attr name="footerIndicatorStyle">
<enum name="none" value="0" />
<enum name="triangle" value="1" />
<enum name="underline" value="2" />
</attr>
<!-- 指示的高度 -->
<attr name="footerIndicatorHeight" format="dimension" />
<!-- 效果就是指示变宽了 -->
<attr name="footerIndicatorUnderlinePadding" format="dimension" />
<!-- 文字tittle和底边指示的距离 -->
<attr name="footerPadding" format="dimension" />
<!-- 指示的位置,tittle的上面,还是tittle的下面 -->
<attr name="linePosition">
<enum name="bottom" value="0" />
<enum name="top" value="1" />
</attr>
<!-- 被选择tittle的颜色 -->
<attr name="selectedColor" />
<!-- 被选择的tittle显示是否加粗 -->
<attr name="selectedBold" format="boolean" />
<!-- 未被选择的tittle的颜色 -->
<attr name="android:textColor" />
<!-- 文字的大小 -->
<attr name="android:textSize" />
<!-- 下一个item距离上一个item多远时,上一个item开始移动消失 -->
<attr name="titlePadding" format="dimension" />
<!-- 指示和上边view的距离 -->
<attr name="topPadding" format="dimension" />
<!-- 整体的背景色 -->
<attr name="android:background" />
</declare-styleable>

    四:使用自定义属性

      1.布局中使用:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <com.viewpagerindicator.TitlePageIndicator
android:id="@+id/indicator"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#18FF0000"
android:padding="10dip"
android:textColor="#AA000000"
app:footerColor="#FFAA2222"
app:footerIndicatorHeight="3dp"
app:footerIndicatorStyle="underline"
app:footerLineHeight="1dp"
app:selectedBold="true"
app:selectedColor="#FF000000" /> <android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>

      3.代码中使用:

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_titles); mAdapter = new TestFragmentAdapter(getSupportFragmentManager()); mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter); TitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.indicator);
mIndicator = indicator;
indicator.setViewPager(mPager); final float density = getResources().getDisplayMetrics().density;
indicator.setBackgroundColor(0x18FF0000);
indicator.setFooterColor(0xFFAA2222);
indicator.setFooterLineHeight(1 * density); //1dp
indicator.setFooterIndicatorHeight(3 * density); //3dp
indicator.setFooterIndicatorStyle(IndicatorStyle.Underline);
indicator.setTextColor(0xAA000000);
indicator.setSelectedColor(0xFF000000);
indicator.setSelectedBold(true);
}

      3.theme使用:

        设置主题其中StyledIndicators可以自己随便定义,然后在配置文件中使用即可:

    <style name="StyledIndicators" parent="@android:style/Theme.Light">
<item name="vpiTitlePageIndicatorStyle">@style/CustomTitlePageIndicator</item>
</style> <style name="CustomTitlePageIndicator">
<item name="android:background">#18FF0000</item>
<item name="footerColor">#FFFF7F24</item>
<item name="footerLineHeight">1dp</item>
<item name="footerIndicatorHeight">2dp</item>
<item name="linePosition">top</item>
<item name="titlePadding">30dp</item>
<item name="footerIndicatorStyle">underline</item>
<item name="android:textColor">#AAFF7F24</item>
<item name="selectedColor">#FFFF7F24</item>
<item name="selectedBold">true</item>
</style>

        使用主题:

        <activity
android:name=".SampleTitlesStyledTheme"
android:label="Titles/Styled (via theme)"
android:theme="@style/StyledIndicators" >
<intent-filter>
<action android:name="android.intent.action.MAIN" /> <category android:name="com.jakewharton.android.viewpagerindicator.sample.SAMPLE" />
</intent-filter>
</activity>

    五:在使用的时候,可以点击当前被选择的tittle,触发点击事件,只需要实现OnCenterItemClickListener即可:

public class SampleTitlesCenterClickListener extends BaseSampleActivity implements OnCenterItemClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_titles); mAdapter = new TestFragmentAdapter(getSupportFragmentManager()); mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter); TitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.indicator);
indicator.setViewPager(mPager);
indicator.setFooterIndicatorStyle(IndicatorStyle.Underline);
indicator.setOnCenterItemClickListener(this);
mIndicator = indicator;
} @Override
public void onCenterItemClick(int position) {
Toast.makeText(this, "You clicked the center title!", Toast.LENGTH_SHORT).show();
}
}

      也可以设置滑动监听:

 mIndicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageSelected(int position) {
Toast.makeText(SampleTitlesWithListener.this, "Changed to page " + position, Toast.LENGTH_SHORT).show();
} @Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
} @Override
public void onPageScrollStateChanged(int state) {
}
});

源码以及Demo下载地址:http://download.csdn.net/detail/as294985925/6796117

Android-ViewPagerIndicator框架使用——TitlePageIndicator的更多相关文章

  1. Android开源框架ViewPagerIndicator的基本使用

    转载本博客请注明出处:点击打开链接    http://blog.csdn.net/qq_32059827/article/details/52495647 很多新闻资讯类的app都有一些共性,那就是 ...

  2. Android百大框架排行榜

    Android百大框架排行榜 15类Android通用流行框架 - 流风,飘然的风 - 博客园https://www.cnblogs.com/zdz8207/p/android-opensource- ...

  3. 2017年Android百大框架排行榜

    框架:提供一定能力的小段程序 >随意转载,标注作者"金诚"即可 >本文已授权微信公众号:鸿洋(hongyangAndroid)原创首发. >本文已经开源到Gith ...

  4. android 优秀框架整理

    程序员界有个神奇的网站,那就是github,这个网站集合了一大批优秀的开源框架,极大地节省了开发者开发的时间,在这里我进行了一下整理,这样可以使我们在使用到时快速的查找到,希望对大家有所帮助! 1. ...

  5. 2017年Android百大框架排行榜(转)

    一.榜单介绍 排行榜包括四大类: 单一框架:仅提供路由.网络层.UI层.通信层或其他单一功能的框架 混合开发框架:提供开发hybrid app.h5与webview结合能力.web app能力的框架 ...

  6. Android开源框架ViewPageIndicator和ViewPager实现Tab导航

    前言: 关于使用ViewPageIndicator和ViewPager实现Tab导航,在开发社区里已经有一堆的博客对其进行了介绍,假设我还在这里写怎样去实现.那简直就是老生常谈,毫无新奇感,并且.我也 ...

  7. Android 开源框架Universal-Image-Loader学习

    Android 开源框架Universal-Image-Loader完全解析(一)--- 基本介绍及使用 Android 开源框架Universal-Image-Loader完全解析(二)--- 图片 ...

  8. Android 数据库框架OrmLite的使用(一)

    在这里记录下最基本的用法,官网上可了解相关的介绍. 1.下载OrmLite jar 在下载android的:ormlite-android-4.48.jar和ormlite-core-4.48.jar ...

  9. Android 开源框架Universal-Image-Loader完全解析(三)---源代码解读

    转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/39057201),请尊重他人的辛勤劳动成果,谢谢! 本篇文章 ...

随机推荐

  1. telnet命令

    详细资料 telnet命令使用方法详解-telnet命令怎么用-win7没有telent怎么办 2017年07月26日 15:37:36 阅读数:1010 什么是Telnet? 对于Telnet的认识 ...

  2. 【Sofa】Sofa比赛成绩记录

    最高得到过第4名,然后后面跌倒了第7名,现在追到了第6名.虽然名次还不是最高,但是很开心,今天能在一道一直困扰的题目上有突破,就是那个自行车预测的题目,开始过拟合了.后面进行了一些处理,效果很明显.继 ...

  3. saltstack之salt event事件用法

    event是一个本地的ZeroMQ PUB Interface,event是一个开放的系统,用于发送信息通知salt或其他的操作系统.每个event都有一个标签.事件标签允许快速制定过滤事件.除了标签 ...

  4. linux设备驱动:中断的实现

    一.什么是中断 中断分两种: 1)中断,又叫外部中断或异步中断,它的产生是由于外设向处理器发出中断请求.其中外部中断也有两种,这是由配置寄存器设定的:普通中断请求(IRQ)和快速中断请求(FIQ).一 ...

  5. GraphicsLab Project之辉光(Glare,Glow)效果 【转】

    作者:i_dovelemon 日期:2016 / 07 / 02 来源:CSDN 主题:Render to Texture, Post process, Glare, Glow, Multi-pass ...

  6. 项目笔记:导出Excel功能

    1.前台这块: var ids=""; $.post("${basePath}/assets/unRegDeviceAction_getDeviceIds.do" ...

  7. 动态创建的文本框想要加上jQuery的datepicker功能变成日期选择控件该怎么办?

    通常页面输入控件想得到日期选择功能,借助jQuery是这样实现的: 1.载入css和js <script src="jqueryui/jquery-ui.js" type=& ...

  8. 从webstorm转vscode,来一个vscode的教程和心得总结

    背景 在公司跑代码,每天卡的吐血,感觉生命都被浪费了. 再在摧残了一段时间,天天想摔电脑以后,被同事安利vscode, 那就开始搞起来 安装 这个我真的不用说了吧 插件 快捷键 shift + alt ...

  9. zoj How Many Sets I(组合计数)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemId=4535 一个集合s有n个元素,求满足这种集合序列{s1,s2....sk}使S ...

  10. 【Datastage】NULL VALUE TO A NOT NULL VALUE

    使用ds是报错如上图: 造成这个问题的原因是: 在写SQL时候没有给字段别名与DS中对应一致 例如有下表: CREATE TABLE DataInfo( ID_1 ), ID_2 ) ) CREATE ...