Android页面指示器,GitHub - mzdongliang/pager-indicator: 这是 Android 上的一个 ViewPager 页面指示器组件,用于标识当前显示的页面...

90d65b241801633e8841d9e51266d591.png PagerIndicator

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f506c6174666f726d2d416e64726f69642d627269676874677265656e2e737667

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4150492d392532422d6f72616e67652e737667

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f416e64726f6964253230417273656e616c2d5061676572496e64696361746f722d677265656e2e7376673f7374796c653d74727565

68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f70616e70662f70616765722d696e64696361746f722e737667

这是 Android 上的一个 ViewPager 页面指示器组件,用于标识当前显示的页面

ba66b6a78eeda333ee9e6cb7353bc7dc.gif

特性

当所有的 Tab 在横向上无法充满整个屏幕的时候,将会根据 allowWidthFull 参数决定是否调整所有的 Item 来充满。调整的规则是平均分配每个 Tab 的宽度,如果某个 Tab 的最小宽度大于平均宽度,那么其宽度保持不变

当所有的 Tab 在横向上能够充满整个屏幕的时候,将会从左到右依次排列

用法简单,支持在布局中直接包含 View 或者在代码中直接调用 PagerIndicator.addTab() 方法添加

Tab View 支持设置外边距

支持通过 disableTensileSlidingBlock 属性设置是否拉伸滑块图片

开始使用

1. 导入 PagerIndicator

在 app 的 build.gradle 文件的 dependencies 节点中加入依赖

dependencies {

implementation 'me.panpf:pager-indicator:$lastVersionName'

}

请自行替换 $lastVersionName 为最新的版本:68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f72656c656173652f70616e70662f70616765722d696e64696361746f722e737667 (不要v)

2. 在布局中使用

xmlns:app="http://schemas.android.com/apk/res/你的包名"

android:id="@+id/slidingTabStrip_2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@drawable/sliding_tab_strip_background"

app:pi_allowWidthFull="true"

app:pi_slidingBlock="@drawable/image_sliding_block">

android:layout_width="wrap_content"

android:layout_height="wrap_content">

效果图如下:

102fa9dfb2d642c9a85544f0a6f73798.png

你还可以在代码中通过 PagerIndicator.setTabViewFactory() 方法添加Tab

支持的布局属性:

pi_slidingBlock:指定滑块图片,对应的方法是 PagerIndicator.setSlidingBlockDrawable(Drawable)

pi_allowWidthFull:当所有 Tab 无法充满 PagerIndicator 时是否自动调整所有 Tab 的宽度来达到充满的目的,对应的方式是 PagerIndicator.setAllowWidthFull(boolean)

pi_disableViewPager:禁用 ViewPager,禁用后不会有初始选中状态效果,也不会绘制滑块,你调用 setViewPager() 方法也不会起作用

pi_disableTensileSlidingBlock:禁止拉伸滑块图片

3. 绑定 ViewPager

ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);

viewPager.setAdapter(...);

PagerIndicator pagerIndicator = (PagerIndicator) findViewById(R.id.slidingTabStrip);

pagerIndicator.setViewPager(viewPager);

注意:

在调用 setViewPager(ViewPager) 方法之前要先设置 ViewPager 的 Adapter

你还可以直接调用 ViewPager.setCurrentItem(int) 方法来改变默认 Tab,无论是在调用 setViewPager(ViewPager) 方法之前还是之后都可以

更多内容请参见 sample 源码

License

Copyright (C) 2017 Peng fei Pan

Licensed under the Apache License, Version 2.0 (the "License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an "AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值