├── .gitignore ├── .idea ├── codeStyles │ └── Project.xml ├── gradle.xml ├── misc.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── README.md ├── README_1.x.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cn │ │ └── mtjsoft │ │ └── www │ │ └── gridviewpager │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── cn │ │ │ └── mtjsoft │ │ │ └── www │ │ │ └── gridviewpager │ │ │ ├── CoordinatorActivity.java │ │ │ ├── ListActivity.java │ │ │ ├── ListAdapter.java │ │ │ └── MainActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_arrow_white_24dp.xml │ │ ├── ic_launcher_background.xml │ │ └── ic_launcher_background_red.xml │ │ ├── layout │ │ ├── activity_coordinatorlayout.xml │ │ ├── activity_list.xml │ │ ├── activity_main.xml │ │ └── item_text.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_category_0.png │ │ ├── ic_category_1.png │ │ ├── ic_category_10.png │ │ ├── ic_category_11.png │ │ ├── ic_category_12.png │ │ ├── ic_category_13.png │ │ ├── ic_category_14.png │ │ ├── ic_category_15.png │ │ ├── ic_category_16.png │ │ ├── ic_category_17.png │ │ ├── ic_category_18.png │ │ ├── ic_category_19.png │ │ ├── ic_category_2.png │ │ ├── ic_category_20.png │ │ ├── ic_category_21.png │ │ ├── ic_category_22.png │ │ ├── ic_category_23.png │ │ ├── ic_category_24.png │ │ ├── ic_category_25.png │ │ ├── ic_category_26.png │ │ ├── ic_category_27.png │ │ ├── ic_category_28.png │ │ ├── ic_category_29.png │ │ ├── ic_category_3.png │ │ ├── ic_category_30.png │ │ ├── ic_category_31.png │ │ ├── ic_category_32.png │ │ ├── ic_category_33.png │ │ ├── ic_category_4.png │ │ ├── ic_category_5.png │ │ ├── ic_category_6.png │ │ ├── ic_category_7.png │ │ ├── ic_category_8.png │ │ ├── ic_category_9.png │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── cn │ └── mtjsoft │ └── www │ └── gridviewpager │ └── ExampleUnitTest.java ├── build.gradle ├── gif.gif ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── gridviewpager_recycleview ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── cn │ │ └── mtjsoft │ │ └── www │ │ └── gridviewpager_recycleview │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── cn │ │ │ └── mtjsoft │ │ │ └── www │ │ │ └── gridviewpager_recycleview │ │ │ ├── GridViewPager.java │ │ │ ├── transformer │ │ │ ├── CoverPageTransformer.java │ │ │ ├── GalleryPageTransformer.java │ │ │ └── TopOrDownPageTransformer.java │ │ │ └── view │ │ │ ├── AndDensityUtils.java │ │ │ └── AndSelectCircleView.java │ └── res │ │ ├── layout │ │ ├── gridpager_item.xml │ │ ├── gridpager_item_layout.xml │ │ ├── gridpager_item_text.xml │ │ └── gridpager_layout.xml │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── attrs.xml │ │ └── strings.xml │ └── test │ └── java │ └── cn │ └── mtjsoft │ └── www │ └── gridviewpager_recycleview │ └── ExampleUnitTest.java ├── settings.gradle ├── show.mp4 └── wxqrcode.jpg /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches/build_file_checksums.ser 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | .DS_Store 9 | /build 10 | /captures 11 | .externalNativeBuild 12 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
7 | 8 | 9 | 10 | xmlns:android 11 | 12 | ^$ 13 | 14 | 15 | 16 |
17 |
18 | 19 | 20 | 21 | xmlns:.* 22 | 23 | ^$ 24 | 25 | 26 | BY_NAME 27 | 28 |
29 |
30 | 31 | 32 | 33 | .*:id 34 | 35 | http://schemas.android.com/apk/res/android 36 | 37 | 38 | 39 |
40 |
41 | 42 | 43 | 44 | .*:name 45 | 46 | http://schemas.android.com/apk/res/android 47 | 48 | 49 | 50 |
51 |
52 | 53 | 54 | 55 | name 56 | 57 | ^$ 58 | 59 | 60 | 61 |
62 |
63 | 64 | 65 | 66 | style 67 | 68 | ^$ 69 | 70 | 71 | 72 |
73 |
74 | 75 | 76 | 77 | .* 78 | 79 | ^$ 80 | 81 | 82 | BY_NAME 83 | 84 |
85 |
86 | 87 | 88 | 89 | .* 90 | 91 | http://schemas.android.com/apk/res/android 92 | 93 | 94 | ANDROID_ATTRIBUTE_ORDER 95 | 96 |
97 |
98 | 99 | 100 | 101 | .* 102 | 103 | .* 104 | 105 | 106 | BY_NAME 107 | 108 |
109 |
110 |
111 |
112 |
113 |
-------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 20 | 21 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 24 | 41 | 42 | 43 | 44 | 45 | 46 | 48 | 49 | 54 | 55 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /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 2019-2021 mtjsoft 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 | # GridViewPager3.x 3 | 4 | GridViewPager3.0组件:采用RecycleView + FlexBoxLayout + PagerSnapHelper实现方式,轻松实现类似美团首页分类多页展示。也可用于表情面板的展示。 5 | 链式调用,属性配置,几行代码轻松搞定。 6 | 7 | v3.3.0 开始采用 viewpager2 + FlexBoxLayout 实现 8 | 9 | ① 应用的首页经常需要用到这样的分类**多页展示**的效果,还有些消息输入框需要这样的**表情面板**。 10 | 11 | ② 既然是常用的,作为懒惰的我,肯定不会每次都去写一遍。网上也找了很多类似的例子,但始终不是我想要的**简洁接入**使用的方式。要么就是加载图片有限制,要么就是样式限制的太死,还得改源码,我不喜欢,我得造一个轮子。。。必须封装一个简单好用的组件,做到几行代码就可实现效果才行。于是乎,**GridViewPager组件**就诞生了。 12 | 13 | # GridViewPager组件效果 14 | 15 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200731204720854.gif) 16 | 17 | # 内置了3个覆盖翻页的效果(这是其中一) 18 | 19 | ![覆盖的翻页效果](https://img-blog.csdnimg.cn/20200731205107341.gif) 20 | 21 | # 如何使用GridViewPager组件 22 | 23 | # 1、在根目录 build.gradle 添加: 24 | 25 | ``` 26 | allprojects { 27 | repositories { 28 | ... 29 | maven { url 'https://jitpack.io' } 30 | } 31 | } 32 | ``` 33 | 34 | # 2、在app项目下的build.gradle中添加: 35 | 36 | [![](https://jitpack.io/v/mtjsoft/GridPager.svg)](https://jitpack.io/#mtjsoft/GridPager) 37 | 38 | ``` 39 | dependencies { 40 | implementation 'com.github.mtjsoft:GridPager:v3.7.0' 41 | } 42 | ``` 43 | 44 | 45 | # 3、在需要使用的布局xml中添加GridViewPager组件,根据需要设置相关属性 46 | 47 | ``` 48 | 73 | 74 | 75 | 80 | 81 | ``` 82 | # 4、GridViewPager组件的版本及属性说明 83 | 84 | V3.7.0 85 | -------------------------- 86 | 新增方法 setAlignContent() 设置Item行的对齐方式。等同FLEX布局的AlignContent,可设置以下6个值: 87 | ``` 88 | AlignContent.FLEX_START //(默认值) 89 | AlignContent.FLEX_END 90 | AlignContent.CENTER 91 | AlignContent.SPACE_BETWEEN 92 | AlignContent.SPACE_AROUND 93 | AlignContent.STRETCH 94 | ``` 95 | 新增属性 | 属性说明 96 | ------------- | ------------- 97 | text_is_show | 是否需要显示文本,默认true显示(一般作为表情面板时,可以直接设置false不显示)。也可通过方法setIsShowText()设置 98 | 99 | V3.6.0 100 | -------------------------- 101 | 102 | 新增方法 setGridItemLongClickListener() 设置Item长按监听 103 | 104 | 3.6.0 新增属性 | 属性说明 105 | ------------- | ------------- 106 | pager_loop | 是否开启无限循环滑动 默认 false 不开启 107 | 108 | V3.5.0 109 | -------------------------- 110 | 修复已知问题 111 | 112 | 新增 setTopOrDownPageTransformer(TopOrDownPageTransformer.ModeType.MODE_DOWN)方法,设置内置的上下进入翻页效果 113 | 114 | 新增 setGalleryPageTransformer()方法,设置内置的画廊翻页效果 115 | 116 | V3.4.0 117 | -------------------------- 118 | 修复已知问题 119 | 120 | 新增 setCoverPageTransformer()方法,设置内置的覆盖渐变翻页效果 121 | 122 | 新增 setCustomPageTransformer()方法,用于设置自定义的翻页效果 123 | 124 | V3.3.0 125 | -------------------------- 126 | 更换 viewpager2 + FlexBoxLayout 实现 127 | 128 | V3.2.0 129 | -------------------------- 130 | 修复已知问题 131 | 132 | V3.1.0 133 | -------------------------- 134 | 修复布局设置margin显示不全的问题 135 | 136 | 137 | V3.0.0 138 | -------------------------- 139 | 140 | 3.0.0 属性 | 属性说明 141 | ------------- | ------------- 142 | pager_MarginTop | 设置每页的上边距 默认10dp 单位dp 143 | pager_MarginBottom | 设置每页的下边距 默认10dp 单位dp 144 | verticalSpacing | 设置item的纵向间距 默认10dp 单位dp 145 | img_width | 设置图片宽度 默认50dp 单位dp 146 | img_height | 设置图片高度 默认50dp 单位dp 147 | text_color | 设置文字颜色 默认黑色 148 | imgtext_margin | 设置文字与图片的间距 默认5dp 单位dp 149 | text_size | 设置文字大小 默认10sp 单位sp 150 | row_count | 设置每页行数 默认2 151 | column_count | 设置每页列数 默认4 152 | point_is_show | 是否展示指示器,默认true展示 153 | point_width | 设置指示器的item宽度 默认8dp 单位dp 154 | point_height | 设置指示器的item高度 默认8dp 单位dp 155 | point_margin | 设置指示器的item的间距 默认8dp 单位dp 156 | point_normal_color | 指示器item未选中的颜色 默认灰色 157 | point_select_color | 指示器item选中的颜色 默认红色 158 | point_is_circle | 指示器的item是否为圆形,默认圆形直径取宽高的最小值 159 | point_margin_page | 设置指示器与page的间距,默认是verticalSpacing的值 160 | point_margin_bottom | 设置指示器与底部的间距,默认是verticalSpacing的值 161 | background_color | 设置组件背景颜色,默认白色 162 | notifyItemChanged(int position) | 刷新指定页数据 163 | 164 | 165 | # 5、代码实现。链式调用,只需要设置总数量即可。数据绑定完全自定义,不受任何图片加载框架限制,更加自由。 166 | 167 | ``` 168 | GridViewPager gridViewPager = findViewById(R.id.gridviewpager); 169 | gridViewPager 170 | // 设置数据总数量 171 | .setDataAllCount(titles.length) 172 | // 设置背景图片(此时设置的背景色无效,以背景图片为主) 173 | .setBackgroundImageLoader(new GridViewPager.BackgroundImageLoaderInterface() { 174 | @Override 175 | public void setBackgroundImg(ImageView bgImageView) { 176 | bgImageView.setImageResource(R.drawable.ic_launcher_background); 177 | } 178 | }) 179 | // 数据绑定 180 | .setImageTextLoaderInterface(new GridViewPager.ImageTextLoaderInterface() { 181 | @Override 182 | public void displayImageText(ImageView imageView, TextView textView, int position) { 183 | // 自己进行数据的绑定,灵活度更高,不受任何限制 184 | imageView.setImageResource(iconS[position]); 185 | textView.setText(titles[position]); 186 | } 187 | }) 188 | // Item点击 189 | .setGridItemClickListener(new GridViewPager.GridItemClickListener() { 190 | @Override 191 | public void click(int position) { 192 | Toast.makeText(getBaseContext(), "点击了" + titles[position] + position, Toast.LENGTH_SHORT).show(); 193 | } 194 | }) 195 | .show(); 196 | ``` 197 | 198 | # 6、代码中也可直接设置属性(如果xml与代码都设置了, 最终以代码设置为准) 199 | 200 | ``` 201 | GridViewPager gridViewPager2 = findViewById(R.id.gridviewpager2); 202 | gridViewPager2 203 | // 设置数据总数量 204 | .setDataAllCount(titles.length) 205 | // 设置背景色,默认白色 206 | .setGridViewPagerBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.white)) 207 | // 设置item的纵向间距 208 | .setVerticalSpacing(10) 209 | // 设置上边距 210 | .setPagerMarginTop(10) 211 | // 设置下边距 212 | .setPagerMarginBottom(10) 213 | // 设置图片宽度 214 | .setImageWidth(50) 215 | // 设置图片高度 216 | .setImageHeight(50) 217 | // 设置文字与图片的间距 218 | .setTextImgMargin(5) 219 | // 设置文字颜色 220 | .setTextColor(ContextCompat.getColor(getBaseContext(), R.color.white)) 221 | // 设置文字大小 222 | .setTextSize(12) 223 | // 设置每页行数 224 | .setRowCount(2) 225 | // 设置每页列数 226 | .setColumnCount(5) 227 | // 设置无限循环 228 | .setPageLoop(true) 229 | // 设置是否显示指示器 230 | .setPointIsShow(true) 231 | // 设置指示器与page的间距 232 | .setPointMarginPage(10) 233 | // 设置指示器与底部的间距 234 | .setPointMarginBottom(10) 235 | // 设置指示器的item宽度 236 | .setPointChildWidth(8) 237 | // 设置指示器的item高度 238 | .setPointChildHeight(8) 239 | // 设置指示器的item的间距 240 | .setPointChildMargin(8) 241 | // 指示器的item是否为圆形,默认圆形直径取宽高的最小值 242 | .setPointIsCircle(true) 243 | // 指示器item未选中的颜色 244 | .setPointNormalColor(ContextCompat.getColor(getBaseContext(), R.color.white)) 245 | // 指示器item选中的颜色 246 | .setPointSelectColor(ContextCompat.getColor(getBaseContext(), R.color.black_text)) 247 | // 设置背景图片(此时设置的背景色无效,以背景图片为主) 248 | .setBackgroundImageLoader(new GridViewPager.BackgroundImageLoaderInterface() { 249 | @Override 250 | public void setBackgroundImg(ImageView bgImageView) { 251 | bgImageView.setImageResource(R.drawable.ic_launcher_background_red); 252 | } 253 | }) 254 | // 数据绑定 255 | .setImageTextLoaderInterface(new GridViewPager.ImageTextLoaderInterface() { 256 | @Override 257 | public void displayImageText(ImageView imageView, TextView textView, int position) { 258 | // 自己进行数据的绑定,灵活度更高,不受任何限制 259 | imageView.setImageResource(iconS[position]); 260 | textView.setText(titles[position]); 261 | } 262 | }) 263 | // Item点击 264 | .setGridItemClickListener(new GridViewPager.GridItemClickListener() { 265 | @Override 266 | public void click(int position) { 267 | Toast.makeText(getBaseContext(), "点击了" + titles[position] + position, Toast.LENGTH_SHORT).show(); 268 | } 269 | }) 270 | // 设置Item长按 271 | .setGridItemLongClickListener(new GridViewPager.GridItemLongClickListener() { 272 | @Override 273 | public void longClick(int position) { 274 | Toast.makeText(getBaseContext(), "长按了" + titles[position].split("_")[0], Toast.LENGTH_SHORT).show(); 275 | } 276 | }) 277 | .show(); 278 | ``` 279 | # 7、刷新数据 280 | 281 | **设置新属性,刷新** 282 | 283 | ``` 284 | gridViewPager 285 | // 数据大小有变化时,得设置 286 | .setDataAllCount(titles.length) 287 | // 动态改变其他属性 288 | .setRowCount(3) 289 | .setColumnCount(3) 290 | // 再次show(),gridViewPager不为空时,走notifyDataSetChanged()刷新数据 291 | .show(); 292 | ``` 293 | 294 | **刷新指定页码数据(每页行列数、数据总数不变,只有某个数据的值改变时使用)** 295 | 296 | ``` 297 | // 页码从0开始,例如下面是只刷新第二页数据 298 | gridViewPager.notifyItemChanged(1) 299 | ``` 300 | 301 | ## 实现就是如此简单 302 | 303 | **添加我个人微信号交流,记得添加时备注一下哦** 304 | 305 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20200629201307276.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI4Nzc5MDgz,size_16,color_FFFFFF,t_70) 306 | 307 | **本人公众号,也可关注一波,共同交流吧。** 308 | 309 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/2019012509485178.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI4Nzc5MDgz,size_16,color_FFFFFF,t_70) 310 | 311 | -------------------------------------------------------------------------------- /README_1.x.md: -------------------------------------------------------------------------------- 1 | 2 | # GridPager 3 | 4 | GridPager组件:ViewPager结合GridView,轻松实现类似美团首页分类多页展示。也可用于表情面板的展示。 5 | 链式调用,属性配置,几行代码轻松搞定。 6 | 7 | ① 应用的首页经常需要用到这样的分类**多页展示**的效果,还有些消息输入框需要这样的**表情面板**。 8 | 9 | ② 既然是常用的,作为懒惰的我,肯定不会每次都去写一遍。网上也找了很多类似的例子,但始终不是我想要的**简洁接入**使用的方式。要么就是加载图片有限制,要么就是样式限制的太死,还得改源码,我不喜欢,我得造一个轮子。。。必须封装一个简单好用的组件,做到几行代码就可实现效果才行。于是乎,**GridPager组件**就诞生了。 10 | 11 | # GridPager组件效果 12 | 13 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/20190801185758323.gif) 14 | 15 | # 如何使用GridPager组件 16 | 17 | # 1、在根目录 build.gradle 添加: 18 | 19 | ``` 20 | allprojects { 21 | repositories { 22 | ... 23 | maven { url 'https://jitpack.io' } 24 | } 25 | } 26 | ``` 27 | 28 | # 2、在app项目下的build.gradle中添加: 29 | 30 | ``` 31 | dependencies { 32 | implementation 'com.github.mtjsoft:GridPager:v1.3.2' 33 | } 34 | ``` 35 | 36 | 37 | # 3、在需要使用的布局xml中添加GridPager组件,根据需要设置相关属性 38 | 39 | ``` 40 | 62 | 63 | ``` 64 | # 4、GridPager组件的版本及属性说明 65 | 66 | V1.3.2 67 | -------------------------- 68 | 69 | 新增方法 | 属性说明 | 备注 70 | ------------- | ------------- | ------------- 71 | setGridPagersetBackgroundImage() | 支持设置背景图片,不再是单一的背景颜色了 | 2019-11-8 17:52:08 72 | 73 | V1.3.1 74 | -------------------------- 75 | 76 | 说明 | 备注 77 | ------------- | ------------- 78 | 优化自动计算高度方法。不手动设置setViewPageHeight()固定高度时,默认自动计算,自适应高度。 | 2019-9-28 20:48:57 79 | 80 | V1.2.2 81 | -------------------------- 82 | 83 | 新增方法 | 属性说明 | 备注 84 | ------------- | ------------- | ------------- 85 | setViewPageHeight(152) | 手动设置ViewPager为固定的高度,单位dp。 | 2019-9-27 17:47:31 86 | 87 | V1.2.1 88 | -------------------------- 89 | 90 | 新增属性 | 属性说明 | 备注 91 | ------------- | ------------- | ------------- 92 | background_color | 设置组件背景颜色,默认白色 | 2019-9-23 22:20:05 93 | 94 | V1.2.0 95 | -------------------------- 96 | 97 | 新增属性 | 属性说明 | 备注 98 | ------------- | ------------- | ------------- 99 | point_margin_page | 设置指示器与page的间距,默认是verticalSpacing的值 | 2019-9-11 13:20:45 100 | point_margin_bottom | 设置指示器与底部的间距,默认是verticalSpacing的值 101 | point_is_show | 是否展示指示器,默认true展示 102 | 103 | V1.1.0 104 | -------------------------- 105 | 106 | 1.1.0 属性 | 属性说明 | 备注 107 | ------------- | ------------- | ------------- 108 | verticalSpacing | 设置item的纵向间距 默认10dp 单位dp | 2019-8-22 16:09:55 109 | img_width | 设置图片宽度 默认50dp 单位dp 110 | img_height | 设置图片高度 默认50dp 单位dp 111 | text_color | 设置文字颜色 默认黑色 112 | imgtext_margin | 设置文字与图片的间距 默认5dp 单位dp 113 | text_size | 设置文字大小 默认10sp 单位sp 114 | row_count | 设置每页行数 默认2 115 | column_count | 设置每页列数 默认4 116 | point_width | 设置指示器的item宽度 默认8dp 单位dp 117 | point_height | 设置指示器的item高度 默认8dp 单位dp 118 | point_margin | 设置指示器的item的间距 默认8dp 单位dp 119 | point_normal_color | 指示器item未选中的颜色 默认灰色 120 | point_select_color | 指示器item选中的颜色 默认红色 121 | point_is_circle | 指示器的item是否为圆形,默认圆形直径取宽高的最小值 122 | 123 | 124 | # 5、代码实现。链式调用,只需要设置总数量即可。数据绑定完全自定义,不受任何图片加载框架限制,更加自由。 125 | 126 | ``` 127 | GridPager gridPager = findViewById(R.id.gridpager); 128 | gridPager 129 | // 设置数量总条数 130 | .setDataAllCount(titles.length) 131 | // 数据绑定 132 | .setItemBindDataListener(new GridPager.ItemBindDataListener() { 133 | @Override 134 | public void BindData(ImageView imageView, TextView textView, int position) { 135 | // 自己进行数据的绑定,灵活度更高,不受任何限制 136 | imageView.setImageResource(iconS[position]); 137 | textView.setText(titles[position]); 138 | } 139 | }) 140 | // Item点击 141 | .setGridItemClickListener(new GridPager.GridItemClickListener() { 142 | @Override 143 | public void click(int position) { 144 | Toast.makeText(getBaseContext(), "点击了" + titles[position], Toast.LENGTH_SHORT).show(); 145 | } 146 | }) 147 | .show(); 148 | ``` 149 | 150 | # 6、代码中也可直接设置属性(如果xml与代码都设置了, 最终以代码设置为准) 151 | 152 | ``` 153 | gridPager 154 | // 设置数量总条数 155 | .setDataAllCount(titles.length) 156 | // 设置背景图片(有背景图片时,设置背景颜色无效,采用的Glide加载) 157 | .setGridPagersetBackgroundImage("https://huilife.api.luoyangzixun.cn/XiaoFile/index-miandan-bg.png") 158 | // 手动设置ViewPager为固定的高度,单位dp。(可以不设置,默认是自动计算高度) 159 | //.setViewPageHeight(152) 160 | // 设置背景色,默认白色 161 | .setGridPagerBackgroundColor(ContextCompat.getColor(getBaseContext(),R.color.colorBg)) 162 | // 设置item的纵向间距 163 | .setVerticalSpacing(20) 164 | // 设置图片宽度 165 | .setImageWidth(80) 166 | // 设置图片高度 167 | .setImageHeight(80) 168 | // 设置文字与图片的间距 169 | .setTextImgMargin(10) 170 | // 设置文字颜色 171 | .setTextColor(ContextCompat.getColor(getBaseContext(),R.color.colorPrimaryDark)) 172 | // 设置文字大小 173 | .setTextSize(12) 174 | // 设置每页行数 175 | .setRowCount(2) 176 | // 设置每页列数 177 | .setColumnCount(4) 178 | // 设置是否显示指示器 179 | .setPointIsShow(true) 180 | // 设置指示器与page的间距 181 | .setPointMarginPage(10) 182 | // 设置指示器与底部的间距 183 | .setPointMarginBottom(10) 184 | // 设置指示器的item宽度 185 | .setPointChildWidth(15) 186 | // 设置指示器的item高度 187 | .setPointChildHeight(3) 188 | // 设置指示器的item的间距 189 | .setPointChildMargin(5) 190 | // 指示器的item是否为圆形,默认圆形直径取宽高的最小值 191 | .setPointIsCircle(false) 192 | // 指示器item未选中的颜色 193 | .setPointNormalColor(ContextCompat.getColor(getBaseContext(),R.color.colorPrimary)) 194 | // 指示器item选中的颜色 195 | .setPointSelectColor(ContextCompat.getColor(getBaseContext(),R.color.colorAccent)) 196 | // 数据绑定 197 | .setItemBindDataListener(new GridPager.ItemBindDataListener() { 198 | @Override 199 | public void BindData(ImageView imageView, TextView textView, int position) { 200 | // 自己进行数据的绑定,灵活度更高,不受任何限制 201 | imageView.setImageResource(iconS[position]); 202 | textView.setText(titles[position]); 203 | } 204 | }) 205 | // Item点击 206 | .setGridItemClickListener(new GridPager.GridItemClickListener() { 207 | @Override 208 | public void click(int position) { 209 | Toast.makeText(getBaseContext(), "点击了" + titles[position], Toast.LENGTH_SHORT).show(); 210 | } 211 | }) 212 | .show(); 213 | ``` 214 | ## 实现就是如此简单 215 | 216 | **添加我个人微信号交流,记得添加时备注一下哦** 217 | 218 | 219 | 220 | **本人公众号,也可关注一波,共同交流吧。** 221 | 222 | ![在这里插入图片描述](https://img-blog.csdnimg.cn/2019012509485178.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI4Nzc5MDgz,size_16,color_FFFFFF,t_70) 223 | 224 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | buildToolsVersion "28.0.2" 6 | defaultConfig { 7 | applicationId "cn.mtjsoft.www.gridviewpager" 8 | minSdkVersion 19 9 | targetSdkVersion 28 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | implementation fileTree(include: ['*.jar'], dir: 'libs') 24 | implementation 'androidx.appcompat:appcompat:1.1.0' 25 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 26 | implementation 'com.google.android.material:material:1.1.0' 27 | implementation 'androidx.legacy:legacy-support-v4:1.0.0' 28 | testImplementation 'junit:junit:4.12' 29 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 30 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' 31 | implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:3.0.4' 32 | implementation project(':gridviewpager_recycleview') 33 | // implementation 'com.github.mtjsoft:GridPager:v3.4.0' 34 | } 35 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/cn/mtjsoft/www/gridviewpager/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package cn.mtjsoft.www.gridviewpager; 2 | 3 | import android.content.Context; 4 | 5 | import org.junit.Test; 6 | import org.junit.runner.RunWith; 7 | 8 | import androidx.test.InstrumentationRegistry; 9 | import androidx.test.runner.AndroidJUnit4; 10 | 11 | import static org.junit.Assert.*; 12 | 13 | /** 14 | * Instrumented test, which will execute on an Android device. 15 | * 16 | * @see Testing documentation 17 | */ 18 | @RunWith(AndroidJUnit4.class) 19 | public class ExampleInstrumentedTest { 20 | @Test 21 | public void useAppContext() { 22 | // Context of the app under test. 23 | Context appContext = InstrumentationRegistry.getTargetContext(); 24 | 25 | assertEquals("cn.mtjsoft.www.gridviewpager", appContext.getPackageName()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/java/cn/mtjsoft/www/gridviewpager/CoordinatorActivity.java: -------------------------------------------------------------------------------- 1 | package cn.mtjsoft.www.gridviewpager; 2 | 3 | import android.content.Context; 4 | import android.os.Bundle; 5 | import android.view.LayoutInflater; 6 | import android.view.View; 7 | import android.view.ViewGroup; 8 | import android.widget.ImageView; 9 | import android.widget.TextView; 10 | import android.widget.Toast; 11 | 12 | import androidx.annotation.NonNull; 13 | import androidx.annotation.Nullable; 14 | import androidx.appcompat.app.AppCompatActivity; 15 | import androidx.recyclerview.widget.RecyclerView; 16 | import androidx.viewpager2.widget.ViewPager2; 17 | 18 | import cn.mtjsoft.www.gridviewpager_recycleview.GridViewPager; 19 | 20 | public class CoordinatorActivity extends AppCompatActivity { 21 | 22 | private String[] titles = {"美食", "电影", "酒店住宿", "休闲娱乐", "外卖", "自助餐", "KTV", "机票/火车票", "周边游", "美甲美睫", 23 | "火锅", "生日蛋糕", "甜品饮品", "水上乐园", "汽车服务", "美发", "丽人", "景点", "足疗按摩", "运动健身", "健身", "超市", "买菜", 24 | "今日新单", "小吃快餐", "面膜", "洗浴/汗蒸", "母婴亲子", "生活服务", "婚纱摄影", "学习培训", "家装", "结婚", "全部分配"}; 25 | 26 | private int[] iconS = new int[titles.length]; 27 | 28 | @Override 29 | protected void onCreate(@Nullable Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | setContentView(R.layout.activity_coordinatorlayout); 32 | initData(); 33 | GridViewPager gridViewPager2 = findViewById(R.id.gridviewpager2); 34 | gridViewPager2 35 | // 设置数据总数量 36 | .setDataAllCount(titles.length) 37 | // 设置内置的覆盖翻页效果 38 | .setCoverPageTransformer() 39 | // 数据绑定 40 | .setImageTextLoaderInterface(new GridViewPager.ImageTextLoaderInterface() { 41 | @Override 42 | public void displayImageText(ImageView imageView, TextView textView, int position) { 43 | // 自己进行数据的绑定,灵活度更高,不受任何限制 44 | imageView.setImageResource(iconS[position]); 45 | textView.setText(titles[position]); 46 | } 47 | }) 48 | // Item点击 49 | .setGridItemClickListener(new GridViewPager.GridItemClickListener() { 50 | @Override 51 | public void click(int position) { 52 | Toast.makeText(getBaseContext(), "点击了" + titles[position] + position, Toast.LENGTH_SHORT).show(); 53 | } 54 | }) 55 | .show(); 56 | ViewPager2 viewPager2 = findViewById(R.id.viewPager); 57 | viewPager2.setAdapter(new PagerAdapter(getBaseContext())); 58 | } 59 | 60 | private class PagerAdapter extends RecyclerView.Adapter { 61 | private Context context; 62 | 63 | public PagerAdapter(Context context) { 64 | this.context = context; 65 | } 66 | 67 | @NonNull 68 | @Override 69 | public PagerAdapter.Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { 70 | View view = LayoutInflater.from(context).inflate(R.layout.item_text, parent, false); 71 | ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 72 | view.setLayoutParams(lp); 73 | return new PagerAdapter.Holder(view); 74 | } 75 | 76 | @Override 77 | public void onBindViewHolder(@NonNull PagerAdapter.Holder holder, int position) { 78 | holder.textView.setText("ViewPager_" + (position + 1)); 79 | } 80 | 81 | @Override 82 | public int getItemCount() { 83 | return 10; 84 | } 85 | 86 | public class Holder extends RecyclerView.ViewHolder { 87 | private TextView textView; 88 | 89 | public Holder(@NonNull View itemView) { 90 | super(itemView); 91 | textView = itemView.findViewById(R.id.tv_name); 92 | } 93 | } 94 | } 95 | 96 | /** 97 | * 初始化数据源,这里使用本地图标作为示例 98 | */ 99 | private void initData() { 100 | for (int i = 0; i < titles.length; i++) { 101 | //动态获取资源ID,第一个参数是资源名,第二个参数是资源类型例如drawable,string等,第三个参数包名 102 | int imageId = getResources().getIdentifier("ic_category_" + i, "mipmap", getPackageName()); 103 | iconS[i] = imageId; 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /app/src/main/java/cn/mtjsoft/www/gridviewpager/ListActivity.java: -------------------------------------------------------------------------------- 1 | package cn.mtjsoft.www.gridviewpager; 2 | 3 | import android.os.Bundle; 4 | import android.widget.ImageView; 5 | import android.widget.TextView; 6 | import android.widget.Toast; 7 | 8 | import java.util.Arrays; 9 | 10 | import androidx.annotation.Nullable; 11 | import androidx.appcompat.app.AppCompatActivity; 12 | import androidx.core.content.ContextCompat; 13 | import androidx.recyclerview.widget.LinearLayoutManager; 14 | import androidx.recyclerview.widget.RecyclerView; 15 | import cn.mtjsoft.www.gridviewpager_recycleview.GridViewPager; 16 | 17 | public class ListActivity extends AppCompatActivity { 18 | 19 | private String[] datas = {"美食", "电影", "酒店住宿", "休闲娱乐", "外卖", "自助餐", "KTV", "机票/火车票", "周边游", "美甲美睫", 20 | "火锅", "生日蛋糕", "甜品饮品", "水上乐园", "汽车服务", "美发", "丽人", "景点", "足疗按摩", "运动健身", "健身", "超市", "买菜", 21 | "今日新单", "小吃快餐", "面膜", "洗浴/汗蒸", "母婴亲子", "生活服务", "婚纱摄影", "学习培训", "家装", "结婚", "全部分配"}; 22 | 23 | private int[] iconS = new int[datas.length]; 24 | 25 | @Override 26 | protected void onCreate(@Nullable Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_list); 29 | 30 | RecyclerView recyclerView = findViewById(R.id.rlv); 31 | recyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext())); 32 | 33 | for (int i = 0; i < datas.length; i++) { 34 | //动态获取资源ID,第一个参数是资源名,第二个参数是资源类型例如drawable,string等,第三个参数包名 35 | int imageId = getResources().getIdentifier("ic_category_" + i, "mipmap", getPackageName()); 36 | iconS[i] = imageId; 37 | } 38 | 39 | 40 | ListAdapter adapter = new ListAdapter(Arrays.asList(datas)); 41 | adapter.addHeaderView(getGridViewPager()); 42 | recyclerView.setAdapter(adapter); 43 | } 44 | 45 | 46 | private GridViewPager getGridViewPager(){ 47 | GridViewPager gridViewPager = new GridViewPager(getBaseContext()); 48 | gridViewPager 49 | // 设置数据总数量 50 | .setDataAllCount(datas.length) 51 | // 设置背景色,默认白色 52 | .setGridViewPagerBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.white)) 53 | // 设置item的纵向间距 54 | .setVerticalSpacing(10) 55 | // 设置上边距 56 | .setPagerMarginTop(10) 57 | // 设置下边距 58 | .setPagerMarginBottom(10) 59 | // 设置图片宽度 60 | .setImageWidth(50) 61 | // 设置图片高度 62 | .setImageHeight(50) 63 | // 设置文字与图片的间距 64 | .setTextImgMargin(5) 65 | // 设置文字颜色 66 | .setTextColor(ContextCompat.getColor(getBaseContext(), R.color.white)) 67 | // 设置文字大小 68 | .setTextSize(12) 69 | // 设置每页行数 70 | .setRowCount(3) 71 | // 设置每页列数 72 | .setColumnCount(3) 73 | // 设置是否显示指示器 74 | .setPointIsShow(true) 75 | // 设置指示器与page的间距 76 | .setPointMarginPage(10) 77 | // 设置指示器与底部的间距 78 | .setPointMarginBottom(10) 79 | // 设置指示器的item宽度 80 | .setPointChildWidth(8) 81 | // 设置指示器的item高度 82 | .setPointChildHeight(8) 83 | // 设置指示器的item的间距 84 | .setPointChildMargin(8) 85 | // 指示器的item是否为圆形,默认圆形直径取宽高的最小值 86 | .setPointIsCircle(true) 87 | // 指示器item未选中的颜色 88 | .setPointNormalColor(ContextCompat.getColor(getBaseContext(), R.color.white)) 89 | // 指示器item选中的颜色 90 | .setPointSelectColor(ContextCompat.getColor(getBaseContext(), R.color.black_text)) 91 | // 设置背景图片(此时设置的背景色无效,以背景图片为主) 92 | .setBackgroundImageLoader(new GridViewPager.BackgroundImageLoaderInterface() { 93 | @Override 94 | public void setBackgroundImg(ImageView bgImageView) { 95 | bgImageView.setImageResource(R.drawable.ic_launcher_background_red); 96 | } 97 | }) 98 | // 数据绑定 99 | .setImageTextLoaderInterface(new GridViewPager.ImageTextLoaderInterface() { 100 | @Override 101 | public void displayImageText(ImageView imageView, TextView textView, int position) { 102 | // 自己进行数据的绑定,灵活度更高,不受任何限制 103 | imageView.setImageResource(iconS[position]); 104 | textView.setText(datas[position]); 105 | } 106 | }) 107 | // Item点击 108 | .setGridItemClickListener(new GridViewPager.GridItemClickListener() { 109 | @Override 110 | public void click(int position) { 111 | Toast.makeText(getBaseContext(), "点击了" + datas[position] + position, Toast.LENGTH_SHORT).show(); 112 | } 113 | }) 114 | .show(); 115 | return gridViewPager; 116 | } 117 | } -------------------------------------------------------------------------------- /app/src/main/java/cn/mtjsoft/www/gridviewpager/ListAdapter.java: -------------------------------------------------------------------------------- 1 | package cn.mtjsoft.www.gridviewpager; 2 | 3 | import com.chad.library.adapter.base.BaseQuickAdapter; 4 | import com.chad.library.adapter.base.viewholder.BaseViewHolder; 5 | 6 | import org.jetbrains.annotations.NotNull; 7 | import org.jetbrains.annotations.Nullable; 8 | 9 | import java.util.List; 10 | 11 | public class ListAdapter extends BaseQuickAdapter { 12 | 13 | public ListAdapter(@Nullable List data) { 14 | super(R.layout.item_text, data); 15 | } 16 | 17 | @Override 18 | protected void convert(@NotNull BaseViewHolder baseViewHolder, String s) { 19 | baseViewHolder.setText(R.id.tv_name, s); 20 | } 21 | } -------------------------------------------------------------------------------- /app/src/main/java/cn/mtjsoft/www/gridviewpager/MainActivity.java: -------------------------------------------------------------------------------- 1 | package cn.mtjsoft.www.gridviewpager; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | import android.widget.Button; 7 | import android.widget.ImageView; 8 | import android.widget.RadioGroup; 9 | import android.widget.SeekBar; 10 | import android.widget.TextView; 11 | import android.widget.Toast; 12 | 13 | import com.google.android.flexbox.AlignContent; 14 | 15 | import androidx.appcompat.app.AppCompatActivity; 16 | import androidx.core.content.ContextCompat; 17 | 18 | import java.util.Random; 19 | 20 | import cn.mtjsoft.www.gridviewpager_recycleview.GridViewPager; 21 | import cn.mtjsoft.www.gridviewpager_recycleview.transformer.TopOrDownPageTransformer; 22 | 23 | public class MainActivity extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener { 24 | 25 | private String[] titles = {"美食", "电影", "酒店住宿", "休闲娱乐", "外卖", "自助餐", "KTV", "机票/火车票", "周边游", "美甲美睫", 26 | "火锅", "生日蛋糕", "甜品饮品", "水上乐园", "汽车服务", "美发", "丽人", "景点", "足疗按摩", "运动健身", "健身", "超市", "买菜", 27 | "今日新单", "小吃快餐", "面膜", "洗浴/汗蒸", "母婴亲子", "生活服务", "婚纱摄影", "学习培训", "家装", "结婚", "全部分配"}; 28 | 29 | private int[] iconS = new int[titles.length]; 30 | 31 | private int rowCount = 3; 32 | private int columnCount = 4; 33 | // 指定刷新某一页数据 34 | private int page = 0; 35 | 36 | // 37 | private GridViewPager gridViewPager; 38 | private TextView tv_row; 39 | private TextView tv_column; 40 | private TextView tv_page; 41 | 42 | 43 | private Random random = new Random(); 44 | 45 | @Override 46 | protected void onCreate(Bundle savedInstanceState) { 47 | super.onCreate(savedInstanceState); 48 | setContentView(R.layout.activity_main); 49 | 50 | initData(); 51 | 52 | 53 | gridViewPager = findViewById(R.id.gridviewpager); 54 | gridViewPager 55 | // 设置数据总数量 56 | .setDataAllCount(18) 57 | // 设置背景色,默认白色 58 | .setGridViewPagerBackgroundColor(ContextCompat.getColor(getBaseContext(), R.color.white)) 59 | // 设置item的纵向间距 60 | .setVerticalSpacing(10) 61 | // 设置上边距 62 | .setPagerMarginTop(10) 63 | // 设置下边距 64 | .setPagerMarginBottom(10) 65 | // 设置图片宽度 66 | .setImageWidth(50) 67 | // 设置图片高度 68 | .setImageHeight(50) 69 | // 设置是否显示文本 70 | .setIsShowText(true) 71 | // 设置子VIEW对齐方式 72 | .setAlignContent(AlignContent.FLEX_START) 73 | // 设置文字与图片的间距 74 | .setTextImgMargin(5) 75 | // 设置文字颜色 76 | .setTextColor(ContextCompat.getColor(getBaseContext(), R.color.white)) 77 | // 设置文字大小 78 | .setTextSize(12) 79 | // 设置每页行数 80 | .setRowCount(rowCount) 81 | // 设置每页列数 82 | .setColumnCount(columnCount) 83 | // 设置无限循环 84 | .setPageLoop(true) 85 | // 设置是否显示指示器 86 | .setPointIsShow(true) 87 | // 设置指示器与page的间距 88 | .setPointMarginPage(0) 89 | // 设置指示器与底部的间距 90 | .setPointMarginBottom(10) 91 | // 设置指示器的item宽度 92 | .setPointChildWidth(8) 93 | // 设置指示器的item高度 94 | .setPointChildHeight(8) 95 | // 设置指示器的item的间距 96 | .setPointChildMargin(8) 97 | // 指示器的item是否为圆形,默认圆形直径取宽高的最小值 98 | .setPointIsCircle(true) 99 | // 指示器item未选中的颜色 100 | .setPointNormalColor(ContextCompat.getColor(getBaseContext(), R.color.white)) 101 | // 指示器item选中的颜色 102 | .setPointSelectColor(ContextCompat.getColor(getBaseContext(), R.color.black_text)) 103 | // 设置背景图片(此时设置的背景色无效,以背景图片为主) 104 | .setBackgroundImageLoader(new GridViewPager.BackgroundImageLoaderInterface() { 105 | @Override 106 | public void setBackgroundImg(ImageView bgImageView) { 107 | bgImageView.setImageResource(R.drawable.ic_launcher_background_red); 108 | } 109 | }) 110 | // 数据绑定 111 | .setImageTextLoaderInterface(new GridViewPager.ImageTextLoaderInterface() { 112 | @Override 113 | public void displayImageText(ImageView imageView, TextView textView, int position) { 114 | // 自己进行数据的绑定,灵活度更高,不受任何限制 115 | imageView.setImageResource(iconS[position]); 116 | if (textView != null) { 117 | textView.setText(titles[position]); 118 | } 119 | } 120 | }) 121 | // Item点击 122 | .setGridItemClickListener(new GridViewPager.GridItemClickListener() { 123 | @Override 124 | public void click(int position) { 125 | Toast.makeText(getBaseContext(), "点击了" + titles[position].split("_")[0], Toast.LENGTH_SHORT).show(); 126 | } 127 | }) 128 | // 设置Item长按 129 | .setGridItemLongClickListener(new GridViewPager.GridItemLongClickListener() { 130 | @Override 131 | public void longClick(int position) { 132 | Toast.makeText(getBaseContext(), "长按了" + titles[position].split("_")[0], Toast.LENGTH_SHORT).show(); 133 | } 134 | }) 135 | .show(); 136 | 137 | // 138 | tv_row = findViewById(R.id.tv_row); 139 | tv_column = findViewById(R.id.tv_column); 140 | tv_page = findViewById(R.id.tv_page); 141 | SeekBar sb_row = findViewById(R.id.sb_row); 142 | SeekBar sb_column = findViewById(R.id.sb_column); 143 | SeekBar sb_page = findViewById(R.id.sb_page); 144 | sb_row.setOnSeekBarChangeListener(this); 145 | sb_column.setOnSeekBarChangeListener(this); 146 | sb_page.setOnSeekBarChangeListener(this); 147 | 148 | // 刷新 149 | Button button = findViewById(R.id.btu_page); 150 | button.setOnClickListener(new View.OnClickListener() { 151 | @Override 152 | public void onClick(View v) { 153 | // 改变数据 154 | int x = random.nextInt(5); 155 | for (int i = 0; i < titles.length; i++) { 156 | titles[i] = titles[i].split("_")[0] + "_" + x; 157 | } 158 | // 刷新 159 | gridViewPager.setDataAllCount(titles.length).setRowCount(rowCount).setColumnCount(columnCount).show(); 160 | } 161 | }); 162 | // 刷新指定的第page页 163 | Button button2 = findViewById(R.id.btu_page2); 164 | button2.setOnClickListener(new View.OnClickListener() { 165 | @Override 166 | public void onClick(View v) { 167 | int pageSize = gridViewPager.getOnePageSize(); 168 | int x = random.nextInt(5) + 5; 169 | // 改变第page页的数据 170 | for (int i = 0; i < titles.length; i++) { 171 | if (i >= pageSize * page && i < pageSize * (page + 1)) { 172 | titles[i] = titles[i].split("_")[0] + "_" + x; 173 | } 174 | } 175 | // 刷新第page页的数据 176 | gridViewPager.notifyItemChanged(page); 177 | } 178 | }); 179 | 180 | // 测试翻页效果 181 | final GridViewPager gridViewPager2 = findViewById(R.id.gridviewpager2); 182 | gridViewPager2 183 | // 设置数据总数量 184 | .setDataAllCount(titles.length) 185 | // 设置内置的覆盖翻页效果 186 | .setCoverPageTransformer() 187 | // 设置内置的上下进入效果 188 | // .setTopOrDownPageTransformer(TopOrDownPageTransformer.ModeType.MODE_DOWN) 189 | // 设置内置的画廊效果 190 | // .setGalleryPageTransformer() 191 | // 数据绑定 192 | .setImageTextLoaderInterface(new GridViewPager.ImageTextLoaderInterface() { 193 | @Override 194 | public void displayImageText(ImageView imageView, TextView textView, int position) { 195 | // 自己进行数据的绑定,灵活度更高,不受任何限制 196 | imageView.setImageResource(iconS[position]); 197 | textView.setText(titles[position].split("_")[0]); 198 | } 199 | }) 200 | // Item点击 201 | .setGridItemClickListener(new GridViewPager.GridItemClickListener() { 202 | @Override 203 | public void click(int position) { 204 | Toast.makeText(getBaseContext(), "点击了" + titles[position].split("_")[0], Toast.LENGTH_SHORT).show(); 205 | } 206 | }) 207 | // 设置Item长按 208 | .setGridItemLongClickListener(new GridViewPager.GridItemLongClickListener() { 209 | @Override 210 | public void longClick(int position) { 211 | Toast.makeText(getBaseContext(), "长按了" + titles[position].split("_")[0], Toast.LENGTH_SHORT).show(); 212 | } 213 | }) 214 | .show(); 215 | 216 | // 测试列表 217 | Button button3 = findViewById(R.id.btu_list); 218 | button3.setOnClickListener(new View.OnClickListener() { 219 | @Override 220 | public void onClick(View v) { 221 | startActivity(new Intent(getBaseContext(), ListActivity.class)); 222 | } 223 | }); 224 | 225 | // 测试嵌套滑动 226 | findViewById(R.id.btu_list2).setOnClickListener(new View.OnClickListener() { 227 | @Override 228 | public void onClick(View v) { 229 | startActivity(new Intent(getBaseContext(), CoordinatorActivity.class)); 230 | } 231 | }); 232 | } 233 | 234 | 235 | /** 236 | * 初始化数据源,这里使用本地图标作为示例 237 | */ 238 | private void initData() { 239 | for (int i = 0; i < titles.length; i++) { 240 | //动态获取资源ID,第一个参数是资源名,第二个参数是资源类型例如drawable,string等,第三个参数包名 241 | int imageId = getResources().getIdentifier("ic_category_" + i, "mipmap", getPackageName()); 242 | iconS[i] = imageId; 243 | } 244 | } 245 | 246 | @Override 247 | public void onProgressChanged(SeekBar seekBar, int i, boolean b) { 248 | switch (seekBar.getId()) { 249 | case R.id.sb_row: 250 | if (i < 1) { 251 | Toast.makeText(getBaseContext(), "最少设置一行", Toast.LENGTH_SHORT).show(); 252 | } else { 253 | tv_row.setText("设置行数:" + i); 254 | rowCount = i; 255 | } 256 | break; 257 | case R.id.sb_column: 258 | if (i < 1) { 259 | Toast.makeText(getBaseContext(), "最少设置一列", Toast.LENGTH_SHORT).show(); 260 | } else { 261 | tv_column.setText("设置列数:" + i); 262 | columnCount = i; 263 | } 264 | break; 265 | case R.id.sb_page: 266 | if (i > gridViewPager.getPageSize() - 1) { 267 | Toast.makeText(getBaseContext(), "超出页码", Toast.LENGTH_SHORT).show(); 268 | } else { 269 | tv_page.setText("指定刷新页:" + i); 270 | page = i; 271 | } 272 | break; 273 | } 274 | } 275 | 276 | @Override 277 | public void onStartTrackingTouch(SeekBar seekBar) { 278 | 279 | } 280 | 281 | @Override 282 | public void onStopTrackingTouch(SeekBar seekBar) { 283 | 284 | } 285 | } 286 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_arrow_white_24dp.xml: -------------------------------------------------------------------------------- 1 | 6 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background_red.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_coordinatorlayout.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 12 | 13 | 17 | 18 | 23 | 24 | 30 | 31 | 32 | 33 | 34 | 35 | 40 | 41 | 45 | 46 | 70 | 71 | 78 | 79 | 88 | 89 | 98 | 99 | 108 | 109 | 118 | 119 | 128 | 129 | 138 | 139 | 140 | 141 | 142 | 143 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_list.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 9 | 10 | 15 | 16 | 40 | 41 | 46 | 47 | 53 | 54 | 61 | 62 | 63 | 64 | 69 | 70 | 76 | 77 | 84 | 85 | 86 | 87 | 92 | 93 | 99 | 100 | 106 | 107 | 108 | 109 |