├── .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 | 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 | ![默认](images/banner_example.gif) 36 | 37 | ![画廊](images/banner_example2.gif) 38 | 39 | ![魅族](images/banner_example1.gif) 40 | 41 | ![头条](images/banner_example3.gif) 42 | 43 | ##### 内置了多种PageTransformer效果 44 | 45 | ![DepthPageTransformer](images/DepthPageTransformer.gif) 46 | ![ZoomOutPageTransformer](images/ZoomOutPageTransformer.gif) 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 |