Mobile Development 11 min read

Introduction to PickerView Library for Android

This article introduces a PickerView library for Android, comparing it with existing libraries and providing examples of its usage in mobile applications.

Beike Product & Technology
Beike Product & Technology
Beike Product & Technology
Introduction to PickerView Library for Android

付朝阳,新房移动端,15年6月加入链家。先后负责新房用户端、经纪人端Android app的开发。

如果您点进了此文,恭喜您可能遇到了史上最好用的PickerView库!

本文会通过和目前github上最受欢迎的PickerView库做详细的对比,通过对比你可以清晰的感受它的好用和强大。基本可以解决链家现有app的所有pickerview相关需求。 GitHub地址:https://github.com/jaaksi/pickerview 背景

TimePicker示意图 :

MixedTimePicker示意图:

OptionPicker示意图:

介 绍

Demo App下载连接

README

PickerView

README

TimePicker示意图 :

MixedTimePicker示意图

OptionPicker示意图:

背景

通过组装PickerView实现常用的Picker选择器。下面列举提供的3中常用的Picker。

支持扩展自定义Picker。

API

对比github上最受欢迎的同类库 Android-PickerView 本库将TopBar等通用相关逻辑封装在基类中,并提供代码中创建PickerView方法,不需要再依赖xml。用户自定义Picker时,继承BasePicker,只需要处理自己的逻辑即可,简单便捷。 而对Android-PickerView来说,实现自定义Picker,依然需要处理TopBar等逻辑。造成大量重复代码。

TopBar

TopBar:TopBar通过抽象接口ITopBar来管理,实现Picker与TopBar的解耦。

提供默认实现DefaultTopBar。可实现接口定制自己的TopBar。

Interceptor

拦截器:用于在pickerview创建时拦截,设置pickerview的属性。

这一点对比 Android-PickerView, 每个Picker都需要声明对PickerView的设置方法,与PickerView严重耦合。需要开发者copy大量重复代码,且无法区分每一个PickerView设置不同的属性。

TimePicker

常用的时间选择器,支持 年、月、日、时、分

对比 Android-PickerView TimePickerView:

完美支持时间区间设置以及选中联动

支持Format,如显示今年,明年

API

Formatter

TimePicker Formatter:用于根据type和num格式化时间文案

内部提供默认的 Formatter实现。用户可以设置自定义Formatter或继承DefaultFormatter进行扩展。

Simple Example

MixedTimePicker

常用的聚合时间选择器。日期(年、月、日)聚合,时间(小时、分钟)聚合。

支持自定义日期格式,时间格式

API

Formatter

MixedTimePicker Formatter:用于自定义日期和时间格式。内部提供默认的 Formatter实现。

OptionPicker

支持设置层级

构造数据源及其简单,只需要实现OptionDataSet接口

支持通过对应选中的values设置选中项。内部处理选中项逻辑,避免用户 记录下标且麻烦的遍历处理

对比 Android-PickerView的 OptionsPickerView

本库中的OptionPicker

如上面对比表格中所列举的,无论是层级,构造数据源和设置数据源,还是设置选中的选项,本库的API都十分简单,方便。

API

需要注意的是:本库中的OptionPicker只用于联动的,不支持多级别且不联动。 基本没有这种需求,如果大家有这种需求,我会在后续迭代中支持。

Others

奇葩设计:部分default属性声明为static而非final

全局设置default属性

奇葩也好,亮点也罢。作为一个UI控件,不同的app,不同的UI,不同的产品自然会有不同的样式。 考虑到在一个app中我们会用到很多Picker,而我们又需要定制自己的UI的样式,如果通过动态方法设置样式就太麻烦了。 故做此设计。你可以通过配置这些static变量来快速定制一个满足自己app样式需求的Picker。 当然你也可以通过封装方法来处理PickerView,Picker,装饰器等样式,但这样一样十分麻烦。我相信你自己都会烦。

静态默认值

所有的这些静态属性值都以 sDefault 开头

• BasePickerView

• PickerView

• BasePicker

• DefaultCenterDecoration

建议初始化这些属性值放到Application中完成,避免app发生crash而导致失效。

Simple Example

Change Log

v1.0.0(2018-03-03)

- release v1.0.0

Gradle

Thanks

• ScrollPickerView

• Android-PickerView

Github地址:

https://github.com/jaaksi/pickerview

作者:付朝阳

监审:蔡白银

编辑:钟   艳

网址:tech.lianjia.com

链家产品技术团队

产品技术先行

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Mobile DevelopmentAndroidCode ExampleLibraryUI ComponentPickerViewTechnical Comparison
Beike Product & Technology
Written by

Beike Product & Technology

As Beike's official product and technology account, we are committed to building a platform for sharing Beike's product and technology insights, targeting internet/O2O developers and product professionals. We share high-quality original articles, tech salon events, and recruitment information weekly. Welcome to follow us.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.