├── .gitignore
├── BannerExample.iml
├── LICENSE
├── README.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── test
│ │ └── banner
│ │ ├── MainActivity.java
│ │ ├── adapter
│ │ ├── ImageAdapter.java
│ │ ├── ImageNetAdapter.java
│ │ ├── ImageTitleAdapter.java
│ │ ├── ImageTitleNumAdapter.java
│ │ ├── MultipleTypesAdapter.java
│ │ ├── MyRecyclerViewAdapter.java
│ │ └── TopLineAdapter.java
│ │ ├── bean
│ │ └── DataBean.java
│ │ ├── indicator
│ │ └── NumIndicator.java
│ │ ├── ui
│ │ ├── BannerFragment.java
│ │ ├── BannerListFragment.java
│ │ ├── BlankFragment.java
│ │ ├── ConstraintLayoutBannerActivity.java
│ │ ├── GalleryActivity.java
│ │ ├── RecyclerViewBannerActivity.java
│ │ ├── TVActivity.java
│ │ ├── TouTiaoActivity.java
│ │ ├── VideoActivity.java
│ │ └── Vp2FragmentRecyclerviewActivity.java
│ │ ├── util
│ │ ├── ParentRecyclerView.java
│ │ └── TabLayoutMediator.java
│ │ └── viewholder
│ │ ├── ImageHolder.java
│ │ ├── ImageTitleHolder.java
│ │ ├── TitleHolder.java
│ │ └── VideoHolder.java
│ └── res
│ ├── drawable-xhdpi
│ ├── home_hotpoint.png
│ ├── home_right_arrow.png
│ ├── image1.jpg
│ ├── image10.jpg
│ ├── image11.jpg
│ ├── image2.jpg
│ ├── image3.jpg
│ ├── image4.jpg
│ ├── image5.jpg
│ ├── image6.jpg
│ ├── image7.jpg
│ ├── image8.jpg
│ ├── image9.jpg
│ ├── indicator_normal.png
│ └── indicator_selected.png
│ ├── drawable
│ ├── background.xml
│ ├── default_selecter.xml
│ ├── green.xml
│ ├── loading.gif
│ ├── selected_radius.xml
│ ├── unselected_radius.xml
│ └── white.xml
│ ├── layout
│ ├── activity_constraint_layout_banner.xml
│ ├── activity_gallery.xml
│ ├── activity_main.xml
│ ├── activity_recyclerview_banner.xml
│ ├── activity_t_v.xml
│ ├── activity_tou_tiao.xml
│ ├── activity_video.xml
│ ├── activity_vp2_fragment_recyclerview.xml
│ ├── banner.xml
│ ├── banner_image.xml
│ ├── banner_image_title.xml
│ ├── banner_image_title_num.xml
│ ├── banner_title.xml
│ ├── banner_video.xml
│ ├── item.xml
│ ├── test.xml
│ └── top_line_item2.xml
│ ├── mipmap-hdpi
│ └── ic_launcher.png
│ ├── mipmap-mdpi
│ └── ic_launcher.png
│ ├── mipmap-xhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxxhdpi
│ └── ic_launcher.png
│ ├── values-w820dp
│ └── dimens.xml
│ └── values
│ ├── arrays.xml
│ ├── colors.xml
│ ├── dimens.xml
│ ├── strings.xml
│ └── styles.xml
├── banner
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── youth
│ │ └── banner
│ │ ├── Banner.java
│ │ ├── adapter
│ │ ├── BannerAdapter.java
│ │ └── BannerImageAdapter.java
│ │ ├── config
│ │ ├── BannerConfig.java
│ │ └── IndicatorConfig.java
│ │ ├── holder
│ │ ├── BannerImageHolder.java
│ │ └── IViewHolder.java
│ │ ├── indicator
│ │ ├── BaseIndicator.java
│ │ ├── CircleIndicator.java
│ │ ├── DrawableIndicator.java
│ │ ├── Indicator.java
│ │ ├── RectangleIndicator.java
│ │ └── RoundLinesIndicator.java
│ │ ├── itemdecoration
│ │ └── MarginDecoration.java
│ │ ├── listener
│ │ ├── OnBannerListener.java
│ │ └── OnPageChangeListener.java
│ │ ├── transformer
│ │ ├── AlphaPageTransformer.java
│ │ ├── BasePageTransformer.java
│ │ ├── DepthPageTransformer.java
│ │ ├── MZScaleInTransformer.java
│ │ ├── RotateDownPageTransformer.java
│ │ ├── RotateUpPageTransformer.java
│ │ ├── RotateYTransformer.java
│ │ ├── ScaleInTransformer.java
│ │ └── ZoomOutPageTransformer.java
│ │ └── util
│ │ ├── BannerLifecycleObserver.java
│ │ ├── BannerLifecycleObserverAdapter.java
│ │ ├── BannerUtils.java
│ │ ├── LogUtils.java
│ │ └── ScrollSpeedManger.java
│ └── res
│ └── values
│ ├── attr.xml
│ ├── ids.xml
│ └── strings.xml
├── build.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── images
├── DepthPageTransformer.gif
├── ZoomOutPageTransformer.gif
├── banner_example.gif
├── banner_example1.gif
├── banner_example2.gif
├── banner_example3.gif
├── mailme.png
├── pay.jpg
└── qq.png
├── settings.gradle
├── update_message.md
└── usekotlin
├── .gitignore
├── build.gradle
├── consumer-rules.pro
├── proguard-rules.pro
└── src
├── androidTest
└── java
│ └── com
│ └── spring
│ └── usekotlin
│ └── ExampleInstrumentedTest.kt
├── main
├── AndroidManifest.xml
├── java
│ └── com
│ │ └── spring
│ │ └── usekotlin
│ │ ├── ImageAdapter.kt
│ │ └── MainActivity.kt
└── res
│ ├── layout
│ └── activity_main.xml
│ ├── mipmap-hdpi
│ └── ic_launcher.png
│ ├── mipmap-mdpi
│ └── ic_launcher.png
│ ├── mipmap-xhdpi
│ └── ic_launcher.png
│ ├── mipmap-xxhdpi
│ └── ic_launcher.png
│ └── values
│ ├── colors.xml
│ ├── strings.xml
│ └── styles.xml
└── test
└── java
└── com
└── spring
└── usekotlin
└── ExampleUnitTest.kt
/.gitignore:
--------------------------------------------------------------------------------
1 | # Built application files
2 | *.apk
3 | *.ap_
4 |
5 | # Files for the Dalvik VM
6 | *.dex
7 |
8 | # Java class files
9 | *.class
10 |
11 | # Generated files
12 | bin/
13 | gen/
14 |
15 | # Gradle files
16 | .gradle/
17 | build/
18 |
19 | # Local configuration file (sdk path, etc)
20 | local.properties
21 |
22 | # Proguard folder generated by Eclipse
23 | proguard/
24 |
25 | # Log Files
26 | *.log
27 |
28 | # Android Studio Navigation editor temp files
29 | .navigation/
30 |
31 | # Android Studio captures folder
32 | captures/
33 | .idea
34 | /banner.iml
35 | /gradlew
36 | /gradlew.bat
37 | /gradle.properties
38 |
--------------------------------------------------------------------------------
/BannerExample.iml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "{}"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright {yyyy} {name of copyright owner}
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | ## Banner 2.0 全新升级
3 | > 只做一个可以自定义的轮播容器,不侵入UI ———— Banner 2.0
4 |
5 | ```
6 | > 各位老铁反馈的问题我都有看,不是不解决,有时真的很难复现,
7 | > 如果能在提交问题时,有条件提供个demo发到我邮箱,方便定位问题,毕竟大家的场景和使用方式都有差异,
8 | > 我自己也在使用,在多个千万级APP上一直稳定运行,如果真有严重问题肯定第一时间就修复了
9 | ```
10 |
11 | Banner 1.4.10(还想看老版本的可以点击这里)
12 |
13 | ### 阔别已久,从新回归
14 |
15 | * 首先我声明几点:
16 | * 这只是一个开源库,如果满意你可以使用、可以借鉴修改,希望对你们有所帮助。
17 | * 如果不满意请友好的提出,注明错误的详细信息或者修改建议,好的想法和自定义的东西亦可以直接提交,大家都能来一起完善。
18 | * 如果你觉得实在是没用,也请你做一个有自我修养的人。
19 |
20 | ### 主要改进功能介绍
21 | 最开始是想上传以前基于viewpager更新好的版本,但是看着viewpager2正式版已经出来了,就上新的吧,viewpager2确实比viewpager性能好很多。
22 |
23 | - [x] 使用了ViewPager2为基础控件 ViewPager2介绍
24 | - [x] 支持了androidx兼容包
25 | - [x] 方便了UI、Indicator自定义
26 | - [x] 支持画廊效果、魅族效果
27 | - [x] 兼容了水平和垂直轮播,也可以实现类似淘宝头条的效果
28 | - [x] 依赖包目前只需要导入了ViewPager2
29 |
30 |
31 |
32 | ### 效果图
33 | 更多效果运行demo查看
34 |
35 | 
36 |
37 | 
38 |
39 | 
40 |
41 | 
42 |
43 | ##### 内置了多种PageTransformer效果
44 |
45 | 
46 | 
47 |
48 | |内置的PageTransformer|
49 | |---|
50 | |AlphaPageTransformer|
51 | |DepthPageTransformer|
52 | |RotateDownPageTransformer|
53 | |RotateUpPageTransformer|
54 | |RotateYTransformer|
55 | |ScaleInTransformer|
56 | |ZoomOutPageTransformer|
57 | 也可以组合使用效果更佳
58 |
59 | ## 方法
60 | 更多方法以实际使用为准,下面不一定全部列出了
61 |
62 | |方法名|返回类型|描述|
63 | |---|---|---|
64 | |getAdapter()|extends BannerAdapter|获取你设置的BannerAdapter
65 | |getViewPager2()|ViewPager2|获取ViewPager2
66 | |getIndicator()|Indicator|获取你设置的指示器(没有设置直接获取会抛异常哦)
67 | |getIndicatorConfig()|IndicatorConfig|获取你设置的指示器配置信息(没有设置直接获取会抛异常哦)
68 | |getRealCount()|int|返回banner真实总数
69 | |setUserInputEnabled(boolean)|this|禁止手动滑动Banner;true 允许,false 禁止
70 | |setDatas(List)|this|重新设置banner数据
71 | |isAutoLoop(boolean)|this|是否允许自动轮播
72 | |setLoopTime(long)|this|设置轮播间隔时间(默认3000毫秒)
73 | |setScrollTime(long)|this|设置轮播滑动的时间(默认800毫秒)
74 | |start()|this|开始轮播(主要配合生命周期使用),或者你手动暂停再次启动
75 | |stop()|this|停止轮播(主要配合生命周期使用),或者你需要手动暂停
76 | |setAdapter(T extends BannerAdapter)|this|设置banner的适配器
77 | |setAdapter(T extends BannerAdapter,boolean)|this|设置banner的适配器,是否支持无限循环
78 | |setOrientation(@Orientation)|this|设置banner轮播方向(垂直or水平)
79 | |setOnBannerListener(this)|this|设置点击事件,下标是从0开始
80 | |addOnPageChangeListener(this)|this|添加viewpager2的滑动监听
81 | |setPageTransformer(PageTransformer)|this|设置viewpager的切换效果
82 | |addPageTransformer(PageTransformer)|this|添加viewpager的切换效果(可以设置多个)
83 | |setIndicator(Indicator)|this|设置banner轮播指示器(提供有base和接口,可以自定义)
84 | |setIndicator(Indicator,boolean)|this|设置指示器(传false代表不将指示器添加到banner上,配合布局文件,可以自我发挥)
85 | |setIndicatorSelectedColor(@ColorInt)|this|设置指示器选中颜色
86 | |setIndicatorSelectedColorRes(@ColorRes)|this|设置指示器选中颜色
87 | |setIndicatorNormalColor(@ColorInt)|this|设置指示器默认颜色
88 | |setIndicatorNormalColorRes(@ColorRes)|this|设置指示器默认颜色
89 | |setIndicatorGravity(@IndicatorConfig.Direction)|this|设置指示器位置(左,中,右)
90 | |setIndicatorSpace(int)|this|设置指示器之间的间距
91 | |setIndicatorMargins(IndicatorConfig.Margins)|this|设置指示器的Margins
92 | |setIndicatorWidth(int,int)|this|设置指示器选中和未选中的宽度,直接影响绘制指示器的大小
93 | |setIndicatorNormalWidth(int)|this|设置指示器未选中的宽度
94 | |setIndicatorSelectedWidth(int)|this|设置指示器选中的宽度
95 | |setIndicatorRadius(int)|this|设置指示器圆角,不要圆角可以设置为0
96 | |setIndicatorHeight(int)|this|设置指示器高度
97 | |setBannerRound(float)|this|设置banner圆角(还有一种setBannerRound2方法,需要5.0以上)
98 | |setBannerGalleryEffect(int,int,float)|this|画廊效果
99 | |setBannerGalleryMZ(int,float)|this|魅族效果
100 | |setStartPosition(int)|this|设置开始的位置 (需要在setAdapter或者setDatas之前调用才有效哦)
101 | |setIndicatorPageChange()|this|设置指示器改变监听 (一般是为了配合数据操作使用,看情况自己发挥)
102 | |setCurrentItem()|this|设置当前位置,和原生使用效果一样
103 | |addBannerLifecycleObserver()|this|给banner添加生命周期观察者,内部自动管理banner的生命周期
104 |
105 |
106 | ## Attributes属性
107 | >在banner布局文件中调用,如果你自定义了indicator请做好兼容处理。
108 | 下面的属性并不是每个指示器都用得到,所以使用时要注意!
109 |
110 | |Attributes|format|describe
111 | |---|---|---|
112 | |banner_loop_time|integer|轮播间隔时间,默认3000
113 | |banner_auto_loop|boolean|是否自动轮播,默认true
114 | |banner_infinite_loop|boolean|是否支持无限循环(即首尾直接过渡),默认true
115 | |banner_orientation|enum|轮播方向:horizontal(默认) or vertical
116 | |banner_radius|dimension|banner圆角半径,默认0(不绘制圆角)
117 | |banner_indicator_normal_width|dimension|指示器默认的宽度,默认5dp (对RoundLinesIndicator无效)
118 | |banner_indicator_selected_width|dimension|指示器选中的宽度,默认7dp
119 | |banner_indicator_normal_color|color|指示器默认颜色,默认0x88ffffff
120 | |banner_indicator_selected_color|color|指示器选中颜色,默认0x88000000
121 | |banner_indicator_space|dimension|指示器之间的间距,默认5dp (对RoundLinesIndicator无效)
122 | |banner_indicator_gravity|dimension|指示器位置,默认center
123 | |banner_indicator_margin|dimension|指示器的margin,默认5dp,不能和下面的同时使用
124 | |banner_indicator_marginLeft|dimension|指示器左边的margin
125 | |banner_indicator_marginTop|dimension|指示器上边的margin
126 | |banner_indicator_marginRight|dimension|指示器右边的margin
127 | |banner_indicator_marginBottom|dimension|指示器下边的margin
128 | |banner_indicator_height|dimension|指示器高度(对CircleIndicator无效)
129 | |banner_indicator_radius|dimension|指示器圆角(对CircleIndicator无效)
130 | |banner_round_top_left|boolean|设置要绘制的banner圆角方向(如果都不设置默认全部)
131 | |banner_round_top_right|boolean|设置要绘制的banner圆角方向(如果都不设置默认全部)
132 | |banner_round_bottom_left|boolean|设置要绘制的banner圆角方向(如果都不设置默认全部)
133 | |banner_round_bottom_right|boolean|设置要绘制的banner圆角方向(如果都不设置默认全部)
134 |
135 |
136 |
137 | ## 使用步骤
138 | >以下提供的是最简单的步骤,需要复杂的样式自己可以自定义
139 |
140 | #### Step 1.依赖banner
141 | Gradle
142 | ```groovy
143 |
144 | repositories {
145 | maven { url "https://s01.oss.sonatype.org/content/groups/public" }
146 | }
147 |
148 | dependencies{
149 | //2.1.0以前jcenter的依赖
150 | //implementation 'com.youth.banner:banner:2.1.0'
151 | //现在Maven Central
152 | implementation 'io.github.youth5201314:banner:2.2.3'
153 |
154 | }
155 | ```
156 |
157 | #### Step 2.添加权限到你的 AndroidManifest.xml
158 | ```xml
159 |
160 |
161 |
162 | ```
163 |
164 | #### Step 3.在布局文件中添加Banner,可以设置自定义属性
165 | !!!此步骤可以省略,可以直接在Activity或者Fragment中new Banner();
166 | ```xml
167 |
172 | ```
173 |
174 | #### Step 4.继承BannerAdapter,和RecyclerView的Adapter一样(如果你只是图片轮播也可以使用默认的)
175 | !!!此步骤可以省略,图片轮播提供有默认适配器,其他的没有提供是因为大家的可变性要求不确定,所以直接自定义的比较好。
176 | ```java
177 |
178 | /**
179 | * 自定义布局,下面是常见的图片样式,更多实现可以看demo,可以自己随意发挥
180 | */
181 | public class ImageAdapter extends BannerAdapter {
182 |
183 | public ImageAdapter(List mDatas) {
184 | //设置数据,也可以调用banner提供的方法,或者自己在adapter中实现
185 | super(mDatas);
186 | }
187 |
188 | //创建ViewHolder,可以用viewType这个字段来区分不同的ViewHolder
189 | @Override
190 | public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
191 | ImageView imageView = new ImageView(parent.getContext());
192 | //注意,必须设置为match_parent,这个是viewpager2强制要求的
193 | imageView.setLayoutParams(new ViewGroup.LayoutParams(
194 | ViewGroup.LayoutParams.MATCH_PARENT,
195 | ViewGroup.LayoutParams.MATCH_PARENT));
196 | imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
197 | return new BannerViewHolder(imageView);
198 | }
199 |
200 | @Override
201 | public void onBindView(BannerViewHolder holder, DataBean data, int position, int size) {
202 | holder.imageView.setImageResource(data.imageRes);
203 | }
204 |
205 | class BannerViewHolder extends RecyclerView.ViewHolder {
206 | ImageView imageView;
207 |
208 | public BannerViewHolder(@NonNull ImageView view) {
209 | super(view);
210 | this.imageView = view;
211 | }
212 | }
213 | }
214 |
215 | ```
216 |
217 | #### Step 5.Banner具体方法调用
218 |
219 | ```java
220 | public class BannerActivity extends AppCompatActivity {
221 | public void useBanner() {
222 | //--------------------------简单使用-------------------------------
223 | banner.addBannerLifecycleObserver(this)//添加生命周期观察者
224 | .setAdapter(new BannerExampleAdapter(DataBean.getTestData()))
225 | .setIndicator(new CircleIndicator(this));
226 |
227 | //—————————————————————————如果你想偷懒,而又只是图片轮播————————————————————————
228 | banner.setAdapter(new BannerImageAdapter(DataBean.getTestData3()) {
229 | @Override
230 | public void onBindView(BannerImageHolder holder, DataBean data, int position, int size) {
231 | //图片加载自己实现
232 | Glide.with(holder.itemView)
233 | .load(data.imageUrl)
234 | .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
235 | .into(holder.imageView);
236 | }
237 | })
238 | .addBannerLifecycleObserver(this)//添加生命周期观察者
239 | .setIndicator(new CircleIndicator(this));
240 | //更多使用方法仔细阅读文档,或者查看demo
241 | }
242 | }
243 | ```
244 |
245 | ## Banner使用中优化体验
246 | **如果你需要考虑更好的体验,可以看看下面的代码**
247 | #### Step 1.(可选)生命周期改变时
248 | ```java
249 | public class BannerActivity {
250 |
251 | //方法一:自己控制banner的生命周期
252 |
253 | @Override
254 | protected void onStart() {
255 | super.onStart();
256 | //开始轮播
257 | banner.start();
258 | }
259 |
260 | @Override
261 | protected void onStop() {
262 | super.onStop();
263 | //停止轮播
264 | banner.stop();
265 | }
266 |
267 | @Override
268 | protected void onDestroy() {
269 | super.onDestroy();
270 | //销毁
271 | banner.destroy();
272 | }
273 |
274 | //方法二:调用banner的addBannerLifecycleObserver()方法,让banner自己控制
275 |
276 | protected void onCreate(Bundle savedInstanceState) {
277 | //添加生命周期观察者
278 | banner.addBannerLifecycleObserver(this);
279 | }
280 | }
281 | ```
282 |
283 |
284 | ## 常见问题(收录被反复询问的问题)
285 |
286 | * 网络图片加载不出来?
287 |
288 | `banner本身不提供图片加载功能,首先确认banner本身使用是否正确,具体参考demo,
289 | 然后请检查你的图片加载框架或者网络请求框架,服务端也可能加了https安全认证,是看下是否报有证书相关错误`
290 |
291 | * 怎么实现视频轮播?
292 |
293 | `demo中有实现类似淘宝商品详情的效果,第一个放视频,后面的放的是图片,并且可以设置首尾不能滑动。
294 | 因为大家使用的播放器不一样业务环境也不同,具体情况自己把握,demo就是给一个思路哈!可以参考和修改`
295 |
296 | * 我想指定轮播开始的位置?
297 |
298 | `现在提供了setStartPosition()方法,在sheAdapter和setDatas直接调用一次就行了,当然setAdapter后通过setCurrentItem设置也行`
299 |
300 | * 父控件滑动时,banner切换会获取焦点,然后自动全部显示。不想让banner获取焦点可以给父控件加上:
301 |
302 | ```
303 | //banner也一定要用最新版哦!
304 | android:focusable="true"
305 | android:focusableInTouchMode="true"
306 | ```
307 | * 怎么设置圆角?
308 | 1、调用提供的方法或者自定义属性进行设置,这里设置的是banner本身的圆角,不是轮播内view的圆角
309 | 2、在adapter中对自定义的view进行自己实现,就拿图片举例:可以自己定义一个圆角的ImageView控件,或者使用glide渲染都行。请举一反三,view都自定义了还有什么不能改的?
310 |
311 |
312 | ## Thanks
313 |
314 | - [MZBannerView](https://github.com/pinguo-zhouwei/MZBannerView)
315 | - [MagicViewPager](https://github.com/hongyangAndroid/MagicViewPager)
316 | - [zguop的viewpager2的滑动时间解决方案](https://github.com/zguop/banner/blob/master/pager2banner/src/main/java/com/to/aboomy/pager2banner/Banner.java)
317 |
318 |
319 | ### 联系方式
320 | * 我的个人微博:https://weibo.com/u/3013494003 有兴趣的也可以关注,大家一起交流
321 | * 有问题可以加群大家一起交流,如果你觉得对你有帮助可以扫描下面支付宝二维码随意打赏下哦!
322 |
323 |
324 |
325 |
326 | ## 更新说明
327 | [更新说明](update_message.md)
328 |
329 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdk 33
5 |
6 | defaultConfig {
7 | applicationId "com.test.banner"
8 | minSdk 21
9 | targetSdk 33
10 | versionCode 1
11 | versionName "1.0"
12 | consumerProguardFiles 'proguard-rules.pro'
13 | }
14 | buildTypes {
15 | debug {
16 | minifyEnabled true
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | release {
20 | minifyEnabled true
21 | shrinkResources true
22 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
23 | }
24 | }
25 | compileOptions {
26 | sourceCompatibility JavaVersion.VERSION_1_8
27 | targetCompatibility JavaVersion.VERSION_1_8
28 | }
29 | }
30 |
31 | dependencies {
32 | implementation fileTree(dir: 'libs', include: ['*.jar'])
33 | implementation 'androidx.appcompat:appcompat:1.1.0'
34 | implementation 'androidx.recyclerview:recyclerview:1.2.0'
35 | implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0'
36 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
37 | implementation 'androidx.viewpager2:viewpager2:1.1.0-alpha01'
38 | implementation 'androidx.viewpager:viewpager:1.0.0'
39 | implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.2'
40 | implementation 'androidx.cardview:cardview:1.0.0'
41 | implementation 'com.github.bumptech.glide:glide:4.12.0'
42 | annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
43 | implementation 'com.google.android.material:material:1.2.0-alpha06'
44 | implementation 'com.jakewharton:butterknife:10.2.3'
45 | annotationProcessor 'com.jakewharton:butterknife-compiler:10.2.3'
46 |
47 | implementation 'com.shuyu:GSYVideoPlayer:7.1.3'
48 |
49 |
50 | implementation project(':banner')
51 | // implementation 'io.github.youth5201314:banner:2.2.1'
52 | }
53 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | #指定代码的压缩级别
2 | -optimizationpasses 5
3 | #包明不混合大小写
4 | -dontusemixedcaseclassnames
5 | #不去忽略非公共的库类
6 | -dontskipnonpubliclibraryclasses
7 | #优化 不优化输入的类文件
8 | -dontoptimize
9 | #预校验
10 | -dontpreverify
11 | #混淆时是否记录日志
12 | -verbose
13 | # 混淆时所采用的算法
14 | -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
15 | #保护注解
16 | -keepattributes *Annotation*
17 | # 保持哪些类不被混淆
18 | -keep public class * extends android.app.Fragment
19 | -keep public class * extends android.app.Activity
20 | -keep public class * extends android.app.Application
21 | -keep public class * extends android.app.Service
22 | -keep public class * extends android.content.BroadcastReceiver
23 | -keep public class * extends android.content.ContentProvider
24 | -keep public class * extends android.app.backup.BackupAgentHelper
25 | -keep public class * extends android.preference.Preference
26 | -keep public class com.android.vending.licensing.ILicensingService
27 |
28 | #glide
29 | -keep public class * implements com.bumptech.glide.module.GlideModule
30 | -keep public class * extends com.bumptech.glide.module.AppGlideModule
31 | -keep public enum com.bumptech.glide.load.ImageHeaderParser$** {
32 | **[] $VALUES;
33 | public *;
34 | }
35 |
36 | #butterknife
37 | -keep public class * implements butterknife.Unbinder { public (**, android.view.View); }
38 | -keep class butterknife.*
39 | -keepclasseswithmembernames class * { @butterknife.* ; }
40 | -keepclasseswithmembernames class * { @butterknife.* ; }
41 | -keepclassmembers class * { @butterknife.* ; }
42 | -keepclassmembers class * { @butterknife.* ; }
43 |
44 | #gsyvideoplayer
45 | -keep class com.shuyu.gsyvideoplayer.video.** { *; }
46 | -dontwarn com.shuyu.gsyvideoplayer.video.**
47 | -keep class com.shuyu.gsyvideoplayer.video.base.** { *; }
48 | -dontwarn com.shuyu.gsyvideoplayer.video.base.**
49 | -keep class com.shuyu.gsyvideoplayer.utils.** { *; }
50 | -dontwarn com.shuyu.gsyvideoplayer.utils.**
51 | -keep class tv.danmaku.ijk.** { *; }
52 | -dontwarn tv.danmaku.ijk.**
53 | -keep public class * extends android.view.View{
54 | *** get*();
55 | void set*(***);
56 | public (android.content.Context);
57 | public (android.content.Context, android.util.AttributeSet);
58 | public (android.content.Context, android.util.AttributeSet, int);
59 | }
60 |
61 | -dontwarn com.google.android.exoplayer.**
62 | -keep class com.google.android.exoplayer.**{*;}
63 |
64 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
12 |
13 |
14 |
15 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
31 |
32 |
33 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.test.banner;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.os.Handler;
6 | import android.view.View;
7 |
8 | import com.bumptech.glide.Glide;
9 | import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
10 | import com.bumptech.glide.request.RequestOptions;
11 | import com.google.android.material.snackbar.Snackbar;
12 | import com.test.banner.adapter.ImageAdapter;
13 | import com.test.banner.adapter.ImageTitleAdapter;
14 | import com.test.banner.adapter.ImageTitleNumAdapter;
15 | import com.test.banner.adapter.MultipleTypesAdapter;
16 | import com.test.banner.bean.DataBean;
17 | import com.youth.banner.indicator.DrawableIndicator;
18 | import com.test.banner.ui.ConstraintLayoutBannerActivity;
19 | import com.test.banner.ui.GalleryActivity;
20 | import com.test.banner.ui.RecyclerViewBannerActivity;
21 | import com.test.banner.ui.TVActivity;
22 | import com.test.banner.ui.TouTiaoActivity;
23 | import com.test.banner.ui.VideoActivity;
24 | import com.test.banner.ui.Vp2FragmentRecyclerviewActivity;
25 | import com.youth.banner.Banner;
26 | import com.youth.banner.adapter.BannerImageAdapter;
27 | import com.youth.banner.holder.BannerImageHolder;
28 | import com.youth.banner.config.BannerConfig;
29 | import com.youth.banner.config.IndicatorConfig;
30 | import com.youth.banner.indicator.CircleIndicator;
31 | import com.youth.banner.indicator.RoundLinesIndicator;
32 | import com.youth.banner.listener.OnPageChangeListener;
33 | import com.youth.banner.util.BannerUtils;
34 | import com.youth.banner.util.LogUtils;
35 |
36 | import androidx.appcompat.app.AppCompatActivity;
37 | import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
38 |
39 | import java.util.List;
40 |
41 | import butterknife.BindView;
42 | import butterknife.ButterKnife;
43 | import butterknife.OnClick;
44 |
45 | public class MainActivity extends AppCompatActivity {
46 | @BindView(R.id.banner)
47 | Banner banner;
48 | @BindView(R.id.indicator)
49 | RoundLinesIndicator indicator;
50 | @BindView(R.id.swipeRefresh)
51 | SwipeRefreshLayout refresh;
52 |
53 | @Override
54 | protected void onCreate(Bundle savedInstanceState) {
55 | super.onCreate(savedInstanceState);
56 | setContentView(R.layout.activity_main);
57 | ButterKnife.bind(this);
58 |
59 | List datas = DataBean.getTestData2();
60 |
61 | //自定义的图片适配器,也可以使用默认的BannerImageAdapter
62 | ImageAdapter adapter = new ImageAdapter(datas);
63 |
64 | banner.setAdapter(adapter)
65 | // .setCurrentItem(0,false)
66 | //添加生命周期观察者
67 | .addBannerLifecycleObserver(this)
68 | //设置指示器
69 | .setIndicator(new CircleIndicator(this))
70 | .setOnBannerListener((data, position) -> {
71 | Snackbar.make(banner, ((DataBean) data).title, Snackbar.LENGTH_SHORT).show();
72 | LogUtils.d("position:" + position);
73 | });
74 |
75 | //添加item之间切换时的间距(如果使用了画廊效果就不要添加间距了,因为内部已经添加过了)
76 | // banner.addPageTransformer(new MarginPageTransformer( BannerUtils.dp2px(10)));
77 |
78 | //和下拉刷新配套使用
79 | refresh.setOnRefreshListener(() -> {
80 | //模拟网络请求需要3秒,请求完成,设置setRefreshing 为false
81 | new Handler().postDelayed(() -> {
82 | refresh.setRefreshing(false);
83 |
84 | //给banner重新设置数据(完全覆盖)
85 | banner.setDatas(DataBean.getTestData());
86 |
87 | //模拟请求成功(原数据减少) 刷新banner
88 | // datas.remove(0);
89 | // adapter.notifyDataSetChanged();
90 |
91 | //对setDatas()方法不满意?你可以自己在adapter控制数据,参考setDatas()的实现修改
92 | // adapter.updateData(DataBean.getTestData());
93 | // banner.setCurrentItem(banner.getStartPosition(), false);
94 | // banner.setIndicatorPageChange();
95 |
96 | }, 2000);
97 | });
98 |
99 | }
100 |
101 |
102 | @OnClick({R.id.style_image, R.id.style_image_title, R.id.style_image_title_num, R.id.style_multiple,
103 | R.id.style_net_image, R.id.change_indicator, R.id.rv_banner, R.id.cl_banner, R.id.vp_banner,
104 | R.id.banner_video, R.id.banner_tv, R.id.gallery, R.id.topLine})
105 | public void click(View view) {
106 | indicator.setVisibility(View.GONE);
107 | switch (view.getId()) {
108 | case R.id.style_image:
109 | refresh.setEnabled(true);
110 | banner.setAdapter(new ImageAdapter(DataBean.getTestData()));
111 | banner.setIndicator(new CircleIndicator(this));
112 | banner.setIndicatorGravity(IndicatorConfig.Direction.CENTER);
113 | break;
114 | case R.id.style_image_title:
115 | refresh.setEnabled(true);
116 | banner.setAdapter(new ImageTitleAdapter(DataBean.getTestData()));
117 | banner.setIndicator(new CircleIndicator(this));
118 | banner.setIndicatorGravity(IndicatorConfig.Direction.RIGHT);
119 | banner.setIndicatorMargins(new IndicatorConfig.Margins(0, 0,
120 | BannerConfig.INDICATOR_MARGIN, BannerUtils.dp2px(12)));
121 | break;
122 | case R.id.style_image_title_num:
123 | refresh.setEnabled(true);
124 | //这里是将数字指示器和title都放在adapter中的,如果不想这样你也可以直接设置自定义的数字指示器
125 | banner.setAdapter(new ImageTitleNumAdapter(DataBean.getTestData()));
126 | banner.removeIndicator();
127 | break;
128 | case R.id.style_multiple:
129 | refresh.setEnabled(true);
130 | banner.setIndicator(new CircleIndicator(this));
131 | banner.setAdapter(new MultipleTypesAdapter(this, DataBean.getTestData()));
132 | break;
133 | case R.id.style_net_image:
134 | refresh.setEnabled(false);
135 | //方法一:使用自定义图片适配器
136 | // banner.setAdapter(new ImageNetAdapter(DataBean.getTestData3()));
137 |
138 | //方法二:使用自带的图片适配器
139 | banner.setAdapter(new BannerImageAdapter(DataBean.getTestData3()) {
140 | @Override
141 | public void onBindView(BannerImageHolder holder, DataBean data, int position, int size) {
142 | //图片加载自己实现
143 | Glide.with(holder.itemView)
144 | .load(data.imageUrl)
145 | .thumbnail(Glide.with(holder.itemView).load(R.drawable.loading))
146 | .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
147 | .into(holder.imageView);
148 | }
149 | });
150 | banner.setIndicator(new RoundLinesIndicator(this));
151 | banner.setIndicatorSelectedWidth(BannerUtils.dp2px(15));
152 | break;
153 | case R.id.change_indicator:
154 | indicator.setVisibility(View.VISIBLE);
155 | //在布局文件中使用指示器,这样更灵活
156 | banner.setIndicator(indicator, false);
157 | banner.setIndicatorSelectedWidth(BannerUtils.dp2px(15));
158 | break;
159 | case R.id.gallery:
160 | startActivity(new Intent(this, GalleryActivity.class));
161 | break;
162 | case R.id.rv_banner:
163 | startActivity(new Intent(this, RecyclerViewBannerActivity.class));
164 | break;
165 | case R.id.cl_banner:
166 | startActivity(new Intent(this, ConstraintLayoutBannerActivity.class));
167 | break;
168 | case R.id.vp_banner:
169 | startActivity(new Intent(this, Vp2FragmentRecyclerviewActivity.class));
170 | break;
171 | case R.id.banner_video:
172 | startActivity(new Intent(this, VideoActivity.class));
173 | break;
174 | case R.id.banner_tv:
175 | startActivity(new Intent(this, TVActivity.class));
176 | break;
177 | case R.id.topLine:
178 | startActivity(new Intent(this, TouTiaoActivity.class));
179 | break;
180 | default:
181 | throw new IllegalStateException("Unexpected value: " + view.getId());
182 | }
183 | }
184 | }
185 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/adapter/ImageAdapter.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.adapter;
2 |
3 | import android.view.ViewGroup;
4 | import android.widget.ImageView;
5 |
6 | import com.test.banner.bean.DataBean;
7 | import com.test.banner.viewholder.ImageHolder;
8 | import com.youth.banner.adapter.BannerAdapter;
9 |
10 | import java.util.List;
11 |
12 | /**
13 | * 自定义布局,图片
14 | */
15 | public class ImageAdapter extends BannerAdapter {
16 |
17 | public ImageAdapter(List mDatas) {
18 | //设置数据,也可以调用banner提供的方法,或者自己在adapter中实现
19 | super(mDatas);
20 | }
21 |
22 | //更新数据
23 | public void updateData(List data) {
24 | //这里的代码自己发挥,比如如下的写法等等
25 | mDatas.clear();
26 | mDatas.addAll(data);
27 | notifyDataSetChanged();
28 | }
29 |
30 |
31 | //创建ViewHolder,可以用viewType这个字段来区分不同的ViewHolder
32 | @Override
33 | public ImageHolder onCreateHolder(ViewGroup parent, int viewType) {
34 | ImageView imageView = new ImageView(parent.getContext());
35 | //注意,必须设置为match_parent,这个是viewpager2强制要求的
36 | ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
37 | ViewGroup.LayoutParams.MATCH_PARENT,
38 | ViewGroup.LayoutParams.MATCH_PARENT);
39 | imageView.setLayoutParams(params);
40 | imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
41 | return new ImageHolder(imageView);
42 | }
43 |
44 | @Override
45 | public void onBindView(ImageHolder holder, DataBean data, int position, int size) {
46 | holder.imageView.setImageResource(data.imageRes);
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/adapter/ImageNetAdapter.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.adapter;
2 |
3 | import android.graphics.Bitmap;
4 | import android.graphics.Outline;
5 | import android.graphics.drawable.Drawable;
6 | import android.os.Build;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.view.ViewOutlineProvider;
10 | import android.widget.ImageView;
11 |
12 | import androidx.annotation.NonNull;
13 | import androidx.annotation.Nullable;
14 | import androidx.annotation.RequiresApi;
15 |
16 | import com.bumptech.glide.Glide;
17 | import com.bumptech.glide.load.engine.DiskCacheStrategy;
18 | import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
19 | import com.bumptech.glide.request.RequestOptions;
20 | import com.bumptech.glide.request.target.BitmapImageViewTarget;
21 | import com.bumptech.glide.request.target.SimpleTarget;
22 | import com.bumptech.glide.request.transition.Transition;
23 | import com.test.banner.R;
24 | import com.test.banner.bean.DataBean;
25 | import com.test.banner.viewholder.ImageHolder;
26 | import com.youth.banner.adapter.BannerAdapter;
27 | import com.youth.banner.util.BannerUtils;
28 |
29 | import java.util.List;
30 |
31 | /**
32 | * 自定义布局,网络图片
33 | */
34 | public class ImageNetAdapter extends BannerAdapter {
35 |
36 | public ImageNetAdapter(List mDatas) {
37 | super(mDatas);
38 | }
39 |
40 | @Override
41 | public ImageHolder onCreateHolder(ViewGroup parent, int viewType) {
42 | ImageView imageView = (ImageView) BannerUtils.getView(parent, R.layout.banner_image);
43 | //通过裁剪实现圆角
44 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
45 | BannerUtils.setBannerRound(imageView, 20);
46 | }
47 | return new ImageHolder(imageView);
48 | }
49 |
50 | @Override
51 | public void onBindView(ImageHolder holder, DataBean data, int position, int size) {
52 | //通过图片加载器实现圆角,你也可以自己使用圆角的imageview,实现圆角的方法很多,自己尝试哈
53 | Glide.with(holder.itemView)
54 | .load(data.imageUrl)
55 | .thumbnail(Glide.with(holder.itemView).load(R.drawable.loading))
56 | .skipMemoryCache(true)
57 | .diskCacheStrategy(DiskCacheStrategy.NONE)
58 | // .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
59 | .into(holder.imageView);
60 | }
61 |
62 | }
63 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/adapter/ImageTitleAdapter.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.adapter;
2 |
3 | import android.view.LayoutInflater;
4 | import android.view.View;
5 | import android.view.ViewGroup;
6 |
7 | import com.test.banner.bean.DataBean;
8 | import com.test.banner.R;
9 | import com.test.banner.viewholder.ImageTitleHolder;
10 | import com.youth.banner.adapter.BannerAdapter;
11 | import com.youth.banner.util.BannerUtils;
12 |
13 | import java.util.List;
14 |
15 | /**
16 | * 自定义布局,图片+标题
17 | */
18 |
19 | public class ImageTitleAdapter extends BannerAdapter {
20 |
21 | public ImageTitleAdapter(List mDatas) {
22 | super(mDatas);
23 | }
24 |
25 | @Override
26 | public ImageTitleHolder onCreateHolder(ViewGroup parent, int viewType) {
27 | return new ImageTitleHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.banner_image_title, parent, false));
28 | }
29 |
30 | @Override
31 | public void onBindView(ImageTitleHolder holder, DataBean data, int position, int size) {
32 | holder.imageView.setImageResource(data.imageRes);
33 | holder.title.setText(data.title);
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/adapter/ImageTitleNumAdapter.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.adapter;
2 |
3 | import android.view.LayoutInflater;
4 | import android.view.View;
5 | import android.view.ViewGroup;
6 | import android.widget.ImageView;
7 | import android.widget.TextView;
8 |
9 | import androidx.annotation.NonNull;
10 | import androidx.recyclerview.widget.RecyclerView;
11 |
12 | import com.test.banner.bean.DataBean;
13 | import com.test.banner.R;
14 | import com.youth.banner.adapter.BannerAdapter;
15 |
16 | import java.util.List;
17 |
18 | /**
19 | * 自定义布局,图片+标题+数字指示器
20 | */
21 | public class ImageTitleNumAdapter extends BannerAdapter {
22 |
23 | public ImageTitleNumAdapter(List mDatas) {
24 | //设置数据,也可以调用banner提供的方法
25 | super(mDatas);
26 | }
27 |
28 | //创建ViewHolder,可以用viewType这个字段来区分不同的ViewHolder
29 | @Override
30 | public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
31 | //注意布局文件,item布局文件要设置为match_parent,这个是viewpager2强制要求的
32 | //或者调用BannerUtils.getView(parent,R.layout.banner_image_title_num);
33 | View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.banner_image_title_num, parent, false);
34 | return new BannerViewHolder(view);
35 | }
36 |
37 | //绑定数据
38 | @Override
39 | public void onBindView(BannerViewHolder holder, DataBean data, int position, int size) {
40 | holder.imageView.setImageResource(data.imageRes);
41 | holder.title.setText(data.title);
42 | //可以在布局文件中自己实现指示器,亦可以使用banner提供的方法自定义指示器,目前样式较少,后面补充
43 | holder.numIndicator.setText((position + 1) + "/" + size);
44 | }
45 |
46 |
47 | class BannerViewHolder extends RecyclerView.ViewHolder {
48 | ImageView imageView;
49 | TextView title;
50 | TextView numIndicator;
51 |
52 | public BannerViewHolder(@NonNull View view) {
53 | super(view);
54 | imageView = view.findViewById(R.id.image);
55 | title = view.findViewById(R.id.bannerTitle);
56 | numIndicator = view.findViewById(R.id.numIndicator);
57 | }
58 | }
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/adapter/MultipleTypesAdapter.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.adapter;
2 |
3 | import android.content.Context;
4 | import android.graphics.Color;
5 | import android.util.SparseArray;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 | import android.widget.ImageView;
9 |
10 | import androidx.recyclerview.widget.RecyclerView;
11 |
12 | import com.test.banner.R;
13 | import com.test.banner.bean.DataBean;
14 | import com.test.banner.viewholder.ImageHolder;
15 | import com.test.banner.viewholder.TitleHolder;
16 | import com.test.banner.viewholder.VideoHolder;
17 | import com.youth.banner.adapter.BannerAdapter;
18 | import com.youth.banner.util.BannerUtils;
19 |
20 | import java.util.List;
21 |
22 | /**
23 | * 自定义布局,多个不同UI切换
24 | */
25 | public class MultipleTypesAdapter extends BannerAdapter {
26 | private Context context;
27 | private SparseArray mVHMap = new SparseArray<>();
28 |
29 | public MultipleTypesAdapter(Context context, List mDatas) {
30 | super(mDatas);
31 | this.context = context;
32 | }
33 |
34 | @Override
35 | public RecyclerView.ViewHolder onCreateHolder(ViewGroup parent, int viewType) {
36 | switch (viewType) {
37 | case 1:
38 | return new ImageHolder(BannerUtils.getView(parent, R.layout.banner_image));
39 | case 2:
40 | return new VideoHolder(BannerUtils.getView(parent, R.layout.banner_video));
41 | case 3:
42 | return new TitleHolder(BannerUtils.getView(parent, R.layout.banner_title));
43 | }
44 | return new ImageHolder(BannerUtils.getView(parent, R.layout.banner_image));
45 | }
46 |
47 | @Override
48 | public int getItemViewType(int position) {
49 | //先取得真实的position,在获取实体
50 | // return getData(getRealPosition(position)).viewType;
51 | //直接获取真实的实体
52 | return getRealData(position).viewType;
53 | //或者自己直接去操作集合
54 | // return mDatas.get(getRealPosition(position)).viewType;
55 | }
56 |
57 | @Override
58 | public void onBindView(RecyclerView.ViewHolder holder, DataBean data, int position, int size) {
59 | int viewType = holder.getItemViewType();
60 | switch (viewType) {
61 | case 1:
62 | ImageHolder imageHolder = (ImageHolder) holder;
63 | mVHMap.append(position,imageHolder);
64 | imageHolder.imageView.setImageResource(data.imageRes);
65 | break;
66 | case 2:
67 | VideoHolder videoHolder = (VideoHolder) holder;
68 | mVHMap.append(position,videoHolder);
69 | videoHolder.player.setUp(data.imageUrl, true, null);
70 | videoHolder.player.getBackButton().setVisibility(View.GONE);
71 | //增加封面
72 | ImageView imageView = new ImageView(context);
73 | imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
74 | imageView.setImageResource(R.drawable.image4);
75 | videoHolder.player.setThumbImageView(imageView);
76 | // videoHolder.player.startPlayLogic();
77 | break;
78 | case 3:
79 | TitleHolder titleHolder = (TitleHolder) holder;
80 | mVHMap.append(position,titleHolder);
81 | titleHolder.title.setText(data.title);
82 | titleHolder.title.setBackgroundColor(Color.parseColor(DataBean.getRandColor()));
83 | break;
84 | }
85 | }
86 |
87 | public SparseArray getVHMap() {
88 | return mVHMap;
89 | }
90 |
91 |
92 | }
93 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/adapter/MyRecyclerViewAdapter.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.adapter;
2 |
3 | import android.content.Context;
4 | import android.graphics.Color;
5 | import android.util.Log;
6 | import android.view.LayoutInflater;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 |
10 | import androidx.annotation.NonNull;
11 | import androidx.cardview.widget.CardView;
12 | import androidx.recyclerview.widget.RecyclerView;
13 |
14 | import com.test.banner.R;
15 | import com.test.banner.bean.DataBean;
16 | import com.youth.banner.Banner;
17 | import com.youth.banner.indicator.RoundLinesIndicator;
18 | import com.youth.banner.util.BannerUtils;
19 |
20 | public class MyRecyclerViewAdapter extends RecyclerView.Adapter {
21 | private Context context;
22 |
23 | public MyRecyclerViewAdapter(Context context) {
24 | this.context = context;
25 | }
26 |
27 | @NonNull
28 | @Override
29 | public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
30 | if (viewType==R.layout.item) {
31 | return new MyViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item, parent, false));
32 | }else{
33 | return new MyBannerViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.banner, parent, false));
34 | }
35 | }
36 |
37 | @Override
38 | public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
39 | if (holder instanceof MyViewHolder) {
40 | ((MyViewHolder) holder).cardView.setBackgroundColor(Color.parseColor(DataBean.getRandColor()));
41 | }else if (holder instanceof MyBannerViewHolder){
42 | Banner banner=((MyBannerViewHolder) holder).banner;
43 | banner.setAdapter(new ImageNetAdapter(DataBean.getTestData3()));
44 | banner.setBannerRound(BannerUtils.dp2px(5));
45 | banner.setIndicator(new RoundLinesIndicator(context));
46 | banner.setIndicatorSelectedWidth((int) BannerUtils.dp2px(15));
47 | }
48 |
49 | }
50 |
51 | @Override
52 | public int getItemViewType(int position) {
53 | if (position%2==0){
54 | return R.layout.item;
55 | }else{
56 | return R.layout.banner;
57 | }
58 | }
59 |
60 | @Override
61 | public int getItemCount() {
62 | return 10;
63 | }
64 |
65 |
66 | //banner 内部已实现
67 | // @Override
68 | // public void onViewDetachedFromWindow(@NonNull RecyclerView.ViewHolder holder) {
69 | // super.onViewDetachedFromWindow(holder);
70 | // Log.e("banner_log", "onViewDetachedFromWindow:" + holder.getAdapterPosition());
71 | // //定位你的位置
72 | // if (holder.getAdapterPosition()%2!=0) {
73 | // if (holder instanceof MyBannerViewHolder) {
74 | // ((MyBannerViewHolder) holder).banner.stop();
75 | // }
76 | // }
77 | // }
78 | //
79 | // @Override
80 | // public void onViewAttachedToWindow(RecyclerView.ViewHolder holder) {
81 | // super.onViewAttachedToWindow(holder);
82 | // Log.e("banner_log", "onViewAttachedToWindow:" + holder.getAdapterPosition());
83 | // if (holder.getAdapterPosition()%2!=0) {
84 | // if (holder instanceof MyBannerViewHolder) {
85 | // ((MyBannerViewHolder) holder).banner.start();
86 | // }
87 | // }
88 | // }
89 |
90 | class MyViewHolder extends RecyclerView.ViewHolder {
91 | public CardView cardView;
92 |
93 | public MyViewHolder(@NonNull View itemView) {
94 | super(itemView);
95 | cardView = itemView.findViewById(R.id.card_view);
96 | }
97 | }
98 |
99 | class MyBannerViewHolder extends RecyclerView.ViewHolder {
100 | public Banner banner;
101 |
102 | public MyBannerViewHolder(@NonNull View itemView) {
103 | super(itemView);
104 | banner = itemView.findViewById(R.id.banner);
105 | }
106 | }
107 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/adapter/TopLineAdapter.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.adapter;
2 |
3 | import android.view.View;
4 | import android.view.ViewGroup;
5 | import android.widget.TextView;
6 |
7 | import androidx.annotation.NonNull;
8 | import androidx.recyclerview.widget.RecyclerView;
9 |
10 | import com.test.banner.bean.DataBean;
11 | import com.test.banner.R;
12 | import com.youth.banner.adapter.BannerAdapter;
13 | import com.youth.banner.util.BannerUtils;
14 |
15 | import java.util.List;
16 |
17 | /**
18 | * 自定义布局,实现类似1号店、淘宝头条的滚动效果
19 | */
20 | public class TopLineAdapter extends BannerAdapter {
21 |
22 | public TopLineAdapter(List mDatas) {
23 | super(mDatas);
24 | }
25 |
26 | @Override
27 | public TopLineHolder onCreateHolder(ViewGroup parent, int viewType) {
28 | return new TopLineHolder(BannerUtils.getView(parent,R.layout.top_line_item2));
29 | }
30 |
31 | @Override
32 | public void onBindView(TopLineHolder holder, DataBean data, int position, int size) {
33 | holder.message.setText(data.title);
34 | if (data.viewType==1) {
35 | holder.source.setText("1号店");
36 | }else if (data.viewType==2) {
37 | holder.source.setText("淘宝头条");
38 | }else if (data.viewType==3) {
39 | holder.source.setText("京东快报");
40 | }
41 | }
42 |
43 | class TopLineHolder extends RecyclerView.ViewHolder {
44 | public TextView message;
45 | public TextView source;
46 |
47 | public TopLineHolder(@NonNull View view) {
48 | super(view);
49 | message=view.findViewById(R.id.message);
50 | source=view.findViewById(R.id.source);
51 | }
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/bean/DataBean.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.bean;
2 |
3 | import com.test.banner.R;
4 |
5 | import java.util.ArrayList;
6 | import java.util.List;
7 | import java.util.Random;
8 |
9 | public class DataBean {
10 | public Integer imageRes;
11 | public String imageUrl;
12 | public String title;
13 | public int viewType;
14 |
15 | public DataBean(Integer imageRes, String title, int viewType) {
16 | this.imageRes = imageRes;
17 | this.title = title;
18 | this.viewType = viewType;
19 | }
20 |
21 | public DataBean(String imageUrl, String title, int viewType) {
22 | this.imageUrl = imageUrl;
23 | this.title = title;
24 | this.viewType = viewType;
25 | }
26 |
27 | public static List getTestData() {
28 | List list = new ArrayList<>();
29 | list.add(new DataBean(R.drawable.image1, "相信自己,你努力的样子真的很美", 1));
30 | list.add(new DataBean(R.drawable.image2, "极致简约,梦幻小屋", 3));
31 | list.add(new DataBean(R.drawable.image3, "超级卖梦人", 3));
32 | list.add(new DataBean(R.drawable.image4, "夏季新搭配", 1));
33 | list.add(new DataBean(R.drawable.image5, "绝美风格搭配", 1));
34 | list.add(new DataBean(R.drawable.image6, "微微一笑 很倾城", 3));
35 | return list;
36 | }
37 |
38 | public static List getTestData2() {
39 | List list = new ArrayList<>();
40 | list.add(new DataBean(R.drawable.image7, "听风.赏雨", 3));
41 | list.add(new DataBean(R.drawable.image8, "迪丽热巴.迪力木拉提", 1));
42 | list.add(new DataBean(R.drawable.image9, "爱美.人间有之", 3));
43 | list.add(new DataBean(R.drawable.image10, "洋洋洋.气质篇", 1));
44 | list.add(new DataBean(R.drawable.image11, "生活的态度", 3));
45 | return list;
46 | }
47 |
48 | /**
49 | * 仿淘宝商品详情第一个是视频
50 | * @return
51 | */
52 | public static List getTestDataVideo() {
53 | List list = new ArrayList<>();
54 | list.add(new DataBean("http://vfx.mtime.cn/Video/2019/03/09/mp4/190309153658147087.mp4", "第一个放视频", 2));
55 | list.add(new DataBean(R.drawable.image7, "听风.赏雨", 1));
56 | list.add(new DataBean(R.drawable.image8, "迪丽热巴.迪力木拉提", 1));
57 | list.add(new DataBean(R.drawable.image9, "爱美.人间有之", 1));
58 | list.add(new DataBean(R.drawable.image10, "洋洋洋.气质篇", 1));
59 | list.add(new DataBean(R.drawable.image11, "生活的态度", 1));
60 | return list;
61 | }
62 |
63 | public static List getTestData3() {
64 | List list = new ArrayList<>();
65 | list.add(new DataBean("https://img.zcool.cn/community/013de756fb63036ac7257948747896.jpg", null, 1));
66 | list.add(new DataBean("https://img.zcool.cn/community/01639a56fb62ff6ac725794891960d.jpg", null, 1));
67 | list.add(new DataBean("https://img.zcool.cn/community/01270156fb62fd6ac72579485aa893.jpg", null, 1));
68 | list.add(new DataBean("https://img.zcool.cn/community/01233056fb62fe32f875a9447400e1.jpg", null, 1));
69 | list.add(new DataBean("https://img.zcool.cn/community/016a2256fb63006ac7257948f83349.jpg", null, 1));
70 | return list;
71 | }
72 |
73 | public static List getVideos() {
74 | List list = new ArrayList<>();
75 | list.add(new DataBean("http://vfx.mtime.cn/Video/2019/03/21/mp4/190321153853126488.mp4", null, 0));
76 | list.add(new DataBean("http://vfx.mtime.cn/Video/2019/03/18/mp4/190318231014076505.mp4", null, 0));
77 | list.add(new DataBean("http://vfx.mtime.cn/Video/2019/03/18/mp4/190318214226685784.mp4", null, 0));
78 | list.add(new DataBean("http://vfx.mtime.cn/Video/2019/03/19/mp4/190319125415785691.mp4", null, 0));
79 | list.add(new DataBean("http://vfx.mtime.cn/Video/2019/03/14/mp4/190314223540373995.mp4", null, 0));
80 | list.add(new DataBean("http://vfx.mtime.cn/Video/2019/03/14/mp4/190314102306987969.mp4", null, 0));
81 | return list;
82 | }
83 |
84 |
85 | public static List getColors(int size) {
86 | List list = new ArrayList<>();
87 | for(int i = 0; i < size; i++) {
88 | list.add(getRandColor());
89 | }
90 | return list;
91 | }
92 |
93 | /**
94 | * 获取十六进制的颜色代码.例如 "#5A6677"
95 | * 分别取R、G、B的随机值,然后加起来即可
96 | *
97 | * @return String
98 | */
99 | public static String getRandColor() {
100 | String R, G, B;
101 | Random random = new Random();
102 | R = Integer.toHexString(random.nextInt(256)).toUpperCase();
103 | G = Integer.toHexString(random.nextInt(256)).toUpperCase();
104 | B = Integer.toHexString(random.nextInt(256)).toUpperCase();
105 |
106 | R = R.length() == 1 ? "0" + R : R;
107 | G = G.length() == 1 ? "0" + G : G;
108 | B = B.length() == 1 ? "0" + B : B;
109 |
110 | return "#" + R + G + B;
111 | }
112 | }
113 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/indicator/NumIndicator.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.indicator;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Color;
6 | import android.graphics.Paint;
7 | import android.graphics.RectF;
8 | import android.util.AttributeSet;
9 |
10 | import com.youth.banner.indicator.BaseIndicator;
11 | import com.youth.banner.util.BannerUtils;
12 |
13 | /**
14 | * 自定义数字指示器demo,比较简单,具体的自己发挥
15 | *
16 | * 这里没有用的自定义属性的参数,可以考虑加上
17 | */
18 | public class NumIndicator extends BaseIndicator {
19 | private int width;
20 | private int height;
21 | private int radius;
22 |
23 | public NumIndicator(Context context) {
24 | this(context, null);
25 | }
26 |
27 | public NumIndicator(Context context, AttributeSet attrs) {
28 | this(context, attrs, 0);
29 | }
30 |
31 | public NumIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
32 | super(context, attrs, defStyleAttr);
33 | mPaint.setTextSize(BannerUtils.dp2px(10));
34 | mPaint.setTextAlign(Paint.Align.CENTER);
35 | width = (int) BannerUtils.dp2px(30);
36 | height = (int) BannerUtils.dp2px(15);
37 | radius = (int) BannerUtils.dp2px(20);
38 | }
39 |
40 | @Override
41 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
42 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
43 | int count = config.getIndicatorSize();
44 | if (count <= 1) {
45 | return;
46 | }
47 | setMeasuredDimension(width, height);
48 | }
49 |
50 | @Override
51 | protected void onDraw(Canvas canvas) {
52 | super.onDraw(canvas);
53 | int count = config.getIndicatorSize();
54 | if (count <= 1) {
55 | return;
56 | }
57 |
58 | RectF rectF = new RectF(0, 0, width, height);
59 | mPaint.setColor(Color.parseColor("#70000000"));
60 | canvas.drawRoundRect(rectF, radius, radius, mPaint);
61 |
62 | String text = config.getCurrentPosition() + 1 + "/" + count;
63 | mPaint.setColor(Color.WHITE);
64 | canvas.drawText(text, width / 2, (float) (height * 0.7), mPaint);
65 |
66 | }
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/ui/BannerFragment.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.ui;
2 |
3 | import android.os.Bundle;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 |
8 | import androidx.annotation.NonNull;
9 | import androidx.annotation.Nullable;
10 | import androidx.fragment.app.Fragment;
11 |
12 | import com.test.banner.R;
13 | import com.test.banner.adapter.ImageNetAdapter;
14 | import com.test.banner.bean.DataBean;
15 | import com.youth.banner.Banner;
16 | import com.youth.banner.indicator.RectangleIndicator;
17 | import com.youth.banner.util.BannerUtils;
18 |
19 | import butterknife.BindView;
20 | import butterknife.ButterKnife;
21 |
22 | public class BannerFragment extends Fragment {
23 |
24 | @BindView(R.id.banner)
25 | Banner banner;
26 |
27 | public static Fragment newInstance() {
28 | return new BannerFragment();
29 | }
30 |
31 | @Nullable
32 | @Override
33 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
34 | View view = inflater.inflate(R.layout.banner, container, false);
35 | ButterKnife.bind(this, view);
36 | return view;
37 | }
38 |
39 | @Override
40 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
41 | super.onViewCreated(view, savedInstanceState);
42 | banner.setAdapter(new ImageNetAdapter(DataBean.getTestData3()));
43 | banner.setIndicator(new RectangleIndicator(getActivity()));
44 | banner.setIndicatorSpace((int) BannerUtils.dp2px(4));
45 | banner.setIndicatorRadius(0);
46 | }
47 |
48 | @Override
49 | public void onStart() {
50 | super.onStart();
51 | banner.start();
52 | }
53 |
54 | @Override
55 | public void onStop() {
56 | super.onStop();
57 | banner.stop();
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/ui/BannerListFragment.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.ui;
2 |
3 | import android.os.Bundle;
4 | import android.view.LayoutInflater;
5 | import android.view.View;
6 | import android.view.ViewGroup;
7 | import android.widget.TextView;
8 |
9 | import androidx.annotation.NonNull;
10 | import androidx.annotation.Nullable;
11 | import androidx.fragment.app.Fragment;
12 | import androidx.recyclerview.widget.LinearLayoutManager;
13 | import androidx.recyclerview.widget.RecyclerView;
14 |
15 | import com.test.banner.R;
16 | import com.test.banner.adapter.MyRecyclerViewAdapter;
17 | import com.test.banner.util.ParentRecyclerView;
18 |
19 | import butterknife.BindView;
20 | import butterknife.ButterKnife;
21 |
22 | public class BannerListFragment extends Fragment {
23 | private static int index;
24 | @BindView(R.id.net_rv)
25 | RecyclerView recyclerView;
26 | @BindView(R.id.text)
27 | TextView text;
28 |
29 | public static Fragment newInstance(int i) {
30 | index = i;
31 | return new BannerListFragment();
32 | }
33 |
34 | @Nullable
35 | @Override
36 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
37 | View view = inflater.inflate(R.layout.activity_recyclerview_banner, container, false);
38 | ButterKnife.bind(this,view);
39 | return view;
40 | }
41 |
42 | @Override
43 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
44 | super.onViewCreated(view, savedInstanceState);
45 | text.setText("当前页:"+index);
46 | recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
47 | recyclerView.setAdapter(new MyRecyclerViewAdapter(getActivity()));
48 | }
49 |
50 | }
51 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/ui/BlankFragment.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.ui;
2 |
3 | import android.os.Bundle;
4 | import android.view.Gravity;
5 | import android.view.LayoutInflater;
6 | import android.view.View;
7 | import android.view.ViewGroup;
8 | import android.widget.LinearLayout;
9 | import android.widget.TextView;
10 |
11 | import androidx.annotation.NonNull;
12 | import androidx.annotation.Nullable;
13 | import androidx.fragment.app.Fragment;
14 | import androidx.recyclerview.widget.RecyclerView;
15 |
16 | import com.test.banner.R;
17 | import com.test.banner.adapter.ImageNetAdapter;
18 | import com.test.banner.adapter.MyRecyclerViewAdapter;
19 | import com.test.banner.bean.DataBean;
20 | import com.test.banner.indicator.NumIndicator;
21 | import com.youth.banner.Banner;
22 | import com.youth.banner.config.IndicatorConfig;
23 | import com.youth.banner.indicator.CircleIndicator;
24 | import com.youth.banner.util.BannerUtils;
25 |
26 | import butterknife.BindView;
27 | import butterknife.ButterKnife;
28 |
29 | public class BlankFragment extends Fragment {
30 |
31 | public static Fragment newInstance() {
32 | return new BlankFragment();
33 | }
34 |
35 | @Nullable
36 | @Override
37 | public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
38 | return inflater.inflate(R.layout.test,null);
39 | }
40 |
41 | @Override
42 | public void onActivityCreated(@Nullable Bundle savedInstanceState) {
43 | super.onActivityCreated(savedInstanceState);
44 | LinearLayout linearLayout = getView().findViewById(R.id.ll_view);
45 |
46 | //通过new的方式创建banner
47 | Banner banner = new Banner(getActivity());
48 | banner.setAdapter(new ImageNetAdapter(DataBean.getTestData3()));
49 | banner.addBannerLifecycleObserver(this);
50 | banner.setIndicator(new CircleIndicator(getActivity()));
51 |
52 | //将banner加入到父容器中,实际使用不一定一样
53 | linearLayout.addView(banner, LinearLayout.LayoutParams.MATCH_PARENT, (int) BannerUtils.dp2px(120));
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/ui/ConstraintLayoutBannerActivity.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.ui;
2 |
3 | import android.os.Bundle;
4 | import android.util.Log;
5 | import android.view.KeyEvent;
6 |
7 | import androidx.appcompat.app.AppCompatActivity;
8 |
9 | import com.test.banner.R;
10 | import com.test.banner.adapter.ImageAdapter;
11 | import com.test.banner.adapter.ImageNetAdapter;
12 | import com.test.banner.adapter.ImageTitleAdapter;
13 | import com.test.banner.bean.DataBean;
14 | import com.youth.banner.Banner;
15 | import com.youth.banner.config.BannerConfig;
16 | import com.youth.banner.config.IndicatorConfig;
17 | import com.youth.banner.indicator.CircleIndicator;
18 | import com.youth.banner.util.BannerUtils;
19 |
20 | import butterknife.BindView;
21 | import butterknife.ButterKnife;
22 |
23 | public class ConstraintLayoutBannerActivity extends AppCompatActivity {
24 | private static final String TAG = "banner_log";
25 | @BindView(R.id.banner)
26 | Banner banner;
27 |
28 | @Override
29 | protected void onCreate(Bundle savedInstanceState) {
30 | super.onCreate(savedInstanceState);
31 | setContentView(R.layout.activity_constraint_layout_banner);
32 | ButterKnife.bind(this);
33 | banner.setAdapter(new ImageTitleAdapter(DataBean.getTestData()));
34 | banner.setIndicator(new CircleIndicator(this));
35 | banner.setIndicatorSelectedColorRes(R.color.main_color);
36 | banner.setIndicatorGravity(IndicatorConfig.Direction.RIGHT);
37 | banner.setIndicatorMargins(new IndicatorConfig.Margins(0, 0,
38 | BannerConfig.INDICATOR_MARGIN, (int) BannerUtils.dp2px(12)));
39 | banner.addBannerLifecycleObserver(this);
40 | }
41 |
42 | }
43 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/ui/GalleryActivity.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.ui;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 |
6 | import com.test.banner.R;
7 | import com.test.banner.adapter.ImageAdapter;
8 | import com.test.banner.adapter.ImageNetAdapter;
9 | import com.test.banner.bean.DataBean;
10 | import com.youth.banner.Banner;
11 | import com.youth.banner.indicator.CircleIndicator;
12 | import com.youth.banner.indicator.DrawableIndicator;
13 | import com.youth.banner.transformer.AlphaPageTransformer;
14 |
15 | import androidx.appcompat.app.AppCompatActivity;
16 | import butterknife.BindView;
17 | import butterknife.ButterKnife;
18 | import butterknife.OnClick;
19 |
20 | public class GalleryActivity extends AppCompatActivity {
21 |
22 | @BindView(R.id.banner1)
23 | Banner mBanner1;
24 | @BindView(R.id.banner2)
25 | Banner mBanner2;
26 | @BindView(R.id.indicator)
27 | DrawableIndicator indicator;
28 |
29 | @Override
30 | protected void onCreate(Bundle savedInstanceState) {
31 | super.onCreate(savedInstanceState);
32 | setContentView(R.layout.activity_gallery);
33 | ButterKnife.bind(this);
34 |
35 | /**
36 | * 画廊效果
37 | */
38 | mBanner1.setAdapter(new ImageAdapter(DataBean.getTestData2()));
39 | mBanner1.setIndicator(new CircleIndicator(this));
40 | //添加画廊效果
41 | mBanner1.setBannerGalleryEffect(50, 10);
42 | //(可以和其他PageTransformer组合使用,比如AlphaPageTransformer,注意但和其他带有缩放的PageTransformer会显示冲突)
43 | //添加透明效果(画廊配合透明效果更棒)
44 | //mBanner1.addPageTransformer(new AlphaPageTransformer());
45 |
46 |
47 | /**
48 | * 魅族效果
49 | */
50 | mBanner2.setAdapter(new ImageAdapter(DataBean.getTestData()));
51 | mBanner2.setIndicator(indicator,false);
52 | //添加魅族效果
53 | mBanner2.setBannerGalleryMZ(20);
54 |
55 |
56 |
57 | }
58 |
59 |
60 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/ui/RecyclerViewBannerActivity.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.ui;
2 |
3 | import android.os.Bundle;
4 | import android.view.View;
5 | import android.widget.TextView;
6 |
7 | import androidx.annotation.NonNull;
8 | import androidx.appcompat.app.AppCompatActivity;
9 | import androidx.recyclerview.widget.LinearLayoutManager;
10 | import androidx.recyclerview.widget.RecyclerView;
11 |
12 | import com.test.banner.R;
13 | import com.test.banner.adapter.MyRecyclerViewAdapter;
14 |
15 | import butterknife.BindView;
16 | import butterknife.ButterKnife;
17 |
18 | public class RecyclerViewBannerActivity extends AppCompatActivity {
19 | @BindView(R.id.net_rv)
20 | RecyclerView recyclerView;
21 |
22 | @Override
23 | protected void onCreate(Bundle savedInstanceState) {
24 | super.onCreate(savedInstanceState);
25 | setContentView(R.layout.activity_recyclerview_banner);
26 | ButterKnife.bind(this);
27 | recyclerView.setLayoutManager(new LinearLayoutManager(this));
28 | recyclerView.setAdapter(new MyRecyclerViewAdapter(this));
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/ui/TVActivity.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.ui;
2 |
3 | import androidx.appcompat.app.AppCompatActivity;
4 |
5 | import android.os.Bundle;
6 | import android.util.Log;
7 | import android.view.KeyEvent;
8 |
9 | import com.test.banner.R;
10 | import com.test.banner.adapter.ImageAdapter;
11 | import com.test.banner.bean.DataBean;
12 | import com.youth.banner.Banner;
13 | import com.youth.banner.indicator.CircleIndicator;
14 | import com.youth.banner.util.BannerUtils;
15 |
16 | import butterknife.BindView;
17 | import butterknife.ButterKnife;
18 |
19 | public class TVActivity extends AppCompatActivity {
20 |
21 | private static final String TAG = "banner_log";
22 | @BindView(R.id.banner)
23 | Banner banner;
24 |
25 | @Override
26 | protected void onCreate(Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | setContentView(R.layout.activity_t_v);
29 | ButterKnife.bind(this);
30 | banner.setAdapter(new ImageAdapter(DataBean.getTestData()));
31 | banner.setIndicator(new CircleIndicator(this));
32 | banner.isAutoLoop(false);
33 | }
34 |
35 | @Override
36 | public boolean onKeyDown(int keyCode, KeyEvent event) {
37 | int count = banner.getItemCount();
38 | switch (keyCode) {
39 | case KeyEvent.KEYCODE_DPAD_LEFT:
40 | Log.d(TAG, "向左");
41 | int prev = (banner.getCurrentItem() - 1) % count;
42 | if (prev == 0) {
43 | prev = banner.getRealCount();
44 | } else if (prev == count - 1) {
45 | prev = 1;
46 | }
47 | banner.setCurrentItem(prev, false);
48 | break;
49 | case KeyEvent.KEYCODE_DPAD_RIGHT:
50 | Log.d(TAG, "向右");
51 | int next = (banner.getCurrentItem() + 1) % count;
52 | if (next == 0) {
53 | next = banner.getRealCount();
54 | } else if (next == count - 1) {
55 | next = 1;
56 | }
57 | banner.setCurrentItem(next, false);
58 | break;
59 | }
60 | //如果没有设置指示器,就不用执行下面两行
61 | int real = BannerUtils.getRealPosition(banner.isInfiniteLoop(), banner.getCurrentItem(), banner.getRealCount());
62 | banner.getIndicator().onPageSelected(real);
63 | return super.onKeyDown(keyCode, event);
64 | }
65 |
66 | }
67 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/ui/TouTiaoActivity.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.ui;
2 |
3 | import androidx.appcompat.app.AppCompatActivity;
4 | import butterknife.BindView;
5 | import butterknife.ButterKnife;
6 |
7 | import android.os.Bundle;
8 |
9 | import com.google.android.material.snackbar.Snackbar;
10 | import com.test.banner.R;
11 | import com.test.banner.adapter.TopLineAdapter;
12 | import com.test.banner.bean.DataBean;
13 | import com.youth.banner.Banner;
14 | import com.youth.banner.transformer.ZoomOutPageTransformer;
15 | import com.youth.banner.util.LogUtils;
16 |
17 | public class TouTiaoActivity extends AppCompatActivity {
18 | @BindView(R.id.banner)
19 | Banner banner;
20 |
21 | @Override
22 | protected void onCreate(Bundle savedInstanceState) {
23 | super.onCreate(savedInstanceState);
24 | setContentView(R.layout.activity_tou_tiao);
25 | ButterKnife.bind(this);
26 |
27 | //实现1号店和淘宝头条类似的效果
28 | banner.setAdapter(new TopLineAdapter(DataBean.getTestData2()))
29 | .setOrientation(Banner.VERTICAL)
30 | .setPageTransformer(new ZoomOutPageTransformer())
31 | .setOnBannerListener((data, position) -> {
32 | Snackbar.make(banner, ((DataBean) data).title, Snackbar.LENGTH_SHORT).show();
33 | LogUtils.d("position:" + position);
34 | });
35 |
36 | }
37 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/ui/VideoActivity.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.ui;
2 |
3 | import android.os.Bundle;
4 | import android.util.Log;
5 |
6 | import com.shuyu.gsyvideoplayer.GSYVideoManager;
7 | import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
8 | import com.test.banner.R;
9 | import com.test.banner.adapter.MultipleTypesAdapter;
10 | import com.test.banner.bean.DataBean;
11 | import com.test.banner.indicator.NumIndicator;
12 | import com.test.banner.viewholder.VideoHolder;
13 | import com.youth.banner.Banner;
14 | import com.youth.banner.config.IndicatorConfig;
15 | import com.youth.banner.listener.OnPageChangeListener;
16 |
17 | import androidx.appcompat.app.AppCompatActivity;
18 | import androidx.recyclerview.widget.RecyclerView;
19 | import butterknife.BindView;
20 | import butterknife.ButterKnife;
21 |
22 | /**
23 | * 仿淘宝商品详情,banner第一个放视频,然后首尾不能自己滑动,加上自定义数字指示器
24 | */
25 | public class VideoActivity extends AppCompatActivity {
26 | @BindView(R.id.banner)
27 | Banner banner;
28 | StandardGSYVideoPlayer player;
29 |
30 | @Override
31 | protected void onCreate(Bundle savedInstanceState) {
32 | super.onCreate(savedInstanceState);
33 | setContentView(R.layout.activity_video);
34 | ButterKnife.bind(this);
35 |
36 | banner.addBannerLifecycleObserver(this)
37 | .setAdapter(new MultipleTypesAdapter(this, DataBean.getTestDataVideo()))
38 | .setIndicator(new NumIndicator(this))
39 | .setIndicatorGravity(IndicatorConfig.Direction.RIGHT)
40 | .addOnPageChangeListener(new OnPageChangeListener() {
41 | @Override
42 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
43 | stopVideo(position);
44 | }
45 |
46 | @Override
47 | public void onPageSelected(int position) {
48 | Log.e("--","position:"+position);
49 | stopVideo(position);
50 | }
51 |
52 | @Override
53 | public void onPageScrollStateChanged(int state) {
54 |
55 | }
56 | });
57 | }
58 |
59 | private void stopVideo(int position) {
60 | if (player == null) {
61 | RecyclerView.ViewHolder viewHolder = banner.getAdapter().getViewHolder();
62 | if (viewHolder instanceof VideoHolder) {
63 | VideoHolder holder = (VideoHolder) viewHolder;
64 | player = holder.player;
65 | if (position != 0) {
66 | player.onVideoPause();
67 | }
68 | }
69 | }else {
70 | if (position != 0) {
71 | player.onVideoPause();
72 | }
73 | }
74 | }
75 |
76 | @Override
77 | protected void onPause() {
78 | super.onPause();
79 | if (player != null)
80 | player.onVideoPause();
81 | }
82 |
83 | @Override
84 | protected void onResume() {
85 | super.onResume();
86 | if (player != null)
87 | player.onVideoResume();
88 | }
89 |
90 | @Override
91 | protected void onDestroy() {
92 | super.onDestroy();
93 | GSYVideoManager.releaseAllVideos();
94 | }
95 |
96 | @Override
97 | public void onBackPressed() {
98 | //释放所有
99 | if (player != null)
100 | player.setVideoAllCallBack(null);
101 | super.onBackPressed();
102 | }
103 |
104 | }
105 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/ui/Vp2FragmentRecyclerviewActivity.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.ui;
2 |
3 | import android.os.Bundle;
4 |
5 | import com.google.android.material.tabs.TabLayout;
6 | import com.test.banner.R;
7 | import com.test.banner.adapter.ImageAdapter;
8 | import com.test.banner.bean.DataBean;
9 | import com.test.banner.util.TabLayoutMediator;
10 | import com.youth.banner.Banner;
11 | import com.youth.banner.indicator.CircleIndicator;
12 |
13 | import androidx.annotation.NonNull;
14 | import androidx.appcompat.app.AppCompatActivity;
15 | import androidx.fragment.app.Fragment;
16 | import androidx.viewpager2.adapter.FragmentStateAdapter;
17 | import androidx.viewpager2.widget.ViewPager2;
18 | import butterknife.BindView;
19 | import butterknife.ButterKnife;
20 |
21 | public class Vp2FragmentRecyclerviewActivity extends AppCompatActivity {
22 |
23 | @BindView(R.id.vp2)
24 | ViewPager2 viewPager2;
25 | @BindView(R.id.tab_layout)
26 | TabLayout mTabLayout;
27 | @BindView(R.id.banner)
28 | Banner mBanner;
29 |
30 | @Override
31 | protected void onCreate(Bundle savedInstanceState) {
32 | super.onCreate(savedInstanceState);
33 | setContentView(R.layout.activity_vp2_fragment_recyclerview);
34 | ButterKnife.bind(this);
35 |
36 | viewPager2.setAdapter(new FragmentStateAdapter(this) {
37 | @NonNull
38 | @Override
39 | public Fragment createFragment(int position) {
40 | if (position == 0) {
41 | return BannerListFragment.newInstance(position);
42 | } else if (position == 1) {
43 | return BlankFragment.newInstance();
44 | } else {
45 | return BannerFragment.newInstance();
46 | }
47 | }
48 |
49 | @Override
50 | public int getItemCount() {
51 | return 3;
52 | }
53 | });
54 |
55 | new TabLayoutMediator(mTabLayout, viewPager2, (tab, position) -> {
56 | tab.setText("页面" + position);
57 | }).attach();
58 |
59 |
60 | mBanner.addBannerLifecycleObserver(this)
61 | .setAdapter(new ImageAdapter(DataBean.getTestData()))
62 | .setIntercept(false)
63 | .setIndicator(new CircleIndicator(this));
64 | }
65 | }
66 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/util/ParentRecyclerView.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.util;
2 |
3 | import android.content.Context;
4 | import android.util.AttributeSet;
5 | import android.view.MotionEvent;
6 |
7 | import androidx.annotation.NonNull;
8 | import androidx.annotation.Nullable;
9 | import androidx.recyclerview.widget.RecyclerView;
10 |
11 | public class ParentRecyclerView extends RecyclerView {
12 | public ParentRecyclerView(@NonNull Context context) {
13 | super(context);
14 | }
15 |
16 | public ParentRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs) {
17 | super(context, attrs);
18 | }
19 |
20 | public ParentRecyclerView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
21 | super(context, attrs, defStyleAttr);
22 | }
23 |
24 | private float mStartX, mStartY;
25 |
26 | @Override
27 | public boolean dispatchTouchEvent(MotionEvent ev) {
28 | switch (ev.getAction()) {
29 | case MotionEvent.ACTION_DOWN:
30 | mStartX = ev.getX();
31 | mStartY = ev.getY();
32 | break;
33 | case MotionEvent.ACTION_MOVE:
34 | float endX = ev.getX();
35 | float endY = ev.getY();
36 | float distanceX = Math.abs(endX - mStartX);
37 | float distanceY = Math.abs(endY - mStartY);
38 | getParent().requestDisallowInterceptTouchEvent(!(distanceX > 4 && distanceX > distanceY));
39 | break;
40 | }
41 | return super.dispatchTouchEvent(ev);
42 | }
43 | }
44 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/viewholder/ImageHolder.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.viewholder;
2 |
3 | import android.view.View;
4 | import android.widget.ImageView;
5 |
6 | import androidx.annotation.NonNull;
7 | import androidx.recyclerview.widget.RecyclerView;
8 |
9 | public class ImageHolder extends RecyclerView.ViewHolder {
10 | public ImageView imageView;
11 |
12 | public ImageHolder(@NonNull View view) {
13 | super(view);
14 | this.imageView = (ImageView) view;
15 | }
16 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/viewholder/ImageTitleHolder.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.viewholder;
2 |
3 | import android.view.View;
4 | import android.widget.ImageView;
5 | import android.widget.TextView;
6 |
7 | import androidx.annotation.NonNull;
8 | import androidx.recyclerview.widget.RecyclerView;
9 |
10 | import com.test.banner.R;
11 |
12 | public class ImageTitleHolder extends RecyclerView.ViewHolder {
13 | public ImageView imageView;
14 | public TextView title;
15 |
16 | public ImageTitleHolder(@NonNull View view) {
17 | super(view);
18 | imageView = view.findViewById(R.id.image);
19 | title = view.findViewById(R.id.bannerTitle);
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/viewholder/TitleHolder.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.viewholder;
2 |
3 | import android.view.View;
4 | import android.widget.ImageView;
5 | import android.widget.TextView;
6 |
7 | import androidx.annotation.NonNull;
8 | import androidx.recyclerview.widget.RecyclerView;
9 |
10 | import com.test.banner.R;
11 |
12 | public class TitleHolder extends RecyclerView.ViewHolder {
13 | public TextView title;
14 |
15 | public TitleHolder(@NonNull View view) {
16 | super(view);
17 | title = view.findViewById(R.id.bannerTitle);
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/app/src/main/java/com/test/banner/viewholder/VideoHolder.java:
--------------------------------------------------------------------------------
1 | package com.test.banner.viewholder;
2 |
3 | import android.view.View;
4 |
5 | import androidx.annotation.NonNull;
6 | import androidx.recyclerview.widget.RecyclerView;
7 |
8 | import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;
9 | import com.test.banner.R;
10 |
11 | public class VideoHolder extends RecyclerView.ViewHolder {
12 | public StandardGSYVideoPlayer player;
13 |
14 | public VideoHolder(@NonNull View view) {
15 | super(view);
16 | player = view.findViewById(R.id.player);
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/home_hotpoint.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/drawable-xhdpi/home_hotpoint.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/home_right_arrow.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/drawable-xhdpi/home_right_arrow.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/image1.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/drawable-xhdpi/image1.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/image10.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/drawable-xhdpi/image10.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/image11.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/drawable-xhdpi/image11.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/image2.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/drawable-xhdpi/image2.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/image3.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/drawable-xhdpi/image3.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/image4.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/drawable-xhdpi/image4.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/image5.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/drawable-xhdpi/image5.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/image6.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/drawable-xhdpi/image6.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/image7.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/drawable-xhdpi/image7.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/image8.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/drawable-xhdpi/image8.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/image9.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/drawable-xhdpi/image9.jpg
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/indicator_normal.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/drawable-xhdpi/indicator_normal.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xhdpi/indicator_selected.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/drawable-xhdpi/indicator_selected.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable/background.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/default_selecter.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/green.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/loading.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/drawable/loading.gif
--------------------------------------------------------------------------------
/app/src/main/res/drawable/selected_radius.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/unselected_radius.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/white.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_constraint_layout_banner.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
20 |
21 |
29 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_gallery.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
16 |
17 |
25 |
26 |
33 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
12 |
13 |
21 |
22 |
29 |
30 |
36 |
37 |
43 |
44 |
49 |
50 |
56 |
57 |
63 |
64 |
71 |
72 |
79 |
80 |
87 |
88 |
89 |
96 |
97 |
104 |
105 |
112 |
113 |
120 |
121 |
128 |
129 |
136 |
137 |
144 |
145 |
146 |
147 |
148 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_recyclerview_banner.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
13 |
14 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_t_v.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
19 |
20 |
29 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_tou_tiao.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
20 |
21 |
29 |
30 |
37 |
46 |
47 |
48 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_video.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_vp2_fragment_recyclerview.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
12 |
13 |
14 |
17 |
18 |
23 |
24 |
28 |
29 |
33 |
34 |
35 |
36 |
40 |
41 |
51 |
52 |
53 |
54 |
61 |
62 |
63 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/banner.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/banner_image.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/banner_image_title.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
11 |
12 |
25 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/banner_image_title_num.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
11 |
12 |
20 |
21 |
33 |
34 |
41 |
42 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/banner_title.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
13 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/banner_video.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
10 |
11 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item.xml:
--------------------------------------------------------------------------------
1 |
2 |
15 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/test.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/top_line_item2.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
14 |
25 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/values-w820dp/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 64dp
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/arrays.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 | - http://ww4.sinaimg.cn/large/006uZZy8jw1faic1xjab4j30ci08cjrv.jpg
5 | - http://ww4.sinaimg.cn/large/006uZZy8jw1faic21363tj30ci08ct96.jpg
6 | - http://ww4.sinaimg.cn/large/006uZZy8jw1faic259ohaj30ci08c74r.jpg
7 | - http://ww4.sinaimg.cn/large/006uZZy8jw1faic2b16zuj30ci08cwf4.jpg
8 | - http://ww4.sinaimg.cn/large/006uZZy8jw1faic2e7vsaj30ci08cglz.jpg
9 |
10 |
11 |
12 | - http://img.zcool.cn/community/01700557a7f42f0000018c1bd6eb23.jpg
13 |
14 |
15 | - http://img.zcool.cn/community/01d28457d621800000018c1bb7877e.jpg
16 | - http://img.zcool.cn/community/01ae5656e1427f6ac72531cb72bac5.jpg
17 |
18 |
19 | - http://img.zcool.cn/community/01b72057a7e0790000018c1bf4fce0.png
20 | - http://img.zcool.cn/community/01fca557a7f5f90000012e7e9feea8.jpg
21 | - http://img.zcool.cn/community/01996b57a7f6020000018c1bedef97.jpg
22 | - http://img.zcool.cn/community/01700557a7f42f0000018c1bd6eb23.jpg
23 |
24 |
25 |
26 | - http://bpic.588ku.com/element_origin_min_pic/00/00/05/115732f19cc0079.jpg
27 | - http://bpic.588ku.com/element_origin_min_pic/00/00/05/115732f1ac12d1d.jpg
28 | - http://bpic.588ku.com/element_origin_min_pic/00/00/05/115732f1bad97d1.jpg
29 | - http://bpic.588ku.com/element_origin_min_pic/00/00/05/115732f1c83c228.jpg
30 | - http://bpic.588ku.com/element_origin_min_pic/00/00/05/115732f1d53e3dd.jpg
31 | - http://bpic.588ku.com/element_origin_min_pic/00/00/05/115732f1e37fea9.jpg
32 | - http://bpic.588ku.com/element_origin_min_pic/00/00/05/115732f1ef4d709.jpg
33 | - http://bpic.588ku.com/element_origin_min_pic/00/00/05/115732f20b3ea10.jpg
34 | - http://bpic.588ku.com/element_origin_min_pic/00/00/05/115732f21927f8d.jpg
35 |
36 |
37 | - 51巅峰钜惠
38 | - 十大星级品牌联盟,全场2折起
39 | - 生命不是要超越别人,而是要超越自己。
40 | - 己所不欲,勿施于人。——孔子
41 | - 嗨购5折不要停
42 |
43 |
44 | - banner动画预览
45 | - banner内置样式预览
46 | - banner指示器位置设置预览
47 | - banner一些自定义样式方法预览
48 | - banner加载本地图片
49 | - banner自定义布局文件(这里通过修改ViewPager举一反三吧)
50 | - !!banner更多用法请看文档,这里就不一一列举了!
51 |
52 |
53 | - Default
54 | - Accordion
55 | - BackgroundToForeground
56 | - ForegroundToBackground
57 | - CubeIn
58 | - CubeOut
59 | - DepthPage
60 | - FlipHorizontal
61 | - FlipVertical
62 | - RotateDown
63 | - RotateUp
64 | - ScaleInOut
65 | - Stack
66 | - Tablet
67 | - ZoomIn
68 | - ZoomOut
69 | - ZoomOutSlide
70 |
71 |
72 | - NOT_INDICATOR
73 | - CIRCLE_INDICATOR
74 | - NUM_INDICATOR
75 | - NUM_INDICATOR_TITLE
76 | - CIRCLE_INDICATOR_TITLE
77 | - CIRCLE_INDICATOR_TITLE_INSIDE
78 |
79 |
80 | - LEFT
81 | - CENTER
82 | - RIGHT
83 |
84 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | @color/main_color
4 | @color/main_color
5 | @color/main_color
6 |
7 | #BDBDBD
8 |
9 | #5CB85C
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 16dp
4 | 16dp
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Banner
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/banner/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/banner/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'maven-publish'
3 | apply plugin: 'signing'
4 |
5 | version = '2.2.3'
6 |
7 | android {
8 | compileSdk 33
9 |
10 | defaultConfig {
11 | minSdk 14
12 | targetSdk 33
13 | versionCode 202003
14 | versionName version
15 | consumerProguardFiles 'proguard-rules.pro'
16 | }
17 |
18 | lintOptions {
19 | abortOnError false
20 | }
21 |
22 | compileOptions {
23 | sourceCompatibility JavaVersion.VERSION_1_8
24 | targetCompatibility JavaVersion.VERSION_1_8
25 | }
26 | }
27 |
28 | dependencies {
29 | implementation "androidx.viewpager2:viewpager2:1.1.0-beta02"
30 | }
31 |
32 | task androidSourcesJar(type: Jar) {
33 | archiveClassifier.set("sources")
34 | from android.sourceSets.main.java.source
35 | exclude "**/R.class"
36 | exclude "**/BuildConfig.class"
37 | }
38 |
39 | publishing {
40 | publications {
41 | release(MavenPublication) {
42 | // group id,发布后引用的依赖的 group id
43 | groupId 'io.github.youth5201314'
44 | // 发布后引用的依赖的 artifact id
45 | artifactId 'banner'
46 | // 发布的版本
47 | version version
48 | // 发布的 arr 的文件和源码文件
49 | artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
50 | artifact androidSourcesJar
51 | pom {
52 | // 构件名称,可以自定义
53 | name = 'banner'
54 | // 构件描述
55 | description = 'Android Banner 2.0 全新轮播控件,采用viewpager2为基础控件,支持AndroidX'
56 | // 构件主页
57 | url = 'https://github.com/youth5201314/banner'
58 | // 许可证名称和地址
59 | licenses {
60 | license {
61 | name = 'The Apache License, Version 2.0'
62 | url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
63 | }
64 | }
65 | // 开发者信息
66 | developers {
67 | developer {
68 | name = '春'
69 | email = '1028729086@qq.com'
70 | }
71 | }
72 | // 版本控制仓库地址
73 | scm {
74 | url = 'https://github.com/youth5201314/banner'
75 | connection = 'scm:git:github.com/youth5201314/banner.git'
76 | developerConnection = 'scm:git:ssh://git@github.com/youth5201314/banner.git'
77 | }
78 | }
79 | }
80 | }
81 | repositories {
82 | maven {
83 | // 发布的位置,这里根据发布的版本区分了 SNAPSHOT 和最终版本两种情况
84 | def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
85 | def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
86 | url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
87 | credentials {
88 | // 这里就是之前在 issues.sonatype.org 注册的账号
89 | username sonatypeUsername
90 | password sonatypePassword
91 | }
92 | }
93 | }
94 | }
95 |
96 | signing {
97 | sign publishing.publications
98 | }
99 |
100 |
--------------------------------------------------------------------------------
/banner/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | -dontwarn androidx.viewpager2.**
2 | -keep class androidx.viewpager2.** {*;}
3 | -dontwarn androidx.recyclerview.widget.RecyclerView
4 | -keep class androidx.recyclerview.widget.RecyclerView{*;}
5 | -dontwarn com.youth.banner.**
6 | -keep class com.youth.banner.** {*;}
7 |
8 |
--------------------------------------------------------------------------------
/banner/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/adapter/BannerAdapter.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.adapter;
2 |
3 | import android.view.ViewGroup;
4 |
5 | import androidx.annotation.NonNull;
6 | import androidx.recyclerview.widget.RecyclerView;
7 |
8 | import com.youth.banner.R;
9 | import com.youth.banner.config.BannerConfig;
10 | import com.youth.banner.holder.IViewHolder;
11 | import com.youth.banner.listener.OnBannerListener;
12 | import com.youth.banner.util.BannerUtils;
13 |
14 | import java.util.ArrayList;
15 | import java.util.List;
16 |
17 | import androidx.annotation.NonNull;
18 | import androidx.recyclerview.widget.RecyclerView;
19 |
20 |
21 | public abstract class BannerAdapter extends RecyclerView.Adapter implements IViewHolder {
22 | protected List mDatas = new ArrayList<>();
23 | private OnBannerListener mOnBannerListener;
24 | private VH mViewHolder;
25 | private int mIncreaseCount = BannerConfig.INCREASE_COUNT;
26 |
27 | public BannerAdapter(List datas) {
28 | setDatas(datas);
29 | }
30 |
31 | /**
32 | * 设置实体集合(可以在自己的adapter自定义,不一定非要使用)
33 | *
34 | * @param datas
35 | */
36 | public void setDatas(List datas) {
37 | if (datas == null) {
38 | datas = new ArrayList<>();
39 | }
40 | mDatas.clear();
41 | mDatas.addAll(datas);
42 | notifyDataSetChanged();
43 | }
44 |
45 | /**
46 | * 获取指定的实体(可以在自己的adapter自定义,不一定非要使用)
47 | *
48 | * @param position 真实的position
49 | * @return
50 | */
51 | public T getData(int position) {
52 | if (position > mDatas.size()-1) {
53 | return null;
54 | }
55 | return mDatas.get(position);
56 | }
57 |
58 | /**
59 | * 获取指定的实体(可以在自己的adapter自定义,不一定非要使用)
60 | *
61 | * @param position 这里传的position不是真实的,获取时转换了一次
62 | * @return
63 | */
64 | public T getRealData(int position) {
65 | int realPosition = getRealPosition(position);
66 | if (realPosition > mDatas.size()-1) {
67 | return null;
68 | }
69 | return mDatas.get(realPosition);
70 | }
71 |
72 |
73 | @Override
74 | public final void onBindViewHolder(@NonNull VH holder, int position) {
75 | mViewHolder = holder;
76 | int real = getRealPosition(position);
77 | T data = mDatas.get(real);
78 | holder.itemView.setTag(R.id.banner_data_key, data);
79 | holder.itemView.setTag(R.id.banner_pos_key, real);
80 | onBindView(holder, mDatas.get(real), real, getRealCount());
81 | if (mOnBannerListener != null) {
82 | holder.itemView.setOnClickListener(view -> mOnBannerListener.OnBannerClick(data, real));
83 | }
84 | }
85 |
86 | @NonNull
87 | @Override
88 | @SuppressWarnings("unchecked")
89 | public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
90 | VH vh = onCreateHolder(parent, viewType);
91 | vh.itemView.setOnClickListener(v -> {
92 | if (mOnBannerListener != null) {
93 | T data = (T) vh.itemView.getTag(R.id.banner_data_key);
94 | int real = (int) vh.itemView.getTag(R.id.banner_pos_key);
95 | mOnBannerListener.OnBannerClick(data, real);
96 | }
97 | });
98 | return vh;
99 | }
100 |
101 | @Override
102 | public int getItemCount() {
103 | return getRealCount() > 1 ? getRealCount() + mIncreaseCount : getRealCount();
104 | }
105 |
106 | public int getRealCount() {
107 | return mDatas == null ? 0 : mDatas.size();
108 | }
109 |
110 | public int getRealPosition(int position) {
111 | return BannerUtils.getRealPosition(mIncreaseCount == BannerConfig.INCREASE_COUNT, position, getRealCount());
112 | }
113 |
114 | public void setOnBannerListener(OnBannerListener listener) {
115 | this.mOnBannerListener = listener;
116 | }
117 |
118 | public VH getViewHolder() {
119 | return mViewHolder;
120 | }
121 |
122 | public void setIncreaseCount(int increaseCount) {
123 | this.mIncreaseCount = increaseCount;
124 | }
125 | }
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/adapter/BannerImageAdapter.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.adapter;
2 |
3 | import android.view.ViewGroup;
4 | import android.widget.ImageView;
5 |
6 | import com.youth.banner.holder.BannerImageHolder;
7 |
8 | import java.util.List;
9 |
10 | /**
11 | * 默认实现的图片适配器,图片加载需要自己实现
12 | */
13 | public abstract class BannerImageAdapter extends BannerAdapter {
14 |
15 | public BannerImageAdapter(List mData) {
16 | super(mData);
17 | }
18 |
19 | @Override
20 | public BannerImageHolder onCreateHolder(ViewGroup parent, int viewType) {
21 | ImageView imageView = new ImageView(parent.getContext());
22 | //注意,必须设置为match_parent,这个是viewpager2强制要求的
23 | ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
24 | ViewGroup.LayoutParams.MATCH_PARENT,
25 | ViewGroup.LayoutParams.MATCH_PARENT);
26 | imageView.setLayoutParams(params);
27 | imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
28 | return new BannerImageHolder(imageView);
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/config/BannerConfig.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.config;
2 |
3 | import com.youth.banner.util.BannerUtils;
4 |
5 | /**
6 | * 不忘初心
7 | *
8 | * ┌───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┬───┐ ┌───┬───┬───┐ ┌┐ ┌┐ ┌┐
9 | * │Esc│ │ F1│ F2│ F3│ F4│ │ F5│ F6│ F7│ F8│ │ F9│F10│F11│F12│ │P/S│S L│P/B│ └┘ └┘ └┘
10 | * └───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┴───┘ └───┴───┴───┘
11 | * ┌───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───┬───────┐ ┌───┬───┬───┐ ┌───┬───┬───┬───┐
12 | * │~ `│! 1│@ 2│# 3│$ 4│% 5│^ 6│& 7│* 8│( 9│) 0│_ -│+ =│ BacSp │ │Ins│Hom│PUp│ │N L│ / │ * │ - │
13 | * ├───┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─────┤ ├───┼───┼───┤ ├───┼───┼───┼───┤
14 | * │ Tab │ Q │ W │ E │ R │ T │ Y │ U │ I │ O │ P │{ [│} ]│ | \ │ │Del│End│PDn│ │ 7 │ 8 │ 9 │ │
15 | * ├─────┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴┬──┴─────┤ └───┴───┴───┘ ├───┼───┼───┤ + │
16 | * │ Caps │ A │ S │ D │ F │ G │ H │ J │ K │ L │: ;│" '│ Enter │ │ 4 │ 5 │ 6 │ │
17 | * ├──────┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴─┬─┴────────┤ ┌───┐ ├───┼───┼───┼───┤
18 | * │ Shift │ Z │ X │ C │ V │ B │ N │ M │< ,│> .│? /│ Shift │ │ ↑ │ │ 1 │ 2 │ 3 │ │
19 | * ├─────┬──┴─┬─┴──┬┴───┴───┴───┴───┴───┴──┬┴───┼───┴┬────┬────┤ ┌───┼───┼───┐ ├───┴───┼───┤ E││
20 | * │ Ctrl│ │Alt │ Space │ Alt│ │ │Ctrl│ │ ← │ ↓ │ → │ │ 0 │ . │←─┘│
21 | * └─────┴────┴────┴───────────────────────┴────┴────┴────┴────┘ └───┴───┴───┘ └───────┴───┴───┘
22 | *
23 | * @author youth5201314/spring
24 | * @date 2020/1/24
25 | * banner 配置参数
26 | */
27 | public class BannerConfig {
28 | public static final boolean IS_AUTO_LOOP = true;
29 | public static final boolean IS_INFINITE_LOOP = true;
30 | public static final int LOOP_TIME = 3000;
31 | public static final int SCROLL_TIME = 600;
32 | public static final int INCREASE_COUNT = 2;
33 | public static final int INDICATOR_NORMAL_COLOR = 0x88ffffff;
34 | public static final int INDICATOR_SELECTED_COLOR = 0x88000000;
35 | public static final int INDICATOR_NORMAL_WIDTH = (int) BannerUtils.dp2px(5);
36 | public static final int INDICATOR_SELECTED_WIDTH = (int) BannerUtils.dp2px(7);
37 | public static final int INDICATOR_SPACE = (int) BannerUtils.dp2px(5);
38 | public static final int INDICATOR_MARGIN = (int) BannerUtils.dp2px(5);
39 |
40 | public static final int INDICATOR_HEIGHT = (int) BannerUtils.dp2px(3);
41 | public static final int INDICATOR_RADIUS = (int) BannerUtils.dp2px(3);
42 |
43 | }
44 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/config/IndicatorConfig.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.config;
2 |
3 | import androidx.annotation.ColorInt;
4 | import androidx.annotation.IntDef;
5 |
6 | import java.lang.annotation.Retention;
7 | import java.lang.annotation.RetentionPolicy;
8 |
9 | public class IndicatorConfig {
10 |
11 | private int indicatorSize;
12 | private int currentPosition;
13 | private int gravity = Direction.CENTER;
14 | private int indicatorSpace = BannerConfig.INDICATOR_SPACE;
15 | private int normalWidth = BannerConfig.INDICATOR_NORMAL_WIDTH;
16 | private int selectedWidth = BannerConfig.INDICATOR_SELECTED_WIDTH;
17 | @ColorInt
18 | private int normalColor = BannerConfig.INDICATOR_NORMAL_COLOR;
19 | @ColorInt
20 | private int selectedColor = BannerConfig.INDICATOR_SELECTED_COLOR;
21 |
22 | private int radius = BannerConfig.INDICATOR_RADIUS;
23 | private int height = BannerConfig.INDICATOR_HEIGHT;
24 |
25 | private Margins margins;
26 |
27 | //是将指示器添加到banner上
28 | private boolean attachToBanner = true;
29 |
30 | @IntDef({Direction.LEFT, Direction.CENTER, Direction.RIGHT})
31 | @Retention(RetentionPolicy.SOURCE)
32 | public @interface Direction {
33 | int LEFT = 0;
34 | int CENTER = 1;
35 | int RIGHT = 2;
36 | }
37 |
38 | public static class Margins {
39 | public int leftMargin;
40 | public int topMargin;
41 | public int rightMargin;
42 | public int bottomMargin;
43 |
44 | public Margins() {
45 | this(BannerConfig.INDICATOR_MARGIN);
46 | }
47 |
48 | public Margins(int marginSize) {
49 | this(marginSize, marginSize, marginSize, marginSize);
50 | }
51 |
52 | public Margins(int leftMargin, int topMargin, int rightMargin, int bottomMargin) {
53 | this.leftMargin = leftMargin;
54 | this.topMargin = topMargin;
55 | this.rightMargin = rightMargin;
56 | this.bottomMargin = bottomMargin;
57 | }
58 | }
59 |
60 | public Margins getMargins() {
61 | if (margins == null) {
62 | setMargins(new Margins());
63 | }
64 | return margins;
65 | }
66 |
67 | public IndicatorConfig setMargins(Margins margins) {
68 | this.margins = margins;
69 | return this;
70 | }
71 |
72 | public int getIndicatorSize() {
73 | return indicatorSize;
74 | }
75 |
76 | public IndicatorConfig setIndicatorSize(int indicatorSize) {
77 | this.indicatorSize = indicatorSize;
78 | return this;
79 | }
80 |
81 | public int getNormalColor() {
82 | return normalColor;
83 | }
84 |
85 | public IndicatorConfig setNormalColor(int normalColor) {
86 | this.normalColor = normalColor;
87 | return this;
88 | }
89 |
90 | public int getSelectedColor() {
91 | return selectedColor;
92 | }
93 |
94 | public IndicatorConfig setSelectedColor(int selectedColor) {
95 | this.selectedColor = selectedColor;
96 | return this;
97 | }
98 |
99 | public int getIndicatorSpace() {
100 | return indicatorSpace;
101 | }
102 |
103 | public IndicatorConfig setIndicatorSpace(int indicatorSpace) {
104 | this.indicatorSpace = indicatorSpace;
105 | return this;
106 | }
107 |
108 | public int getCurrentPosition() {
109 | return currentPosition;
110 | }
111 |
112 | public IndicatorConfig setCurrentPosition(int currentPosition) {
113 | this.currentPosition = currentPosition;
114 | return this;
115 | }
116 |
117 | public int getNormalWidth() {
118 | return normalWidth;
119 | }
120 |
121 | public IndicatorConfig setNormalWidth(int normalWidth) {
122 | this.normalWidth = normalWidth;
123 | return this;
124 | }
125 |
126 | public int getSelectedWidth() {
127 | return selectedWidth;
128 | }
129 |
130 | public IndicatorConfig setSelectedWidth(int selectedWidth) {
131 | this.selectedWidth = selectedWidth;
132 | return this;
133 | }
134 |
135 | public int getGravity() {
136 | return gravity;
137 | }
138 |
139 | public IndicatorConfig setGravity(@Direction int gravity) {
140 | this.gravity = gravity;
141 | return this;
142 | }
143 |
144 | public boolean isAttachToBanner() {
145 | return attachToBanner;
146 | }
147 |
148 | public IndicatorConfig setAttachToBanner(boolean attachToBanner) {
149 | this.attachToBanner = attachToBanner;
150 | return this;
151 | }
152 |
153 | public int getRadius() {
154 | return radius;
155 | }
156 |
157 | public IndicatorConfig setRadius(int radius) {
158 | this.radius = radius;
159 | return this;
160 | }
161 |
162 | public int getHeight() {
163 | return height;
164 | }
165 |
166 | public IndicatorConfig setHeight(int height) {
167 | this.height = height;
168 | return this;
169 | }
170 | }
171 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/holder/BannerImageHolder.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.holder;
2 |
3 | import android.view.View;
4 | import android.widget.ImageView;
5 |
6 | import androidx.annotation.NonNull;
7 | import androidx.recyclerview.widget.RecyclerView;
8 |
9 | public
10 | class BannerImageHolder extends RecyclerView.ViewHolder {
11 | public ImageView imageView;
12 |
13 | public BannerImageHolder(@NonNull View view) {
14 | super(view);
15 | this.imageView = (ImageView) view;
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/holder/IViewHolder.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.holder;
2 |
3 | import android.view.ViewGroup;
4 |
5 | public interface IViewHolder {
6 |
7 | /**
8 | * 创建ViewHolder
9 | *
10 | * @return XViewHolder
11 | */
12 | VH onCreateHolder(ViewGroup parent, int viewType);
13 |
14 | /**
15 | * 绑定布局数据
16 | *
17 | * @param holder XViewHolder
18 | * @param data 数据实体
19 | * @param position 当前位置
20 | * @param size 总数
21 | */
22 | void onBindView(VH holder, T data, int position, int size);
23 |
24 | }
25 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/indicator/BaseIndicator.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.indicator;
2 |
3 | import android.content.Context;
4 | import android.graphics.Color;
5 | import android.graphics.Paint;
6 | import android.util.AttributeSet;
7 | import android.view.Gravity;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.view.animation.DecelerateInterpolator;
11 | import android.view.animation.Interpolator;
12 | import android.widget.FrameLayout;
13 |
14 | import androidx.annotation.NonNull;
15 | import androidx.annotation.Nullable;
16 |
17 | import com.youth.banner.config.IndicatorConfig;
18 | import com.youth.banner.util.LogUtils;
19 |
20 | public class BaseIndicator extends View implements Indicator {
21 | protected IndicatorConfig config;
22 | protected Paint mPaint;
23 | protected float offset;
24 |
25 | public BaseIndicator(Context context) {
26 | this(context, null);
27 | }
28 |
29 | public BaseIndicator(Context context, @Nullable AttributeSet attrs) {
30 | this(context, attrs, 0);
31 | }
32 |
33 | public BaseIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
34 | super(context, attrs, defStyleAttr);
35 | config = new IndicatorConfig();
36 | mPaint = new Paint();
37 | mPaint.setAntiAlias(true);
38 | mPaint.setColor(Color.TRANSPARENT);
39 | mPaint.setColor(config.getNormalColor());
40 | }
41 |
42 | @NonNull
43 | @Override
44 | public View getIndicatorView() {
45 | if (config.isAttachToBanner()) {
46 | FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(
47 | ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
48 | switch (config.getGravity()) {
49 | case IndicatorConfig.Direction.LEFT:
50 | layoutParams.gravity = Gravity.BOTTOM | Gravity.START;
51 | break;
52 | case IndicatorConfig.Direction.CENTER:
53 | layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
54 | break;
55 | case IndicatorConfig.Direction.RIGHT:
56 | layoutParams.gravity = Gravity.BOTTOM | Gravity.END;
57 | break;
58 | }
59 | layoutParams.leftMargin = config.getMargins().leftMargin;
60 | layoutParams.rightMargin = config.getMargins().rightMargin;
61 | layoutParams.topMargin = config.getMargins().topMargin;
62 | layoutParams.bottomMargin = config.getMargins().bottomMargin;
63 | setLayoutParams(layoutParams);
64 | }
65 | return this;
66 | }
67 |
68 | @Override
69 | public IndicatorConfig getIndicatorConfig() {
70 | return config;
71 | }
72 |
73 | @Override
74 | public void onPageChanged(int count, int currentPosition) {
75 | config.setIndicatorSize(count);
76 | config.setCurrentPosition(currentPosition);
77 | requestLayout();
78 | }
79 |
80 | @Override
81 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
82 | offset = positionOffset;
83 | invalidate();
84 |
85 | }
86 |
87 | @Override
88 | public void onPageSelected(int position) {
89 | config.setCurrentPosition(position);
90 | invalidate();
91 | }
92 |
93 | @Override
94 | public void onPageScrollStateChanged(int state) {
95 |
96 | }
97 | }
98 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/indicator/CircleIndicator.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.indicator;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.util.AttributeSet;
6 |
7 | /**
8 | * 圆形指示器
9 | * 如果想要大小一样,可以将选中和默认设置成同样大小
10 | */
11 | public class CircleIndicator extends BaseIndicator {
12 | private int mNormalRadius;
13 | private int mSelectedRadius;
14 | private int maxRadius;
15 |
16 | public CircleIndicator(Context context) {
17 | this(context, null);
18 | }
19 |
20 | public CircleIndicator(Context context, AttributeSet attrs) {
21 | this(context, attrs, 0);
22 | }
23 |
24 | public CircleIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
25 | super(context, attrs, defStyleAttr);
26 | mNormalRadius = config.getNormalWidth() / 2;
27 | mSelectedRadius = config.getSelectedWidth() / 2;
28 | }
29 |
30 | @Override
31 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
32 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
33 | int count = config.getIndicatorSize();
34 | if (count <= 1) {
35 | return;
36 | }
37 |
38 | mNormalRadius = config.getNormalWidth() / 2;
39 | mSelectedRadius = config.getSelectedWidth() / 2;
40 | //考虑当 选中和默认 的大小不一样的情况
41 | maxRadius = Math.max(mSelectedRadius, mNormalRadius);
42 | //间距*(总数-1)+选中宽度+默认宽度*(总数-1)
43 | int width = (count - 1) * config.getIndicatorSpace() + config.getSelectedWidth() + config.getNormalWidth() * (count - 1);
44 | setMeasuredDimension(width, Math.max(config.getNormalWidth(), config.getSelectedWidth()));
45 | }
46 |
47 | @Override
48 | protected void onDraw(Canvas canvas) {
49 | super.onDraw(canvas);
50 | int count = config.getIndicatorSize();
51 | if (count <= 1) {
52 | return;
53 | }
54 | float left = 0;
55 | for (int i = 0; i < count; i++) {
56 | mPaint.setColor(config.getCurrentPosition() == i ? config.getSelectedColor() : config.getNormalColor());
57 | int indicatorWidth = config.getCurrentPosition() == i ? config.getSelectedWidth() : config.getNormalWidth();
58 | int radius = config.getCurrentPosition() == i ? mSelectedRadius : mNormalRadius;
59 | canvas.drawCircle(left + radius, maxRadius, radius, mPaint);
60 | left += indicatorWidth + config.getIndicatorSpace();
61 | }
62 | // mPaint.setColor(config.getNormalColor());
63 | // for (int i = 0; i < count; i++) {
64 | // canvas.drawCircle(left + maxRadius, maxRadius, mNormalRadius, mPaint);
65 | // left += config.getNormalWidth() + config.getIndicatorSpace();
66 | // }
67 | // mPaint.setColor(config.getSelectedColor());
68 | // left = maxRadius + (config.getNormalWidth() + config.getIndicatorSpace()) * config.getCurrentPosition();
69 | // canvas.drawCircle(left, maxRadius, mSelectedRadius, mPaint);
70 | }
71 |
72 | }
73 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/indicator/DrawableIndicator.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.indicator;
2 |
3 | import android.content.Context;
4 | import android.content.res.TypedArray;
5 | import android.graphics.Bitmap;
6 | import android.graphics.BitmapFactory;
7 | import android.graphics.Canvas;
8 | import android.graphics.Color;
9 | import android.graphics.drawable.BitmapDrawable;
10 | import android.graphics.drawable.Drawable;
11 | import android.util.AttributeSet;
12 |
13 | import androidx.annotation.DrawableRes;
14 | import androidx.annotation.IdRes;
15 |
16 | import com.youth.banner.R;
17 |
18 |
19 | /**
20 | * Drawable指示器
21 | */
22 | public class DrawableIndicator extends BaseIndicator {
23 | private Bitmap normalBitmap;
24 | private Bitmap selectedBitmap;
25 |
26 | /**
27 | * 实例化Drawable指示器 ,也可以通过自定义属性设置
28 | * @param context
29 | * @param normalResId
30 | * @param selectedResId
31 | */
32 | public DrawableIndicator(Context context, @DrawableRes int normalResId, @DrawableRes int selectedResId) {
33 | super(context);
34 | normalBitmap = BitmapFactory.decodeResource(getResources(), normalResId);
35 | selectedBitmap = BitmapFactory.decodeResource(getResources(), selectedResId);
36 | }
37 |
38 | public DrawableIndicator(Context context) {
39 | this(context, null);
40 | }
41 |
42 | public DrawableIndicator(Context context, AttributeSet attrs) {
43 | this(context, attrs, 0);
44 | }
45 |
46 | public DrawableIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
47 | super(context, attrs, defStyleAttr);
48 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DrawableIndicator);
49 | if (a != null) {
50 | BitmapDrawable normal = (BitmapDrawable) a.getDrawable(R.styleable.DrawableIndicator_normal_drawable);
51 | BitmapDrawable selected = (BitmapDrawable) a.getDrawable(R.styleable.DrawableIndicator_selected_drawable);
52 | normalBitmap = normal.getBitmap();
53 | selectedBitmap = selected.getBitmap();
54 | }
55 | }
56 |
57 | @Override
58 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
59 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
60 | int count = config.getIndicatorSize();
61 | if (count <= 1) {
62 | return;
63 | }
64 | setMeasuredDimension(selectedBitmap.getWidth() * (count - 1) + selectedBitmap.getWidth() + config.getIndicatorSpace() * (count - 1),
65 | Math.max(normalBitmap.getHeight(), selectedBitmap.getHeight()));
66 | }
67 |
68 | @Override
69 | protected void onDraw(Canvas canvas) {
70 | super.onDraw(canvas);
71 | int count = config.getIndicatorSize();
72 | if (count <= 1 || normalBitmap == null || selectedBitmap == null) {
73 | return;
74 | }
75 |
76 | float left = 0;
77 | for (int i = 0; i < count; i++) {
78 | canvas.drawBitmap(config.getCurrentPosition() == i ? selectedBitmap : normalBitmap, left, 0, mPaint);
79 | left += normalBitmap.getWidth() + config.getIndicatorSpace();
80 | }
81 | }
82 |
83 |
84 | }
85 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/indicator/Indicator.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.indicator;
2 |
3 | import android.view.View;
4 |
5 | import androidx.annotation.NonNull;
6 |
7 | import com.youth.banner.config.IndicatorConfig;
8 | import com.youth.banner.listener.OnPageChangeListener;
9 |
10 | public interface Indicator extends OnPageChangeListener {
11 | @NonNull
12 | View getIndicatorView();
13 |
14 | IndicatorConfig getIndicatorConfig();
15 |
16 | void onPageChanged(int count, int currentPosition);
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/indicator/RectangleIndicator.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.indicator;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.RectF;
6 | import android.util.AttributeSet;
7 |
8 | import com.youth.banner.config.BannerConfig;
9 |
10 | /**
11 | * 矩形(条形)指示器
12 | * 1、可以设置选中和默认的宽度、指示器的圆角
13 | * 2、如果需要正方形将圆角设置为0,可将宽度和高度设置为一样
14 | * 3、如果不想选中时变长,可将选中的宽度和默认宽度设置为一样
15 | */
16 | public class RectangleIndicator extends BaseIndicator {
17 | RectF rectF;
18 |
19 | public RectangleIndicator(Context context) {
20 | this(context, null);
21 | }
22 |
23 | public RectangleIndicator(Context context, AttributeSet attrs) {
24 | this(context, attrs, 0);
25 | }
26 |
27 | public RectangleIndicator(Context context, AttributeSet attrs, int defStyleAttr) {
28 | super(context, attrs, defStyleAttr);
29 | rectF = new RectF();
30 | }
31 |
32 | @Override
33 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
34 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
35 | int count = config.getIndicatorSize();
36 | if (count <= 1) {
37 | return;
38 | }
39 | //间距*(总数-1)+默认宽度*(总数-1)+选中宽度
40 | int space = config.getIndicatorSpace() * (count - 1);
41 | int normal = config.getNormalWidth() * (count - 1);
42 | setMeasuredDimension(space + normal + config.getSelectedWidth(), config.getHeight());
43 | }
44 |
45 | @Override
46 | protected void onDraw(Canvas canvas) {
47 | super.onDraw(canvas);
48 | int count = config.getIndicatorSize();
49 | if (count <= 1) {
50 | return;
51 | }
52 | float left = 0;
53 | for (int i = 0; i < count; i++) {
54 | mPaint.setColor(config.getCurrentPosition() == i ? config.getSelectedColor() : config.getNormalColor());
55 | int indicatorWidth = config.getCurrentPosition() == i ? config.getSelectedWidth() : config.getNormalWidth();
56 | rectF.set(left, 0, left + indicatorWidth, config.getHeight());
57 | left += indicatorWidth + config.getIndicatorSpace();
58 | canvas.drawRoundRect(rectF, config.getRadius(), config.getRadius(), mPaint);
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/indicator/RoundLinesIndicator.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.indicator;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Paint;
6 | import android.graphics.RectF;
7 | import android.util.AttributeSet;
8 |
9 | import androidx.annotation.Nullable;
10 |
11 | import com.youth.banner.util.BannerUtils;
12 |
13 | public class RoundLinesIndicator extends BaseIndicator {
14 |
15 | public RoundLinesIndicator(Context context) {
16 | this(context, null);
17 | }
18 |
19 | public RoundLinesIndicator(Context context, @Nullable AttributeSet attrs) {
20 | this(context, attrs, 0);
21 | }
22 |
23 | public RoundLinesIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
24 | super(context, attrs, defStyleAttr);
25 | mPaint.setStyle(Paint.Style.FILL);
26 | }
27 |
28 | @Override
29 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
30 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
31 | int count = config.getIndicatorSize();
32 | if (count <= 1) return;
33 | setMeasuredDimension((int) (config.getSelectedWidth() * count), config.getHeight());
34 | }
35 |
36 | @Override
37 | protected void onDraw(Canvas canvas) {
38 | super.onDraw(canvas);
39 | int count = config.getIndicatorSize();
40 | if (count <= 1) return;
41 |
42 | mPaint.setColor(config.getNormalColor());
43 | RectF oval = new RectF(0, 0, canvas.getWidth(), config.getHeight());
44 | canvas.drawRoundRect(oval, config.getRadius(), config.getRadius(), mPaint);
45 |
46 | mPaint.setColor(config.getSelectedColor());
47 | int left = config.getCurrentPosition() * config.getSelectedWidth();
48 | RectF rectF = new RectF(left, 0, left + config.getSelectedWidth(), config.getHeight());
49 | canvas.drawRoundRect(rectF, config.getRadius(), config.getRadius(), mPaint);
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/itemdecoration/MarginDecoration.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.itemdecoration;
2 |
3 | import android.graphics.Rect;
4 | import android.view.View;
5 |
6 | import androidx.annotation.NonNull;
7 | import androidx.annotation.Px;
8 | import androidx.recyclerview.widget.LinearLayoutManager;
9 | import androidx.recyclerview.widget.RecyclerView;
10 |
11 | import com.youth.banner.util.BannerUtils;
12 |
13 |
14 | public class MarginDecoration extends RecyclerView.ItemDecoration {
15 | private int mMarginPx;
16 |
17 | public MarginDecoration(@Px int margin) {
18 | mMarginPx = margin;
19 | }
20 |
21 | @Override
22 | public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent,
23 | @NonNull RecyclerView.State state) {
24 | LinearLayoutManager linearLayoutManager = requireLinearLayoutManager(parent);
25 | if (linearLayoutManager.getOrientation() == LinearLayoutManager.VERTICAL) {
26 | outRect.top = mMarginPx;
27 | outRect.bottom = mMarginPx;
28 | } else {
29 | outRect.left = mMarginPx;
30 | outRect.right = mMarginPx;
31 | }
32 | }
33 |
34 | private LinearLayoutManager requireLinearLayoutManager(@NonNull RecyclerView parent) {
35 | RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
36 | if (layoutManager instanceof LinearLayoutManager) {
37 | return (LinearLayoutManager) layoutManager;
38 | }
39 | throw new IllegalStateException("The layoutManager must be LinearLayoutManager");
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/listener/OnBannerListener.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.listener;
2 |
3 | public interface OnBannerListener {
4 |
5 | /**
6 | * 点击事件
7 | *
8 | * @param data 数据实体
9 | * @param position 当前位置
10 | */
11 | void OnBannerClick(T data, int position);
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/listener/OnPageChangeListener.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.listener;
2 |
3 | import androidx.annotation.Px;
4 | import androidx.viewpager2.widget.ViewPager2;
5 |
6 |
7 | public interface OnPageChangeListener {
8 | /**
9 | * This method will be invoked when the current page is scrolled, either as part
10 | * of a programmatically initiated smooth scroll or a user initiated touch scroll.
11 | *
12 | * @param position Position index of the first page currently being displayed.
13 | * Page position+1 will be visible if positionOffset is nonzero.
14 | * @param positionOffset Value from [0, 1) indicating the offset from the page at position.
15 | * @param positionOffsetPixels Value in pixels indicating the offset from position.
16 | */
17 | void onPageScrolled(int position, float positionOffset, @Px int positionOffsetPixels);
18 |
19 | /**
20 | * This method will be invoked when a new page becomes selected. Animation is not
21 | * necessarily complete.
22 | *
23 | * @param position Position index of the new selected page.
24 | */
25 | void onPageSelected(int position);
26 |
27 | /**
28 | * Called when the scroll state changes. Useful for discovering when the user begins
29 | * dragging, when a fake drag is started, when the pager is automatically settling to the
30 | * current page, or when it is fully stopped/idle. {@code state} can be one of
31 | * {@link ViewPager2.SCROLL_STATE_IDLE},
32 | * {@link ViewPager2.SCROLL_STATE_DRAGGING},
33 | * {@link ViewPager2.SCROLL_STATE_SETTLING}.
34 | */
35 | void onPageScrollStateChanged(@ViewPager2.ScrollState int state);
36 | }
37 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/transformer/AlphaPageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.transformer;
2 |
3 | import android.view.View;
4 |
5 | import androidx.annotation.NonNull;
6 |
7 | public class AlphaPageTransformer extends BasePageTransformer {
8 | private static final float DEFAULT_MIN_ALPHA = 0.5f;
9 | private float mMinAlpha = DEFAULT_MIN_ALPHA;
10 |
11 | public AlphaPageTransformer() {
12 | }
13 |
14 | public AlphaPageTransformer(float minAlpha) {
15 | mMinAlpha = minAlpha;
16 | }
17 |
18 | @Override
19 | public void transformPage(@NonNull View view, float position) {
20 | view.setScaleX(0.999f);//hack
21 |
22 | if (position < -1) { // [-Infinity,-1)
23 | view.setAlpha(mMinAlpha);
24 | } else if (position <= 1) { // [-1,1]
25 | //[0,-1]
26 | if (position < 0) {
27 | //[1,min]
28 | float factor = mMinAlpha + (1 - mMinAlpha) * (1 + position);
29 | view.setAlpha(factor);
30 | } else {//[1,0]
31 | //[min,1]
32 | float factor = mMinAlpha + (1 - mMinAlpha) * (1 - position);
33 | view.setAlpha(factor);
34 | }
35 | } else { // (1,+Infinity]
36 | view.setAlpha(mMinAlpha);
37 | }
38 | }
39 | }
40 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/transformer/BasePageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.transformer;
2 |
3 | import androidx.viewpager2.widget.ViewPager2;
4 |
5 | public abstract class BasePageTransformer implements ViewPager2.PageTransformer {
6 | public static final float DEFAULT_CENTER = 0.5f;
7 |
8 |
9 | }
10 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/transformer/DepthPageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.transformer;
2 |
3 | import android.view.View;
4 |
5 | import androidx.viewpager2.widget.ViewPager2;
6 |
7 | public class DepthPageTransformer extends BasePageTransformer {
8 | private static final float DEFAULT_MIN_SCALE = 0.75f;
9 | private float mMinScale = DEFAULT_MIN_SCALE;
10 |
11 | public DepthPageTransformer() {
12 | }
13 |
14 | public DepthPageTransformer(float minScale) {
15 | this.mMinScale = minScale;
16 | }
17 |
18 | public void transformPage(View view, float position) {
19 | int pageWidth = view.getWidth();
20 |
21 | if (position < -1) { // [-Infinity,-1)
22 | // This page is way off-screen to the left.
23 | view.setAlpha(0f);
24 |
25 | } else if (position <= 0) { // [-1,0]
26 | // Use the default slide transition when moving to the left page
27 | view.setAlpha(1f);
28 | view.setTranslationX(0f);
29 | view.setScaleX(1f);
30 | view.setScaleY(1f);
31 |
32 | } else if (position <= 1) { // (0,1]
33 | //进入页面时
34 | view.setVisibility(View.VISIBLE);
35 | // Fade the page out.
36 | view.setAlpha(1 - position);
37 |
38 | // Counteract the default slide transition
39 | view.setTranslationX(pageWidth * -position);
40 |
41 | // Scale the page down (between MIN_SCALE and 1)
42 | float scaleFactor = mMinScale
43 | + (1 - mMinScale) * (1 - Math.abs(position));
44 | view.setScaleX(scaleFactor);
45 | view.setScaleY(scaleFactor);
46 | //退出页面时
47 | if(position ==1){
48 | view.setVisibility(View.INVISIBLE);
49 | }
50 |
51 | } else { // (1,+Infinity]
52 | // This page is way off-screen to the right.
53 | view.setAlpha(0f);
54 | }
55 | }
56 | }
57 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/transformer/MZScaleInTransformer.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.transformer;
2 |
3 | import android.view.View;
4 | import android.view.ViewParent;
5 |
6 | import androidx.annotation.NonNull;
7 | import androidx.recyclerview.widget.RecyclerView;
8 | import androidx.viewpager2.widget.ViewPager2;
9 |
10 | /**
11 | * 内部实现魅族效果使用的,单独使用可能效果不一定好,自己可以尝试下看看是否满意,推荐使用ScaleInTransformer
12 | */
13 | public class MZScaleInTransformer extends BasePageTransformer {
14 | private static final float DEFAULT_MIN_SCALE = 0.85f;
15 | private float mMinScale = DEFAULT_MIN_SCALE;
16 |
17 | public MZScaleInTransformer() {
18 | }
19 |
20 | public MZScaleInTransformer(float minScale) {
21 | this.mMinScale = minScale;
22 | }
23 |
24 | @Override
25 | public void transformPage(@NonNull View view, float position) {
26 | ViewPager2 viewPager = requireViewPager(view);
27 | float paddingLeft = viewPager.getPaddingLeft();
28 | float paddingRight = viewPager.getPaddingRight();
29 | float width = viewPager.getMeasuredWidth();
30 | float offsetPosition = paddingLeft / (width - paddingLeft - paddingRight);
31 | float currentPos = position - offsetPosition;
32 | float reduceX = 0;
33 | float itemWidth = view.getWidth();
34 | //由于左右边的缩小而减小的x的大小的一半
35 | reduceX = (1.0f - mMinScale) * itemWidth / 2.0f;
36 | if (currentPos <= -1.0f) {
37 | view.setTranslationX(reduceX);
38 | view.setScaleX(mMinScale);
39 | view.setScaleY(mMinScale);
40 | } else if (currentPos <= 1.0) {
41 | float scale = (1.0f - mMinScale) * Math.abs(1.0f - Math.abs(currentPos));
42 | float translationX = currentPos * -reduceX;
43 | if (currentPos <= -0.5) {//两个view中间的临界,这时两个view在同一层,左侧View需要往X轴正方向移动覆盖的值()
44 | view.setTranslationX(translationX + Math.abs(Math.abs(currentPos) - 0.5f) / 0.5f);
45 | } else if (currentPos <= 0.0f) {
46 | view.setTranslationX(translationX);
47 | } else if (currentPos >= 0.5) {//两个view中间的临界,这时两个view在同一层
48 | view.setTranslationX(translationX - Math.abs(Math.abs(currentPos) - 0.5f) / 0.5f);
49 | } else {
50 | view.setTranslationX(translationX);
51 | }
52 | view.setScaleX(scale + mMinScale);
53 | view.setScaleY(scale + mMinScale);
54 | } else {
55 | view.setScaleX(mMinScale);
56 | view.setScaleY(mMinScale);
57 | view.setTranslationX(-reduceX);
58 | }
59 |
60 | }
61 |
62 | private ViewPager2 requireViewPager(@NonNull View page) {
63 | ViewParent parent = page.getParent();
64 | ViewParent parentParent = parent.getParent();
65 |
66 | if (parent instanceof RecyclerView && parentParent instanceof ViewPager2) {
67 | return (ViewPager2) parentParent;
68 | }
69 |
70 | throw new IllegalStateException(
71 | "Expected the page view to be managed by a ViewPager2 instance.");
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/transformer/RotateDownPageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.transformer;
2 |
3 | import android.view.View;
4 |
5 | import androidx.annotation.NonNull;
6 |
7 | public class RotateDownPageTransformer extends BasePageTransformer {
8 | private static final float DEFAULT_MAX_ROTATE = 15.0f;
9 | private float mMaxRotate = DEFAULT_MAX_ROTATE;
10 |
11 | public RotateDownPageTransformer() {
12 | }
13 |
14 | public RotateDownPageTransformer(float maxRotate) {
15 | mMaxRotate = maxRotate;
16 | }
17 |
18 | @Override
19 | public void transformPage(@NonNull View view, float position) {
20 | if (position < -1) {
21 | // [-Infinity,-1)
22 | // This page is way off-screen to the left.
23 | view.setRotation(mMaxRotate * -1);
24 | view.setPivotX(view.getWidth());
25 | view.setPivotY(view.getHeight());
26 |
27 | } else if (position <= 1) { // [-1,1]
28 | if (position < 0) {//[0,-1]
29 | view.setPivotX(view.getWidth() * (DEFAULT_CENTER + DEFAULT_CENTER * (-position)));
30 | view.setPivotY(view.getHeight());
31 | view.setRotation(mMaxRotate * position);
32 | } else {//[1,0]
33 | view.setPivotX(view.getWidth() * DEFAULT_CENTER * (1 - position));
34 | view.setPivotY(view.getHeight());
35 | view.setRotation(mMaxRotate * position);
36 | }
37 | } else {
38 | // (1,+Infinity]
39 | // This page is way off-screen to the right.
40 | view.setRotation(mMaxRotate);
41 | view.setPivotX(view.getWidth() * 0);
42 | view.setPivotY(view.getHeight());
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/transformer/RotateUpPageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.transformer;
2 |
3 | import android.view.View;
4 |
5 | import androidx.annotation.NonNull;
6 |
7 | public class RotateUpPageTransformer extends BasePageTransformer {
8 | private static final float DEFAULT_MAX_ROTATE = 15.0f;
9 | private float mMaxRotate = DEFAULT_MAX_ROTATE;
10 |
11 | public RotateUpPageTransformer() {
12 | }
13 |
14 | public RotateUpPageTransformer(float maxRotate) {
15 | mMaxRotate = maxRotate;
16 | }
17 |
18 | @Override
19 | public void transformPage(@NonNull View view, float position) {
20 | if (position < -1) { // [-Infinity,-1)
21 | // This page is way off-screen to the left.
22 | view.setRotation(mMaxRotate);
23 | view.setPivotX(view.getWidth());
24 | view.setPivotY(0);
25 | } else if (position <= 1) { // a页滑动至b页 ; a页从 0.0 ~ -1 ;b页从1 ~ 0.0
26 | // [-1,1]
27 | // Modify the default slide transition to shrink the page as well
28 | if (position < 0) {//[0,-1]
29 | view.setPivotX(view.getWidth() * (0.5f + 0.5f * (-position)));
30 | view.setPivotY(0);
31 | view.setRotation(-mMaxRotate * position);
32 | } else {//[1,0]
33 | view.setPivotX(view.getWidth() * 0.5f * (1 - position));
34 | view.setPivotY(0);
35 | view.setRotation(-mMaxRotate * position);
36 | }
37 | } else { // (1,+Infinity]
38 | // This page is way off-screen to the right.
39 | // ViewHelper.setRotation(view, ROT_MAX);
40 | view.setRotation(-mMaxRotate);
41 | view.setPivotX(0);
42 | view.setPivotY(0);
43 | }
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/transformer/RotateYTransformer.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.transformer;
2 |
3 | import android.view.View;
4 |
5 | import androidx.annotation.NonNull;
6 |
7 | public class RotateYTransformer extends BasePageTransformer {
8 | private static final float DEFAULT_MAX_ROTATE = 35f;
9 | private float mMaxRotate = DEFAULT_MAX_ROTATE;
10 |
11 | public RotateYTransformer() {
12 | }
13 |
14 | public RotateYTransformer(float maxRotate) {
15 | mMaxRotate = maxRotate;
16 | }
17 |
18 | @Override
19 | public void transformPage(@NonNull View view, float position) {
20 | view.setPivotY(view.getHeight()/2);
21 |
22 | if (position < -1) { // [-Infinity,-1)
23 | // This page is way off-screen to the left.
24 | view.setRotationY(-1 * mMaxRotate);
25 | view.setPivotX(view.getWidth());
26 | } else if (position <= 1) { // [-1,1]
27 | // Modify the default slide transition to shrink the page as well
28 | view.setRotationY(position * mMaxRotate);
29 |
30 | //[0,-1]
31 | if (position < 0) {
32 | view.setPivotX(view.getWidth() * (DEFAULT_CENTER + DEFAULT_CENTER * (-position)));
33 | view.setPivotX(view.getWidth());
34 | } else {//[1,0]
35 | view.setPivotX(view.getWidth() * DEFAULT_CENTER * (1 - position));
36 | view.setPivotX(0);
37 | }
38 |
39 | // Scale the page down (between MIN_SCALE and 1)
40 | } else {
41 | // (1,+Infinity]
42 | // This page is way off-screen to the right.
43 | view.setRotationY(1 * mMaxRotate);
44 | view.setPivotX(0);
45 | }
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/transformer/ScaleInTransformer.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.transformer;
2 |
3 | import android.view.View;
4 | import android.view.ViewParent;
5 |
6 | import androidx.annotation.NonNull;
7 | import androidx.annotation.Px;
8 | import androidx.core.view.ViewCompat;
9 | import androidx.recyclerview.widget.RecyclerView;
10 | import androidx.viewpager2.widget.ViewPager2;
11 |
12 | public class ScaleInTransformer extends BasePageTransformer {
13 | private static final float DEFAULT_MIN_SCALE = 0.85f;
14 | private float mMinScale = DEFAULT_MIN_SCALE;
15 |
16 | public ScaleInTransformer() {
17 | }
18 |
19 | public ScaleInTransformer(float minScale) {
20 | this.mMinScale = minScale;
21 | }
22 |
23 | @Override
24 | public void transformPage(@NonNull View view, float position) {
25 | int pageWidth = view.getWidth();
26 | int pageHeight = view.getHeight();
27 |
28 | view.setPivotY(pageHeight / 2);
29 | view.setPivotX(pageWidth / 2);
30 | if (position < -1) { // [-Infinity,-1)
31 | // This page is way off-screen to the left.
32 | view.setScaleX(mMinScale);
33 | view.setScaleY(mMinScale);
34 | view.setPivotX(pageWidth);
35 | } else if (position <= 1) { // [-1,1]
36 | // Modify the default slide transition to shrink the page as well
37 | if (position < 0) //1-2:1[0,-1] ;2-1:1[-1,0]
38 | {
39 |
40 | float scaleFactor = (1 + position) * (1 - mMinScale) + mMinScale;
41 | view.setScaleX(scaleFactor);
42 | view.setScaleY(scaleFactor);
43 |
44 | view.setPivotX(pageWidth * (DEFAULT_CENTER + (DEFAULT_CENTER * -position)));
45 |
46 | } else //1-2:2[1,0] ;2-1:2[0,1]
47 | {
48 | float scaleFactor = (1 - position) * (1 - mMinScale) + mMinScale;
49 | view.setScaleX(scaleFactor);
50 | view.setScaleY(scaleFactor);
51 | view.setPivotX(pageWidth * ((1 - position) * DEFAULT_CENTER));
52 | }
53 |
54 |
55 | } else { // (1,+Infinity]
56 | view.setPivotX(0);
57 | view.setScaleX(mMinScale);
58 | view.setScaleY(mMinScale);
59 | }
60 | }
61 | }
62 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/transformer/ZoomOutPageTransformer.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.transformer;
2 |
3 | import android.view.View;
4 |
5 | import androidx.viewpager2.widget.ViewPager2;
6 |
7 |
8 | public class ZoomOutPageTransformer extends BasePageTransformer {
9 | private static final float DEFAULT_MIN_SCALE = 0.85f;
10 | private static final float DEFAULT_MIN_ALPHA = 0.5f;
11 | private float mMinScale = DEFAULT_MIN_SCALE;
12 | private float mMinAlpha = DEFAULT_MIN_ALPHA;
13 |
14 | public ZoomOutPageTransformer() {
15 | }
16 |
17 | public ZoomOutPageTransformer(float minScale,float minAlpha ) {
18 | this.mMinScale = minScale;
19 | this.mMinAlpha = minAlpha;
20 | }
21 |
22 | public void transformPage(View view, float position) {
23 | int pageWidth = view.getWidth();
24 | int pageHeight = view.getHeight();
25 |
26 | if (position < -1) { // [-Infinity,-1)
27 | // This page is way off-screen to the left.
28 | view.setAlpha(0f);
29 |
30 | } else if (position <= 1) { // [-1,1]
31 | // Modify the default slide transition to shrink the page as well
32 | float scaleFactor = Math.max(mMinScale, 1 - Math.abs(position));
33 | float vertMargin = pageHeight * (1 - scaleFactor) / 2;
34 | float horzMargin = pageWidth * (1 - scaleFactor) / 2;
35 | if (position < 0) {
36 | view.setTranslationX(horzMargin - vertMargin / 2);
37 | } else {
38 | view.setTranslationX(-horzMargin + vertMargin / 2);
39 | }
40 |
41 | // Scale the page down (between MIN_SCALE and 1)
42 | view.setScaleX(scaleFactor);
43 | view.setScaleY(scaleFactor);
44 |
45 | // Fade the page relative to its size.
46 | view.setAlpha(mMinAlpha +
47 | (scaleFactor - mMinScale) /
48 | (1 - mMinScale) * (1 - mMinAlpha));
49 |
50 | } else { // (1,+Infinity]
51 | // This page is way off-screen to the right.
52 | view.setAlpha(0f);
53 | }
54 | }
55 | }
56 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/util/BannerLifecycleObserver.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.util;
2 |
3 | import androidx.lifecycle.LifecycleObserver;
4 | import androidx.lifecycle.LifecycleOwner;
5 |
6 | public interface BannerLifecycleObserver extends LifecycleObserver {
7 |
8 | void onStop(LifecycleOwner owner);
9 |
10 | void onStart(LifecycleOwner owner);
11 |
12 | void onDestroy(LifecycleOwner owner);
13 | }
14 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/util/BannerLifecycleObserverAdapter.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.util;
2 |
3 | import androidx.lifecycle.Lifecycle;
4 | import androidx.lifecycle.LifecycleObserver;
5 | import androidx.lifecycle.LifecycleOwner;
6 | import androidx.lifecycle.OnLifecycleEvent;
7 |
8 | public class BannerLifecycleObserverAdapter implements LifecycleObserver {
9 | private final BannerLifecycleObserver mObserver;
10 | private final LifecycleOwner mLifecycleOwner;
11 |
12 | public BannerLifecycleObserverAdapter(LifecycleOwner lifecycleOwner, BannerLifecycleObserver observer) {
13 | mLifecycleOwner = lifecycleOwner;
14 | mObserver = observer;
15 | }
16 |
17 | @OnLifecycleEvent(Lifecycle.Event.ON_START)
18 | public void onStart() {
19 | LogUtils.i("onStart");
20 | mObserver.onStart(mLifecycleOwner);
21 | }
22 |
23 | @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
24 | public void onStop() {
25 | LogUtils.i("onStop");
26 | mObserver.onStop(mLifecycleOwner);
27 | }
28 |
29 | @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
30 | public void onDestroy() {
31 | LogUtils.i("onDestroy");
32 | mObserver.onDestroy(mLifecycleOwner);
33 | }
34 |
35 | }
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/util/BannerUtils.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.util;
2 |
3 | import android.content.res.Resources;
4 | import android.graphics.Outline;
5 | import android.os.Build;
6 | import android.util.TypedValue;
7 | import android.view.LayoutInflater;
8 | import android.view.View;
9 | import android.view.ViewGroup;
10 | import android.view.ViewOutlineProvider;
11 |
12 | import androidx.annotation.LayoutRes;
13 | import androidx.annotation.NonNull;
14 | import androidx.annotation.RequiresApi;
15 |
16 | public class BannerUtils {
17 |
18 | /**
19 | * 获取真正的位置
20 | *
21 | * @param isIncrease 首尾是否有增加
22 | * @param position 当前位置
23 | * @param realCount 真实数量
24 | * @return
25 | */
26 | public static int getRealPosition(boolean isIncrease, int position, int realCount) {
27 | if (!isIncrease) {
28 | return position;
29 | }
30 | int realPosition;
31 | if (position == 0) {
32 | realPosition = realCount - 1;
33 | } else if (position == realCount + 1) {
34 | realPosition = 0;
35 | } else {
36 | realPosition = position - 1;
37 | }
38 | return realPosition;
39 | }
40 |
41 | /**
42 | * 将布局文件转成view,这里为了适配viewpager2中高宽必须为match_parent
43 | *
44 | * @param parent
45 | * @param layoutId
46 | * @return
47 | */
48 | public static View getView(@NonNull ViewGroup parent, @LayoutRes int layoutId) {
49 | View view = LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false);
50 | ViewGroup.LayoutParams params = view.getLayoutParams();
51 | //这里判断高度和宽带是否都是match_parent
52 | if (params.height != -1 || params.width != -1) {
53 | params.height = -1;
54 | params.width = -1;
55 | view.setLayoutParams(params);
56 | }
57 | return view;
58 | }
59 |
60 | public static int dp2px(float dp) {
61 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics());
62 | }
63 |
64 | /**
65 | * 设置view圆角
66 | *
67 | * @param radius
68 | * @return
69 | */
70 | @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
71 | public static void setBannerRound(View view,float radius) {
72 | view.setOutlineProvider(new ViewOutlineProvider() {
73 | @Override
74 | public void getOutline(View view, Outline outline) {
75 | outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), radius);
76 | }
77 | });
78 | view.setClipToOutline(true);
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/util/LogUtils.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.util;
2 |
3 | import android.util.Log;
4 |
5 | import com.youth.banner.BuildConfig;
6 |
7 | public class LogUtils {
8 | public static final String TAG = "banner_log";
9 |
10 | private static final boolean DEBUG = BuildConfig.DEBUG;
11 |
12 | public static void d(String msg) {
13 | if (DEBUG) {
14 | Log.d(TAG, msg);
15 | }
16 | }
17 |
18 | public static void e(String msg) {
19 | if (DEBUG) {
20 | Log.e(TAG, msg);
21 | }
22 | }
23 |
24 | public static void i(String msg) {
25 | if (DEBUG) {
26 | Log.i(TAG, msg);
27 | }
28 | }
29 |
30 | public static void v( String msg) {
31 | if (DEBUG) {
32 | Log.v(TAG, msg);
33 | }
34 | }
35 |
36 | public static void w(String msg) {
37 | if (DEBUG) {
38 | Log.w(TAG, msg);
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/banner/src/main/java/com/youth/banner/util/ScrollSpeedManger.java:
--------------------------------------------------------------------------------
1 | package com.youth.banner.util;
2 |
3 | import androidx.recyclerview.widget.LinearLayoutManager;
4 | import androidx.recyclerview.widget.LinearSmoothScroller;
5 | import androidx.recyclerview.widget.RecyclerView;
6 | import androidx.viewpager2.widget.ViewPager2;
7 |
8 | import com.youth.banner.Banner;
9 |
10 | import java.lang.reflect.Field;
11 |
12 |
13 | /**
14 | * 改变LinearLayoutManager的切换速度
15 | */
16 | public class ScrollSpeedManger extends LinearLayoutManager {
17 | private Banner banner;
18 |
19 | public ScrollSpeedManger(Banner banner, LinearLayoutManager linearLayoutManager) {
20 | super(banner.getContext(), linearLayoutManager.getOrientation(), false);
21 | this.banner = banner;
22 | }
23 |
24 | @Override
25 | public void smoothScrollToPosition(RecyclerView recyclerView, RecyclerView.State state, int position) {
26 | LinearSmoothScroller linearSmoothScroller = new LinearSmoothScroller(recyclerView.getContext()) {
27 | @Override
28 | protected int calculateTimeForDeceleration(int dx) {
29 | return banner.getScrollTime();
30 | }
31 | };
32 | linearSmoothScroller.setTargetPosition(position);
33 | startSmoothScroll(linearSmoothScroller);
34 | }
35 |
36 | public static void reflectLayoutManager(Banner banner) {
37 | if (banner.getScrollTime() < 100) return;
38 | try {
39 | ViewPager2 viewPager2 = banner.getViewPager2();
40 | RecyclerView recyclerView = (RecyclerView) viewPager2.getChildAt(0);
41 | recyclerView.setOverScrollMode(RecyclerView.OVER_SCROLL_NEVER);
42 |
43 | ScrollSpeedManger speedManger = new ScrollSpeedManger(banner, (LinearLayoutManager) recyclerView.getLayoutManager());
44 | recyclerView.setLayoutManager(speedManger);
45 |
46 |
47 | Field LayoutMangerField = ViewPager2.class.getDeclaredField("mLayoutManager");
48 | LayoutMangerField.setAccessible(true);
49 | LayoutMangerField.set(viewPager2, speedManger);
50 |
51 | Field pageTransformerAdapterField = ViewPager2.class.getDeclaredField("mPageTransformerAdapter");
52 | pageTransformerAdapterField.setAccessible(true);
53 | Object mPageTransformerAdapter = pageTransformerAdapterField.get(viewPager2);
54 | if (mPageTransformerAdapter != null) {
55 | Class> aClass = mPageTransformerAdapter.getClass();
56 | Field layoutManager = aClass.getDeclaredField("mLayoutManager");
57 | layoutManager.setAccessible(true);
58 | layoutManager.set(mPageTransformerAdapter, speedManger);
59 | }
60 | Field scrollEventAdapterField = ViewPager2.class.getDeclaredField("mScrollEventAdapter");
61 | scrollEventAdapterField.setAccessible(true);
62 | Object mScrollEventAdapter = scrollEventAdapterField.get(viewPager2);
63 | if (mScrollEventAdapter != null) {
64 | Class> aClass = mScrollEventAdapter.getClass();
65 | Field layoutManager = aClass.getDeclaredField("mLayoutManager");
66 | layoutManager.setAccessible(true);
67 | layoutManager.set(mScrollEventAdapter, speedManger);
68 | }
69 | } catch (Exception e) {
70 | e.printStackTrace();
71 | }
72 | }
73 |
74 | }
75 |
--------------------------------------------------------------------------------
/banner/src/main/res/values/attr.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/banner/src/main/res/values/ids.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/banner/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | indicator color attribute : “@color/colorID or #000000”
3 | Adapter is NULL!
4 |
5 |
6 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 |
2 | buildscript {
3 | ext {
4 | kotlin_version = '1.7.0'
5 | }
6 | repositories {
7 | maven { url 'https://maven.aliyun.com/repository/public/' }
8 | maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
9 | maven { url 'https://jitpack.io' }
10 | maven { url "https://s01.oss.sonatype.org/content/groups/public" }
11 | google()
12 | }
13 | dependencies {
14 |
15 | classpath 'com.android.tools.build:gradle:7.1.1'
16 | classpath 'com.jakewharton:butterknife-gradle-plugin:10.2.3'
17 | classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
18 |
19 | }
20 | }
21 |
22 | allprojects {
23 | repositories {
24 | maven { url 'https://maven.aliyun.com/repository/public/' }
25 | maven { url 'https://maven.aliyun.com/repository/gradle-plugin' }
26 | maven { url 'https://jitpack.io' }
27 | maven { url "https://s01.oss.sonatype.org/content/groups/public" }
28 | mavenCentral()
29 | google()
30 | }
31 | }
32 |
33 | task clean(type: Delete) {
34 | delete rootProject.buildDir
35 | }
36 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | distributionBase=GRADLE_USER_HOME
2 | distributionPath=wrapper/dists
3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 |
--------------------------------------------------------------------------------
/images/DepthPageTransformer.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/images/DepthPageTransformer.gif
--------------------------------------------------------------------------------
/images/ZoomOutPageTransformer.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/images/ZoomOutPageTransformer.gif
--------------------------------------------------------------------------------
/images/banner_example.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/images/banner_example.gif
--------------------------------------------------------------------------------
/images/banner_example1.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/images/banner_example1.gif
--------------------------------------------------------------------------------
/images/banner_example2.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/images/banner_example2.gif
--------------------------------------------------------------------------------
/images/banner_example3.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/images/banner_example3.gif
--------------------------------------------------------------------------------
/images/mailme.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/images/mailme.png
--------------------------------------------------------------------------------
/images/pay.jpg:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/images/pay.jpg
--------------------------------------------------------------------------------
/images/qq.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/images/qq.png
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 | include ':banner'
3 | include ':usekotlin'
4 |
--------------------------------------------------------------------------------
/update_message.md:
--------------------------------------------------------------------------------
1 |
2 | ## 更新说明
3 | #### v2.2.3
4 | * 修改adapter 数据相关操作方法
5 | * banner 去掉泛型返回
6 | * demo 添加更新数据注释
7 | * 更新viewpager2到beta2
8 |
9 | #### v2.2.1-2.2.2
10 | * 对kotlin使用的兼容支持
11 | * 对kotlin使用demo进行了修改
12 | * 新版本都将迁移至MavenCentral
13 | * 更新viewpager2到1.1.0-alpha01
14 | * 修改bug
15 |
16 | #### v2.2.0
17 | * 自定义属性中增加可选择设置banner圆角的具体方向
18 | * 增加DrawableIndicator指示器,支持直接设置图片了,可以通过构造方法或者自定义属性设置
19 | * 合并ZakAnun朋友提交的优化监听器的设置以及减少 onPageScrolled 回调
20 | * 对demo进行了修改
21 | * 新的版本迁移将至jitpack仓库
22 |
23 | #### v2.1.0
24 | * 修改banner圆角锯齿
25 | * 修改画廊支持左右不同的大小
26 | * 内置增加了默认的图片适配器,方便偷懒的人,但是其他类型的没有内置是因为可变性太高了,大家自定义就好
27 | * 自定义参数全部修改成banner_开头了,避免和其他库冲突。麻烦使用的大家修改下!
28 |
29 | #### v2.0.12
30 | * 修改了new banner时无默认参数的问题
31 | * 自带的指示器支持选中和默认的宽度
32 | * setAdapter支持设置是否无限循环
33 |
34 | #### v2.0.11
35 | * 合并了lvshaojun朋友提交的代码
36 |
37 | #### v2.0.9
38 | * 增加了addBannerLifecycleObserver()方法,给banner添加生命周期观察者,将生命周期交给banner自己处理,
39 | 你也可以不使用这种方式自己象以前的方式自己控制,具体可以参考文档和demo
40 | * 修改了DepthPageTransformer效果,现在使用这种效果返回的位置应该没有问题了
41 | * 其他优化
42 |
43 | #### v2.0.8
44 | * 修复bug
45 | * 增加混淆配置
46 |
47 | #### v2.0.7
48 | * 修复画廊问题
49 | * 增加了多种PageTransformer
50 | * 增加滑动距离方法
51 | * demo中提供了adapter数据操作示例
52 | * 工具类中增加view圆角裁剪方法
53 | * 增加魅族轮播效果
54 |
55 | #### v2.0.6
56 | * 修复指示器无法设置为白色问题
57 | * 新增销毁方法
58 | * 调小滑动距离
59 |
60 | #### v2.0.5
61 | * demo中增加tv遥控器控件切换,我没有真机测试,所以只放到了demo中,使用的朋友有TV真机的帮忙测试下反馈给我哈!
62 | * 增加了view的添加和分离回调时,分别暂停和开始轮播,主要解决滑动嵌套时不可见轮播。这个也可以自己监听实现哈
63 |
64 | #### v2.0.3-2.0.4
65 | banner 修复了几个紧急bug,圆角绘制引起的残影和焦点问题,替换layoutmanager为null的异常
66 |
67 | #### v2.0.2
68 | banner bug修复
69 | * 有朋友想要在引导页使用banner,所以增加了is_infinite_loop属性来控制是否能无限循环
70 | * 增加了设置开始轮播位置的方法
71 | * 修复圆角裁剪和首次设置开始位置不对的bug
72 | * demo中增加了类似淘宝详情banner的案例,第一个放视频,不能无限循环
73 | * 设置指示器方法调整,demo增加自定义数字指示器案例
74 | * 对ProxyLayoutManger进行的优化
75 |
76 | #### v2.0.1
77 | banner bug修改
78 | * 解决一系列嵌套banner的事件冲突和解决方案
79 | (如果我没有考虑到的场景,大家再反馈或者提交方案)
80 |
81 | #### v2.0.0
82 | banner 正式版发布
83 | * 改变banner圆角实现方式,兼容低版本
84 | * 新增两种指示器效果,基本能满足大部分需求了
85 |
86 | #### v2.0.0-beta
87 | banner 测试版发布
88 | * 修改banner内部轮播逻辑
89 | * 增加设置画廊效果方法
90 | * 内置有2种官方Transformer效果,更多效果你可以自定义
91 | * 优化切换事件和点击事件,去掉onBannerChanged方法
92 | * 新增轮播滑动时间控制,感谢zguop,这里的具体实现方法来自zguop的项目
93 | * 支持多PageTransformer组合设置
94 |
95 | #### v2.0.0-alpha03
96 | banner 预览版使用中的问题优化。
97 | 这里要感谢BoldFruit朋友修复了一个bug;我爷爷得了癌症住院了需要照顾,公司事情也多,所以更新频率会慢很多。希望大家理解!
98 | 不过我更希望大家要是发现bug,亦可以自己修复提交,有什么想法新增的功能也可以提交过来。可以提供一个良好的开源环境哈!
99 | 还有不要一直夺命连环催,大家都有自己的事情,多多理解!
100 |
101 | * 增加支持布局文件中使用自定义指示器了(自己发挥)
102 | * 增加支持Android5.0以上的设置banner圆角方法
103 | * 其他问题修复(有些实在是不好复现加上有点忙,只修改了部分bug)
104 |
105 | #### v2.0.0-alpha02
106 | banner 预览版使用中的问题优化
107 | * #666 修改orientation自定义属性与其他库冲突问题:改成banner_orientation
108 | * 增加RecyclerView嵌套banner和ConstraintLayout中使用banner测试demo
109 | * #669 RecyclerView嵌套banner 轮播优化代码建议,demo中可以查看
110 | * 其他问题修复
111 |
112 | #### v2.0.0-alpha
113 | banner 2.0全新基础升级,目前是预览版,使用中的问题慢慢迭代
114 | * 使用了ViewPager2为基础控件
115 | * 支持了androidx兼容包
116 | * 方便了UI、Indicator自定义(现在还是基础版本,后面再提升)
117 | * 依赖包目前只需要导入了ViewPager2
118 |
--------------------------------------------------------------------------------
/usekotlin/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/usekotlin/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'kotlin-android'
3 | apply plugin: 'kotlin-android-extensions'
4 |
5 | android {
6 | compileSdkVersion 33
7 |
8 | defaultConfig {
9 | minSdkVersion 21
10 | targetSdkVersion 33
11 | versionCode 1
12 | versionName "1.0"
13 |
14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
15 | consumerProguardFiles 'consumer-rules.pro'
16 | }
17 |
18 | buildTypes {
19 | release {
20 | minifyEnabled false
21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
22 | }
23 | }
24 | compileOptions {
25 | sourceCompatibility = 1.8
26 | targetCompatibility = 1.8
27 | }
28 |
29 | }
30 |
31 | dependencies {
32 | implementation fileTree(dir: 'libs', include: ['*.jar'])
33 | implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.31'
34 | implementation 'androidx.appcompat:appcompat:1.1.0'
35 | implementation 'androidx.core:core-ktx:1.3.0'
36 | implementation 'com.github.bumptech.glide:glide:4.12.0'
37 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
38 | testImplementation 'junit:junit:4.13.2'
39 | androidTestImplementation 'androidx.test.ext:junit:1.1.1'
40 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
41 | implementation project(path: ':banner')
42 | implementation 'androidx.recyclerview:recyclerview:1.2.0'
43 | }
44 |
--------------------------------------------------------------------------------
/usekotlin/consumer-rules.pro:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/usekotlin/consumer-rules.pro
--------------------------------------------------------------------------------
/usekotlin/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/usekotlin/src/androidTest/java/com/spring/usekotlin/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.spring.usekotlin
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.spring.usekotlin.test", appContext.packageName)
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/usekotlin/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
10 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/usekotlin/src/main/java/com/spring/usekotlin/ImageAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.spring.usekotlin
2 |
3 | import android.view.View
4 | import android.view.ViewGroup
5 | import android.widget.ImageView
6 | import androidx.recyclerview.widget.RecyclerView
7 | import com.bumptech.glide.Glide
8 | import com.youth.banner.adapter.BannerAdapter
9 | import com.youth.banner.util.BannerUtils
10 |
11 | class ImageAdapter(imageUrls: List) : BannerAdapter(imageUrls) {
12 |
13 |
14 | override fun onCreateHolder(parent: ViewGroup?, viewType: Int): ImageHolder {
15 | val imageView = ImageView(parent!!.context)
16 | val params = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
17 | imageView.layoutParams = params
18 | imageView.scaleType = ImageView.ScaleType.CENTER_CROP
19 | //通过裁剪实现圆角
20 | BannerUtils.setBannerRound(imageView, 20f)
21 | return ImageHolder(imageView)
22 | }
23 |
24 | override fun onBindView(holder: ImageHolder, data: String, position: Int, size: Int) {
25 | Glide.with(holder.itemView)
26 | .load(data)
27 | .into(holder.imageView)
28 | }
29 |
30 |
31 | class ImageHolder(view: View) : RecyclerView.ViewHolder(view) {
32 | var imageView: ImageView = view as ImageView
33 | }
34 |
35 | }
36 |
37 |
--------------------------------------------------------------------------------
/usekotlin/src/main/java/com/spring/usekotlin/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.spring.usekotlin
2 |
3 | import android.os.Bundle
4 | import androidx.appcompat.app.AppCompatActivity
5 | import com.bumptech.glide.Glide
6 | import com.youth.banner.Banner
7 | import com.youth.banner.adapter.BannerImageAdapter
8 | import com.youth.banner.holder.BannerImageHolder
9 | import com.youth.banner.indicator.CircleIndicator
10 | import com.youth.banner.indicator.RoundLinesIndicator
11 | import kotlinx.android.synthetic.main.activity_main.*
12 |
13 | class MainActivity : AppCompatActivity() {
14 |
15 | var imageUrls = listOf(
16 | "https://img.zcool.cn/community/01b72057a7e0790000018c1bf4fce0.png",
17 | "https://img.zcool.cn/community/016a2256fb63006ac7257948f83349.jpg",
18 | "https://img.zcool.cn/community/01233056fb62fe32f875a9447400e1.jpg",
19 | "https://img.zcool.cn/community/01700557a7f42f0000018c1bd6eb23.jpg"
20 | )
21 |
22 | override fun onCreate(savedInstanceState: Bundle?) {
23 | super.onCreate(savedInstanceState)
24 | setContentView(R.layout.activity_main)
25 | //使用默认的图片适配器
26 | var banner = (bannerLayout1 as Banner>)
27 | banner.apply {
28 | addBannerLifecycleObserver(this@MainActivity)
29 | setIndicator(CircleIndicator(this@MainActivity))
30 | setAdapter(object : BannerImageAdapter(imageUrls) {
31 | override fun onBindView(holder: BannerImageHolder, data: String, position: Int, size: Int) {
32 | Glide.with(this@MainActivity)
33 | .load(data)
34 | .into(holder.imageView)
35 | }
36 | })
37 | }
38 |
39 |
40 | //使用自定义适配器,更多api方法自己尝试
41 | var banner2 = (bannerLayout2 as Banner)
42 | banner2.apply {
43 | addBannerLifecycleObserver(this@MainActivity)
44 | setBannerRound(20f)
45 | setIndicator(RoundLinesIndicator(this@MainActivity))
46 | setAdapter(ImageAdapter(imageUrls))
47 | }
48 | }
49 |
50 |
51 | }
52 |
--------------------------------------------------------------------------------
/usekotlin/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
15 |
16 |
22 |
23 |
--------------------------------------------------------------------------------
/usekotlin/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/usekotlin/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/usekotlin/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/usekotlin/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/usekotlin/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/usekotlin/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/usekotlin/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/youth5201314/banner/777c7dbfaa0a48d78101858d5520f695fef91fbd/usekotlin/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/usekotlin/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | @color/main_color
4 | @color/main_color
5 | @color/main_color
6 |
7 | #BDBDBD
8 |
9 | #5CB85C
10 |
11 |
12 |
--------------------------------------------------------------------------------
/usekotlin/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | Banner kotlin Example
3 |
4 |
--------------------------------------------------------------------------------
/usekotlin/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/usekotlin/src/test/java/com/spring/usekotlin/ExampleUnitTest.kt:
--------------------------------------------------------------------------------
1 | package com.spring.usekotlin
2 |
3 | import org.junit.Test
4 |
5 | import org.junit.Assert.*
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * See [testing documentation](http://d.android.com/tools/testing).
11 | */
12 | class ExampleUnitTest {
13 | @Test
14 | fun addition_isCorrect() {
15 | assertEquals(4, 2 + 2)
16 | }
17 | }
18 |
--------------------------------------------------------------------------------