├── .gitignore ├── LICENSE ├── README.md ├── fixed_item.gif ├── library_zhy_viewpagerIndicator ├── .classpath ├── .project ├── AndroidManifest.xml ├── ic_launcher-web.png ├── libs │ └── android-support-v4.jar ├── proguard-project.txt ├── project.properties ├── res │ ├── drawable-hdpi │ │ └── ic_launcher.png │ ├── drawable-mdpi │ │ └── ic_launcher.png │ ├── drawable-xhdpi │ │ └── ic_launcher.png │ ├── drawable-xxhdpi │ │ └── ic_launcher.png │ ├── values-v11 │ │ └── styles.xml │ ├── values-v14 │ │ └── styles.xml │ └── values │ │ ├── attr_vpi.xml │ │ ├── strings.xml │ │ └── styles.xml └── src │ └── com │ └── zhy │ └── view │ └── ViewPagerIndicator.java ├── more_items.gif └── sample_zhy_viewpagerIndicator ├── .classpath ├── .project ├── AndroidManifest.xml ├── ic_launcher-web.png ├── libs └── android-support-v4.jar ├── proguard-project.txt ├── project.properties ├── res ├── drawable-hdpi │ ├── ic_launcher.png │ ├── recent_contact_list_bg.9.png │ └── title_bar_bg_one_row.9.png ├── drawable-mdpi │ └── ic_launcher.png ├── drawable-xhdpi │ └── ic_launcher.png ├── drawable-xxhdpi │ └── ic_launcher.png ├── layout │ ├── activity_main.xml │ ├── item_msg.xml │ └── vp_indicator.xml ├── menu │ └── main.xml ├── values-sw600dp │ └── dimens.xml ├── values-sw720dp-land │ └── dimens.xml ├── values-v11 │ └── styles.xml ├── values-v14 │ └── styles.xml └── values │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml └── src └── com └── zhy └── sample └── viewpagerindicator ├── MainActivity.java └── VpSimpleFragment.java /.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 | -------------------------------------------------------------------------------- /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 | 203 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android-ViewPagerIndicator 2 | 一款仿MIUI的ViewPagerIndicator,支持Tab数量随意定义。 3 | 4 | # 效果图 5 | 6 | ## 固定tab数量 7 | ![Sample Screenshots][1] 8 | ## 非固定tab数量 9 | ![Sample Screenshots][2] 10 | 11 | # 用法 12 | 13 | ## 代码生成tab 14 | 15 | mIndicator.setVisibleTabCount(4);//设置可见Tab数量 16 | mIndicator.setTabItemTitles(mDatas);//设置tab标题 17 | mViewPager.setAdapter(mAdapter); 18 | mIndicator.setViewPager(mViewPager,0);//绑定ViewPager 19 | 20 | ## 直接布局中书写 21 | 布局中编写,则代码中只需要绑定ViewPager一行代码。 22 | 23 | 30 | 31 | 38 | 39 | 47 | 48 | 56 | 57 | 65 | 66 | 74 | 75 | 76 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | #关于我 88 | 89 | [我的博客地址][3] 90 | 91 | [1]: https://github.com/hongyangAndroid/Android-ViewPagerIndicator/blob/master/fixed_item.gif 92 | [2]: https://github.com/hongyangAndroid/Android-ViewPagerIndicator/blob/master/more_items.gif 93 | [3]: http://blog.csdn.net/lmj623565791 94 | -------------------------------------------------------------------------------- /fixed_item.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/Android-ViewPagerIndicator/fdd51835e44560129e3aa3c81087c58d3bc6aeff/fixed_item.gif -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | library_zhy_viewpagerIndicator 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | 30 | com.android.ide.eclipse.adt.AndroidNature 31 | org.eclipse.jdt.core.javanature 32 | 33 | 34 | -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 9 | 10 | 15 | 16 | 17 | 18 | -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/Android-ViewPagerIndicator/fdd51835e44560129e3aa3c81087c58d3bc6aeff/library_zhy_viewpagerIndicator/ic_launcher-web.png -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/libs/android-support-v4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/Android-ViewPagerIndicator/fdd51835e44560129e3aa3c81087c58d3bc6aeff/library_zhy_viewpagerIndicator/libs/android-support-v4.jar -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-19 15 | android.library=true 16 | -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/Android-ViewPagerIndicator/fdd51835e44560129e3aa3c81087c58d3bc6aeff/library_zhy_viewpagerIndicator/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/Android-ViewPagerIndicator/fdd51835e44560129e3aa3c81087c58d3bc6aeff/library_zhy_viewpagerIndicator/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/Android-ViewPagerIndicator/fdd51835e44560129e3aa3c81087c58d3bc6aeff/library_zhy_viewpagerIndicator/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/Android-ViewPagerIndicator/fdd51835e44560129e3aa3c81087c58d3bc6aeff/library_zhy_viewpagerIndicator/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/res/values/attr_vpi.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | library_zhy_viewpagerIndicator 4 | 5 | 6 | -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /library_zhy_viewpagerIndicator/src/com/zhy/view/ViewPagerIndicator.java: -------------------------------------------------------------------------------- 1 | package com.zhy.view; 2 | 3 | import java.util.List; 4 | 5 | import android.content.Context; 6 | import android.content.res.TypedArray; 7 | import android.graphics.Canvas; 8 | import android.graphics.Color; 9 | import android.graphics.CornerPathEffect; 10 | import android.graphics.Paint; 11 | import android.graphics.Paint.Style; 12 | import android.graphics.Path; 13 | import android.support.v4.view.ViewPager; 14 | import android.support.v4.view.ViewPager.OnPageChangeListener; 15 | import android.util.AttributeSet; 16 | import android.util.DisplayMetrics; 17 | import android.util.Log; 18 | import android.util.TypedValue; 19 | import android.view.Gravity; 20 | import android.view.View; 21 | import android.view.WindowManager; 22 | import android.widget.LinearLayout; 23 | import android.widget.TextView; 24 | 25 | import com.zhy.library.viewpagerindicator.R; 26 | 27 | /** 28 | * 29 | * @author hongyangAndroid 30 | http://blog.csdn.net/lmj623565791/ 31 | * 32 | */ 33 | public class ViewPagerIndicator extends LinearLayout 34 | { 35 | 36 | private Paint mPaint; 37 | 38 | private Path mPath; 39 | 40 | private int mTriangleWidth; 41 | 42 | private int mTriangleHeight; 43 | 44 | private static final float RADIO_TRIANGLE_WIDTH = 1 / 6F; 45 | /** 46 | * 三角形底边的最大宽度 47 | */ 48 | private final int DIMENSION_TRIANGLE_WIDTH_MAX = (int) (getScreenWidth() / 3 * RADIO_TRIANGLE_WIDTH); 49 | 50 | private int mInitTranslationX; 51 | 52 | private int mTranslationX; 53 | 54 | private int mTabVisibleCount; 55 | 56 | private static final int COUNT_DEFAULT_TAB = 4; 57 | private static final int COLOR_TEXT_NORMAL = 0x77FFFFFF; 58 | private static final int COLOR_TEXT_HIGHLIGHT = 0xFFFFFFFF; 59 | 60 | private List mTitles; 61 | 62 | public ViewPagerIndicator(Context context) 63 | { 64 | this(context, null); 65 | } 66 | 67 | public ViewPagerIndicator(Context context, AttributeSet attrs) 68 | { 69 | super(context, attrs); 70 | 71 | // 获取可见Tab的数量 72 | TypedArray a = context.obtainStyledAttributes(attrs, 73 | R.styleable.ViewPagerIndicator); 74 | 75 | mTabVisibleCount = a.getInt( 76 | R.styleable.ViewPagerIndicator_visible_tab_count, 77 | COUNT_DEFAULT_TAB); 78 | if (mTabVisibleCount < 0) 79 | { 80 | mTabVisibleCount = COUNT_DEFAULT_TAB; 81 | } 82 | a.recycle(); 83 | 84 | // 初始化画笔 85 | mPaint = new Paint(); 86 | mPaint.setAntiAlias(true); 87 | mPaint.setColor(Color.parseColor("#ffffffff")); 88 | mPaint.setStyle(Style.FILL); 89 | mPaint.setPathEffect(new CornerPathEffect(3)); 90 | 91 | } 92 | 93 | @Override 94 | protected void dispatchDraw(Canvas canvas) 95 | { 96 | 97 | canvas.save(); 98 | 99 | canvas.translate(mInitTranslationX + mTranslationX, getHeight() + 2); 100 | canvas.drawPath(mPath, mPaint); 101 | 102 | canvas.restore(); 103 | 104 | super.dispatchDraw(canvas); 105 | 106 | } 107 | 108 | @Override 109 | protected void onSizeChanged(int w, int h, int oldw, int oldh) 110 | { 111 | super.onSizeChanged(w, h, oldw, oldh); 112 | 113 | mTriangleWidth = (int) (w / mTabVisibleCount * RADIO_TRIANGLE_WIDTH); 114 | mTriangleWidth = Math.min(mTriangleWidth, DIMENSION_TRIANGLE_WIDTH_MAX); 115 | mInitTranslationX = w / mTabVisibleCount / 2 - mTriangleWidth / 2; 116 | 117 | initTriangle(); 118 | 119 | } 120 | 121 | @Override 122 | protected void onFinishInflate() 123 | { 124 | super.onFinishInflate(); 125 | 126 | int cCount = getChildCount(); 127 | if (cCount == 0) 128 | return; 129 | 130 | for (int i = 0; i < cCount; i++) 131 | { 132 | View view = getChildAt(i); 133 | LinearLayout.LayoutParams lp = (LayoutParams) view 134 | .getLayoutParams(); 135 | lp.weight = 0; 136 | lp.width = getScreenWidth() / mTabVisibleCount; 137 | view.setLayoutParams(lp); 138 | } 139 | 140 | setItemClickEvent(); 141 | 142 | } 143 | 144 | /** 145 | * 获得屏幕的宽度 146 | * 147 | * @return 148 | */ 149 | private int getScreenWidth() 150 | { 151 | WindowManager wm = (WindowManager) getContext().getSystemService( 152 | Context.WINDOW_SERVICE); 153 | DisplayMetrics outMetrics = new DisplayMetrics(); 154 | wm.getDefaultDisplay().getMetrics(outMetrics); 155 | return outMetrics.widthPixels; 156 | } 157 | 158 | /** 159 | * 初始化三角形 160 | */ 161 | private void initTriangle() 162 | { 163 | 164 | mTriangleHeight = mTriangleWidth / 2; 165 | 166 | mPath = new Path(); 167 | mPath.moveTo(0, 0); 168 | mPath.lineTo(mTriangleWidth, 0); 169 | mPath.lineTo(mTriangleWidth / 2, -mTriangleHeight); 170 | mPath.close(); 171 | 172 | } 173 | 174 | /** 175 | * 指示器跟随手指进行滚动 176 | * 177 | * @param position 178 | * @param offset 179 | */ 180 | public void scroll(int position, float offset) 181 | { 182 | int tabWidth = getWidth() / mTabVisibleCount; 183 | mTranslationX = (int) (tabWidth * (offset + position)); 184 | 185 | // 容器移动,在tab处于移动至最后一个时 186 | if (position >= (mTabVisibleCount - 2) && offset > 0 187 | && getChildCount() > mTabVisibleCount) 188 | { 189 | 190 | if (mTabVisibleCount != 1) 191 | { 192 | Log.e("TAG", 193 | ((position - (mTabVisibleCount - 2)) * tabWidth + (int) (tabWidth * offset)) 194 | + ""); 195 | this.scrollTo((position - (mTabVisibleCount - 2)) * tabWidth 196 | + (int) (tabWidth * offset), 0); 197 | } else 198 | { 199 | this.scrollTo(position * tabWidth + (int) (tabWidth * offset), 200 | 0); 201 | } 202 | 203 | } 204 | 205 | invalidate(); 206 | 207 | } 208 | 209 | public void setTabItemTitles(List titles) 210 | { 211 | if (titles != null && titles.size() > 0) 212 | { 213 | this.removeAllViews(); 214 | mTitles = titles; 215 | for (String title : mTitles) 216 | { 217 | addView(generateTextView(title)); 218 | } 219 | 220 | setItemClickEvent(); 221 | } 222 | } 223 | 224 | /** 225 | * 设置可见的Tab数量 226 | * 227 | * @param count 228 | */ 229 | public void setVisibleTabCount(int count) 230 | { 231 | mTabVisibleCount = count; 232 | } 233 | 234 | /** 235 | * 根据title创建Tab 236 | * 237 | * @param title 238 | * @return 239 | */ 240 | private View generateTextView(String title) 241 | { 242 | TextView tv = new TextView(getContext()); 243 | LinearLayout.LayoutParams lp = new LayoutParams( 244 | LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 245 | lp.width = getScreenWidth() / mTabVisibleCount; 246 | tv.setText(title); 247 | tv.setGravity(Gravity.CENTER); 248 | tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16); 249 | tv.setTextColor(COLOR_TEXT_NORMAL); 250 | tv.setLayoutParams(lp); 251 | return tv; 252 | } 253 | 254 | private ViewPager mViewPager; 255 | 256 | public interface PageOnchangeListener 257 | { 258 | public void onPageScrolled(int position, float positionOffset, 259 | int positionOffsetPixels); 260 | 261 | public void onPageSelected(int position); 262 | 263 | public void onPageScrollStateChanged(int state); 264 | } 265 | 266 | public PageOnchangeListener mListener; 267 | 268 | public void setOnPageChangeListener(PageOnchangeListener listener) 269 | { 270 | this.mListener = listener; 271 | } 272 | 273 | /** 274 | * 设置关联的ViewPager 275 | * 276 | * @param viewPager 277 | * @param pos 278 | */ 279 | public void setViewPager(ViewPager viewPager, int pos) 280 | { 281 | mViewPager = viewPager; 282 | mViewPager.setOnPageChangeListener(new OnPageChangeListener() 283 | { 284 | 285 | @Override 286 | public void onPageSelected(int position) 287 | { 288 | 289 | if (mListener != null) 290 | { 291 | mListener.onPageSelected(position); 292 | } 293 | 294 | highLightTextView(position); 295 | // 极端情况的Bug修复 296 | if (position <= (mTabVisibleCount - 2)) 297 | scrollTo(0, 0); 298 | 299 | } 300 | 301 | @Override 302 | public void onPageScrolled(int position, float positionOffset, 303 | int positionOffsetPixels) 304 | { 305 | scroll(position, positionOffset); 306 | if (mListener != null) 307 | { 308 | mListener.onPageScrolled(position, positionOffset, 309 | positionOffsetPixels); 310 | } 311 | } 312 | 313 | @Override 314 | public void onPageScrollStateChanged(int state) 315 | { 316 | 317 | if (mListener != null) 318 | { 319 | mListener.onPageScrollStateChanged(state); 320 | } 321 | 322 | } 323 | }); 324 | mViewPager.setCurrentItem(pos); 325 | highLightTextView(pos); 326 | } 327 | 328 | /** 329 | * 重置TAB文本颜色 330 | */ 331 | private void resetTextViewColor() 332 | { 333 | for (int i = 0; i < getChildCount(); i++) 334 | { 335 | View view = getChildAt(i); 336 | if (view instanceof TextView) 337 | { 338 | ((TextView) view).setTextColor(COLOR_TEXT_NORMAL); 339 | } 340 | } 341 | 342 | } 343 | 344 | /** 345 | * 高亮某个Tab的文本 346 | * 347 | * @param pos 348 | */ 349 | private void highLightTextView(int pos) 350 | { 351 | resetTextViewColor(); 352 | View view = getChildAt(pos); 353 | if (view instanceof TextView) 354 | { 355 | ((TextView) view).setTextColor(COLOR_TEXT_HIGHLIGHT); 356 | } 357 | } 358 | 359 | /** 360 | * 设置Tab的点击事件 361 | */ 362 | private void setItemClickEvent() 363 | { 364 | int cCount = getChildCount(); 365 | 366 | for (int i = 0; i < cCount; i++) 367 | { 368 | final int j = i; 369 | View view = getChildAt(i); 370 | 371 | view.setOnClickListener(new OnClickListener() 372 | { 373 | @Override 374 | public void onClick(View v) 375 | { 376 | mViewPager.setCurrentItem(j); 377 | } 378 | }); 379 | 380 | } 381 | 382 | } 383 | 384 | } 385 | -------------------------------------------------------------------------------- /more_items.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/Android-ViewPagerIndicator/fdd51835e44560129e3aa3c81087c58d3bc6aeff/more_items.gif -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/.classpath: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | sample_zhy_viewpagerIndicator 4 | 5 | 6 | 7 | 8 | 9 | com.android.ide.eclipse.adt.ResourceManagerBuilder 10 | 11 | 12 | 13 | 14 | com.android.ide.eclipse.adt.PreCompilerBuilder 15 | 16 | 17 | 18 | 19 | org.eclipse.jdt.core.javabuilder 20 | 21 | 22 | 23 | 24 | com.android.ide.eclipse.adt.ApkBuilder 25 | 26 | 27 | 28 | 29 | 30 | com.android.ide.eclipse.adt.AndroidNature 31 | org.eclipse.jdt.core.javanature 32 | 33 | 34 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 6 | 7 | 10 | 11 | 16 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/ic_launcher-web.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/Android-ViewPagerIndicator/fdd51835e44560129e3aa3c81087c58d3bc6aeff/sample_zhy_viewpagerIndicator/ic_launcher-web.png -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/libs/android-support-v4.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/Android-ViewPagerIndicator/fdd51835e44560129e3aa3c81087c58d3bc6aeff/sample_zhy_viewpagerIndicator/libs/android-support-v4.jar -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/proguard-project.txt: -------------------------------------------------------------------------------- 1 | # To enable ProGuard in your project, edit project.properties 2 | # to define the proguard.config property as described in that file. 3 | # 4 | # Add project specific ProGuard rules here. 5 | # By default, the flags in this file are appended to flags specified 6 | # in ${sdk.dir}/tools/proguard/proguard-android.txt 7 | # You can edit the include path and order by changing the ProGuard 8 | # include property in project.properties. 9 | # 10 | # For more details, see 11 | # http://developer.android.com/guide/developing/tools/proguard.html 12 | 13 | # Add any project specific keep options here: 14 | 15 | # If your project uses WebView with JS, uncomment the following 16 | # and specify the fully qualified class name to the JavaScript interface 17 | # class: 18 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 19 | # public *; 20 | #} 21 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/project.properties: -------------------------------------------------------------------------------- 1 | # This file is automatically generated by Android Tools. 2 | # Do not modify this file -- YOUR CHANGES WILL BE ERASED! 3 | # 4 | # This file must be checked in Version Control Systems. 5 | # 6 | # To customize properties used by the Ant build system edit 7 | # "ant.properties", and override values to adapt the script to your 8 | # project structure. 9 | # 10 | # To enable ProGuard to shrink and obfuscate your code, uncomment this (available properties: sdk.dir, user.home): 11 | #proguard.config=${sdk.dir}/tools/proguard/proguard-android.txt:proguard-project.txt 12 | 13 | # Project target. 14 | target=android-19 15 | android.library.reference.1=../library_zhy_viewpagerIndicator 16 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/Android-ViewPagerIndicator/fdd51835e44560129e3aa3c81087c58d3bc6aeff/sample_zhy_viewpagerIndicator/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/drawable-hdpi/recent_contact_list_bg.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/Android-ViewPagerIndicator/fdd51835e44560129e3aa3c81087c58d3bc6aeff/sample_zhy_viewpagerIndicator/res/drawable-hdpi/recent_contact_list_bg.9.png -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/drawable-hdpi/title_bar_bg_one_row.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/Android-ViewPagerIndicator/fdd51835e44560129e3aa3c81087c58d3bc6aeff/sample_zhy_viewpagerIndicator/res/drawable-hdpi/title_bar_bg_one_row.9.png -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/Android-ViewPagerIndicator/fdd51835e44560129e3aa3c81087c58d3bc6aeff/sample_zhy_viewpagerIndicator/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/Android-ViewPagerIndicator/fdd51835e44560129e3aa3c81087c58d3bc6aeff/sample_zhy_viewpagerIndicator/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/hongyangAndroid/Android-ViewPagerIndicator/fdd51835e44560129e3aa3c81087c58d3bc6aeff/sample_zhy_viewpagerIndicator/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 10 | 11 | 15 | 16 | 17 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/layout/item_msg.xml: -------------------------------------------------------------------------------- 1 | 5 | 6 | 12 | 13 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/layout/vp_indicator.xml: -------------------------------------------------------------------------------- 1 | 8 | 9 | 16 | 17 | 25 | 26 | 34 | 35 | 43 | 44 | 52 | 53 | 61 | 62 | 70 | 71 | 79 | 80 | 81 | 86 | 87 | 88 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/menu/main.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/values-sw600dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/values-sw720dp-land/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 128dp 8 | 9 | 10 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/values-v11/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/values-v14/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 8 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16dp 5 | 16dp 6 | 7 | 8 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Demo_zhy_mms_miui 5 | Settings 6 | Hello world! 7 | 8 | 9 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | 14 | 15 | 16 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/src/com/zhy/sample/viewpagerindicator/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.zhy.sample.viewpagerindicator; 2 | 3 | import java.util.ArrayList; 4 | import java.util.Arrays; 5 | import java.util.List; 6 | 7 | import android.os.Bundle; 8 | import android.support.v4.app.Fragment; 9 | import android.support.v4.app.FragmentActivity; 10 | import android.support.v4.app.FragmentPagerAdapter; 11 | import android.support.v4.view.ViewPager; 12 | import android.view.Window; 13 | 14 | import com.zhy.view.ViewPagerIndicator; 15 | 16 | public class MainActivity extends FragmentActivity 17 | { 18 | private List mTabContents = new ArrayList(); 19 | private FragmentPagerAdapter mAdapter; 20 | private ViewPager mViewPager; 21 | private List mDatas = Arrays.asList("短信1", "短信2", "短信3", "短信4", 22 | "短信5", "短信6", "短信7", "短信8", "短信9"); 23 | 24 | private ViewPagerIndicator mIndicator; 25 | 26 | @Override 27 | protected void onCreate(Bundle savedInstanceState) 28 | { 29 | super.onCreate(savedInstanceState); 30 | requestWindowFeature(Window.FEATURE_NO_TITLE); 31 | setContentView(R.layout.vp_indicator); 32 | 33 | initView(); 34 | 35 | // mIndicator.setItemCount(4); 36 | mIndicator.setTabItemTitles(mDatas); 37 | initDatas(); 38 | 39 | mViewPager.setAdapter(mAdapter); 40 | mIndicator.setViewPager(mViewPager,0); 41 | 42 | } 43 | 44 | private void initDatas() 45 | { 46 | 47 | for (String data : mDatas) 48 | { 49 | VpSimpleFragment fragment = VpSimpleFragment.newInstance(data); 50 | mTabContents.add(fragment); 51 | } 52 | 53 | mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) 54 | { 55 | @Override 56 | public int getCount() 57 | { 58 | return mTabContents.size(); 59 | } 60 | 61 | @Override 62 | public Fragment getItem(int position) 63 | { 64 | return mTabContents.get(position); 65 | } 66 | }; 67 | } 68 | 69 | private void initView() 70 | { 71 | mViewPager = (ViewPager) findViewById(R.id.id_vp); 72 | mIndicator = (ViewPagerIndicator) findViewById(R.id.id_indicator); 73 | } 74 | 75 | 76 | } 77 | -------------------------------------------------------------------------------- /sample_zhy_viewpagerIndicator/src/com/zhy/sample/viewpagerindicator/VpSimpleFragment.java: -------------------------------------------------------------------------------- 1 | package com.zhy.sample.viewpagerindicator; 2 | 3 | import android.os.Bundle; 4 | import android.support.v4.app.Fragment; 5 | import android.view.Gravity; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | import android.widget.TextView; 10 | 11 | public class VpSimpleFragment extends Fragment 12 | { 13 | public static final String BUNDLE_TITLE = "title"; 14 | private String mTitle = "DefaultValue"; 15 | 16 | @Override 17 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 18 | Bundle savedInstanceState) 19 | { 20 | Bundle arguments = getArguments(); 21 | if (arguments != null) 22 | { 23 | mTitle = arguments.getString(BUNDLE_TITLE); 24 | } 25 | 26 | TextView tv = new TextView(getActivity()); 27 | tv.setText(mTitle); 28 | tv.setGravity(Gravity.CENTER); 29 | 30 | return tv; 31 | } 32 | 33 | public static VpSimpleFragment newInstance(String title) 34 | { 35 | Bundle bundle = new Bundle(); 36 | bundle.putString(BUNDLE_TITLE, title); 37 | VpSimpleFragment fragment = new VpSimpleFragment(); 38 | fragment.setArguments(bundle); 39 | return fragment; 40 | } 41 | } 42 | --------------------------------------------------------------------------------