├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── gradle.xml ├── inspectionProfiles │ ├── Project_Default.xml │ └── profiles_settings.xml ├── misc.xml ├── modules.xml └── runConfigurations.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── ruffian │ │ └── widget │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── ruffian │ │ │ └── widget │ │ │ ├── MainActivity.java │ │ │ └── VpSimpleFragment.java │ └── res │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── ruffian │ └── widget │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── icon1.gif ├── icon2.gif ├── library ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── ruffian │ │ └── library │ │ └── RVPIndicator.java │ └── res │ ├── drawable-xxhdpi │ └── icon.9.png │ └── values │ └── attr.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 10 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RVPIndicator 2 | 3 | 4 | ## 欢迎使用 RVPIndicator 5 | 6 | ### 1.特点 7 | 8 | > 简单实用的ViewPager指示器 实现联动,自身滚动。 9 | > 10 | > 高仿MIUI但更胜于MIUI,提供多种指示器类型。支持类型 : 下滑线(可设置指示器高度),三角形,图片 11 | 12 | ### 2.效果图 13 | 14 | ![](icon2.gif) ![](icon1.gif) 15 | 16 | 17 | ### 3.属性说明 18 | 19 | > 开发者根据实际需要选择使用对应的功能属性 20 | 21 | | 属性 |说明 | 22 | | ------------- | :-------------| 23 | | text_color_normal | 文本颜色 默认 | 24 | | text_color_selected | 文本颜色 选中 | 25 | | text_size | 文本大小 | 26 | | indicator_visible_count | 屏幕可见指示器数量 | 27 | | indicator_color | 指示器颜色 | 28 | | indicator_style | 指示器样式 { bitmap:图片;line:下划线;triangle:三角形 } | 29 | | indicator_height | 指示器高度 | 30 | | indicator_width | 指示器宽度 | 31 | | style_bitmap_src | 指示器图片路径 style=bitmap有效 | 32 | | style_line_padding | 指示器下划线左右padding style=line有效 | 33 | 34 | ### 4.使用 35 | > ### 4.1 Gradle (版本号根据更新历史使用最新版) 36 | 37 | 38 | compile 'com.ruffian.library:RVPIndicator:1.0.6' 39 | 40 | 41 | > ### 4.2 XML使用 42 | 43 | 44 | 73 | 74 | > ### 4.3.1 代码调用(功能代码) 75 | 76 | // 设置Tab上的标题 77 | mIndicator.setTitleList(mList); 78 | 79 | // 设置关联的ViewPager 80 | mIndicator.setViewPager(mViewPager, 0); 81 | 82 | //设置Adapter 83 | mViewPager.setAdapter(mAdapter); 84 | 85 | 86 | > ### 4.3.2 代码调用(可选功能代码) 87 | 88 | //设置线性指示器左右Padding 89 | mIndicator.setStyleLinePadding(10); 90 | //设置title 91 | mIndicator.setTitle("哈哈哈", 1); 92 | mIndicator.setTitleList(mList); 93 | 94 | // Indicator选中监听 95 | mIndicator.setOnIndicatorSelected(new RVPIndicator.OnIndicatorSelected() { 96 | 97 | @Override 98 | public void setOnIndicatorSelected(int position, String title) { 99 | Log.w("TAG", "setOnIndicatorSelected"); 100 | } 101 | }); 102 | 103 | // PageChange监听 104 | mIndicator.setOnPageChangeListener(new RVPIndicator.PageChangeListener() { 105 | 106 | @Override 107 | public void onPageSelected(int position) { 108 | Log.w("TAG", "onPageSelected"); 109 | } 110 | 111 | @Override 112 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 113 | Log.w("TAG", "onPageScrolled"); 114 | } 115 | 116 | @Override 117 | public void onPageScrollStateChanged(int state) { 118 | Log.w("TAG", "onPageScrollStateChanged"); 119 | } 120 | }); 121 | 122 | 123 | ### 5.版本历史 124 | 125 | 126 | **v1.0.6** `2018.01.12` 扩展指示器宽高均可设置,对各个模式有效 127 | 128 | **v1.0.5** `2018.01.12` 添加指示器宽度设置 129 | 130 | **v1.0.4** `2017.11.10` 添加指示器高度设置(line模式有效,去除全背景模式,全背景效果通过设置指示器高度实现),修复BUG [#3](https://github.com/RuffianZhong/RVPIndicator/issues/3) 131 | 132 | **v1.0.3** `2017.09.08` 发布正式版本 133 | 134 | ## License 135 | 136 | ``` 137 | MIT License 138 | 139 | Copyright (c) 2018 Ruffian-痞子 140 | 141 | Permission is hereby granted, free of charge, to any person obtaining a copy 142 | of this software and associated documentation files (the "Software"), to deal 143 | in the Software without restriction, including without limitation the rights 144 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 145 | copies of the Software, and to permit persons to whom the Software is 146 | furnished to do so, subject to the following conditions: 147 | 148 | The above copyright notice and this permission notice shall be included in all 149 | copies or substantial portions of the Software. 150 | 151 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 152 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 153 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 154 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 155 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 156 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 157 | SOFTWARE. 158 | ``` 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.3" 6 | defaultConfig { 7 | applicationId "com.ruffian.widget" 8 | minSdkVersion 14 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | android { 21 | lintOptions { 22 | abortOnError false 23 | } 24 | } 25 | } 26 | 27 | dependencies { 28 | compile fileTree(dir: 'libs', include: ['*.jar']) 29 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 30 | exclude group: 'com.android.support', module: 'support-annotations' 31 | }) 32 | compile 'com.android.support:appcompat-v7:25.3.1' 33 | testCompile 'junit:junit:4.12' 34 | compile project(':library') 35 | } 36 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/ruffian/widget/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.ruffian.widget; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.ruffian.widget", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/ruffian/widget/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.ruffian.widget; 2 | 3 | import android.os.Bundle; 4 | import android.support.v4.app.Fragment; 5 | import android.support.v4.app.FragmentPagerAdapter; 6 | import android.support.v4.view.ViewPager; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.util.Log; 9 | 10 | import com.ruffian.library.RVPIndicator; 11 | 12 | import java.util.ArrayList; 13 | import java.util.Arrays; 14 | import java.util.List; 15 | 16 | 17 | public class MainActivity extends AppCompatActivity { 18 | 19 | private List mTabContents = new ArrayList(); 20 | private FragmentPagerAdapter mAdapter; 21 | 22 | private ViewPager mViewPager; 23 | private RVPIndicator mIndicator, mIndicator1, mIndicator3, mIndicator4; 24 | private List mList = Arrays.asList("军事", "政治", "娱乐", "头条", "段子", "视频", "直播"); 25 | 26 | 27 | @Override 28 | protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | setContentView(R.layout.activity_main); 31 | 32 | initView(); 33 | init(); 34 | 35 | //设置线性指示器左右Padding 36 | // mIndicator.setStyleLinePadding(10); 37 | //设置title 38 | // mIndicator.setTitle("哈哈哈",1); 39 | // mIndicator.setTitleList(mList); 40 | 41 | // Indicator选中监听 42 | mIndicator.setOnIndicatorSelected(new RVPIndicator.OnIndicatorSelected() { 43 | 44 | @Override 45 | public void setOnIndicatorSelected(int position, String title) { 46 | Log.w("TAG", "setOnIndicatorSelected"); 47 | 48 | } 49 | }); 50 | 51 | // PageChange监听 52 | mIndicator.setOnPageChangeListener(new RVPIndicator.PageChangeListener() { 53 | 54 | @Override 55 | public void onPageSelected(int position) { 56 | Log.w("TAG", "onPageSelected"); 57 | } 58 | 59 | @Override 60 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 61 | Log.w("TAG", "onPageScrolled"); 62 | } 63 | 64 | @Override 65 | public void onPageScrollStateChanged(int state) { 66 | Log.w("TAG", "onPageScrollStateChanged"); 67 | } 68 | }); 69 | } 70 | 71 | private void init() { 72 | 73 | for (String data : mList) { 74 | VpSimpleFragment fragment = VpSimpleFragment.newInstance(data); 75 | mTabContents.add(fragment); 76 | } 77 | 78 | mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) { 79 | @Override 80 | public int getCount() { 81 | return mTabContents.size(); 82 | } 83 | 84 | @Override 85 | public Fragment getItem(int position) { 86 | return mTabContents.get(position); 87 | } 88 | }; 89 | 90 | // 设置Tab上的标题 91 | mIndicator.setTitleList(mList); 92 | mIndicator1.setTitleList(mList); 93 | mIndicator3.setTitleList(mList); 94 | mIndicator4.setTitleList(mList); 95 | 96 | // 设置关联的ViewPager 97 | mIndicator.setViewPager(mViewPager, 0); 98 | 99 | //设置Adapter 100 | mViewPager.setAdapter(mAdapter); 101 | 102 | } 103 | 104 | private void initView() { 105 | mViewPager = (ViewPager) findViewById(R.id.vp_content); 106 | mIndicator = (RVPIndicator) findViewById(R.id.vp_indicator); 107 | mIndicator1 = (RVPIndicator) findViewById(R.id.vp_indicator1); 108 | mIndicator3 = (RVPIndicator) findViewById(R.id.vp_indicator3); 109 | mIndicator4 = (RVPIndicator) findViewById(R.id.vp_indicator4); 110 | } 111 | 112 | } 113 | -------------------------------------------------------------------------------- /app/src/main/java/com/ruffian/widget/VpSimpleFragment.java: -------------------------------------------------------------------------------- 1 | package com.ruffian.widget; 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 | public static final String BUNDLE_TITLE = "title"; 13 | private String mTitle = "DefaultValue"; 14 | 15 | @Override 16 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 17 | Bundle savedInstanceState) { 18 | Bundle arguments = getArguments(); 19 | if (arguments != null) { 20 | mTitle = arguments.getString(BUNDLE_TITLE); 21 | } 22 | 23 | TextView tv = new TextView(getActivity()); 24 | tv.setText(mTitle); 25 | tv.setGravity(Gravity.CENTER); 26 | 27 | return tv; 28 | } 29 | 30 | public static VpSimpleFragment newInstance(String title) { 31 | Bundle bundle = new Bundle(); 32 | bundle.putString(BUNDLE_TITLE, title); 33 | VpSimpleFragment fragment = new VpSimpleFragment(); 34 | fragment.setArguments(bundle); 35 | return fragment; 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 22 | 23 | 24 | 37 | 38 | 51 | 52 | 53 | 65 | 66 | 70 | 71 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuffianZhong/RVPIndicator/c38a9223c36aa92f2f5fd0c0b1a5a0b357a54ae3/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuffianZhong/RVPIndicator/c38a9223c36aa92f2f5fd0c0b1a5a0b357a54ae3/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuffianZhong/RVPIndicator/c38a9223c36aa92f2f5fd0c0b1a5a0b357a54ae3/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuffianZhong/RVPIndicator/c38a9223c36aa92f2f5fd0c0b1a5a0b357a54ae3/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuffianZhong/RVPIndicator/c38a9223c36aa92f2f5fd0c0b1a5a0b357a54ae3/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | RVPIndicator 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/ruffian/widget/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.ruffian.widget; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.2.2' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | classpath 'com.novoda:bintray-release:0.3.4' 13 | } 14 | } 15 | 16 | allprojects { 17 | repositories { 18 | jcenter() 19 | } 20 | tasks.withType(Javadoc) { 21 | options { 22 | encoding "UTF-8" 23 | charSet 'UTF-8' 24 | links "http://docs.oracle.com/javase/7/docs/api" 25 | } 26 | } 27 | } 28 | 29 | task clean(type: Delete) { 30 | delete rootProject.buildDir 31 | } 32 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuffianZhong/RVPIndicator/c38a9223c36aa92f2f5fd0c0b1a5a0b357a54ae3/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /icon1.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuffianZhong/RVPIndicator/c38a9223c36aa92f2f5fd0c0b1a5a0b357a54ae3/icon1.gif -------------------------------------------------------------------------------- /icon2.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuffianZhong/RVPIndicator/c38a9223c36aa92f2f5fd0c0b1a5a0b357a54ae3/icon2.gif -------------------------------------------------------------------------------- /library/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /library/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | apply plugin: 'com.novoda.bintray-release' 3 | 4 | android { 5 | compileSdkVersion 25 6 | buildToolsVersion "25.0.3" 7 | 8 | defaultConfig { 9 | minSdkVersion 14 10 | targetSdkVersion 25 11 | versionCode 1 12 | versionName "1.0" 13 | 14 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 15 | 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | android { 24 | lintOptions { 25 | abortOnError false 26 | } 27 | } 28 | } 29 | 30 | dependencies { 31 | compile fileTree(dir: 'libs', include: ['*.jar']) 32 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 33 | exclude group: 'com.android.support', module: 'support-annotations' 34 | }) 35 | compile 'com.android.support:appcompat-v7:25.3.1' 36 | testCompile 'junit:junit:4.12' 37 | } 38 | 39 | publish { 40 | userOrg = 'ruffian' 41 | groupId = 'com.ruffian.library' 42 | artifactId = 'RVPIndicator' 43 | publishVersion = '1.0.6' 44 | desc = 'A Indicator for ViewPager easy to use.' 45 | website = 'https://github.com/RuffianZhong/RVPIndicator' 46 | } -------------------------------------------------------------------------------- /library/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in D:\android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /library/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /library/src/main/java/com/ruffian/library/RVPIndicator.java: -------------------------------------------------------------------------------- 1 | package com.ruffian.library; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Bitmap; 6 | import android.graphics.Bitmap.Config; 7 | import android.graphics.BitmapFactory; 8 | import android.graphics.Canvas; 9 | import android.graphics.Color; 10 | import android.graphics.Paint; 11 | import android.graphics.Paint.Style; 12 | import android.graphics.Path; 13 | import android.graphics.Rect; 14 | import android.graphics.drawable.BitmapDrawable; 15 | import android.graphics.drawable.Drawable; 16 | import android.graphics.drawable.NinePatchDrawable; 17 | import android.support.v4.view.ViewPager; 18 | import android.support.v4.view.ViewPager.OnPageChangeListener; 19 | import android.util.AttributeSet; 20 | import android.util.TypedValue; 21 | import android.view.Gravity; 22 | import android.view.View; 23 | import android.widget.LinearLayout; 24 | import android.widget.TextView; 25 | 26 | import java.util.ArrayList; 27 | import java.util.List; 28 | 29 | /** 30 | * RVPIndicator 31 | *

32 | * ViewPager指示器 实现联动,自身滚动 33 | * 支持类型 : 下滑线,三角形,全背景,图片 34 | *

35 | * https://github.com/RuffianZhong/RVPIndicator 36 | * 37 | * @author ZhongDaFeng 38 | */ 39 | public class RVPIndicator extends LinearLayout { 40 | 41 | // 指示器风格-图标 42 | private static final int STYLE_BITMAP = 0; 43 | // 指示器风格-下划线 44 | private static final int STYLE_LINE = 1; 45 | // 指示器风格-三角形 46 | private static final int STYLE_TRIANGLE = 2; 47 | 48 | /** 49 | * 系统默认:Tab数量 50 | */ 51 | private static final int D_TAB_COUNT = 3; 52 | 53 | /** 54 | * 系统默认:文字正常时颜色 55 | */ 56 | private static final int D_TEXT_COLOR_NORMAL = Color.parseColor("#000000"); 57 | 58 | /** 59 | * 系统默认:文字选中时颜色 60 | */ 61 | private static final int D_TEXT_COLOR_HIGHLIGHT = Color 62 | .parseColor("#FF0000"); 63 | 64 | /** 65 | * 系统默认:指示器颜色 66 | */ 67 | private static final int D_INDICATOR_COLOR = Color.parseColor("#f29b76"); 68 | 69 | /** 70 | * tab上的内容 71 | */ 72 | private List mTabTitles = new ArrayList(); 73 | 74 | /** 75 | * 可见tab数量 76 | */ 77 | private int mTabVisibleCount = D_TAB_COUNT; 78 | 79 | /** 80 | * 与之绑定的ViewPager 81 | */ 82 | public ViewPager mViewPager; 83 | 84 | /** 85 | * 文字大小 86 | */ 87 | private int mTextSize; 88 | 89 | /** 90 | * 文字正常时的颜色 91 | */ 92 | private int mTextColorNormal = D_TEXT_COLOR_NORMAL; 93 | 94 | /** 95 | * 文字选中时的颜色 96 | */ 97 | private int mTextColorHighlight = D_TEXT_COLOR_HIGHLIGHT; 98 | 99 | /** 100 | * 指示器正常时的颜色 101 | */ 102 | private int mIndicatorColor = D_INDICATOR_COLOR; 103 | 104 | /** 105 | * 画笔 106 | */ 107 | private Paint mPaint; 108 | 109 | /** 110 | * 矩形 111 | */ 112 | private Rect mRectF; 113 | 114 | /** 115 | * bitmap 116 | */ 117 | private Bitmap mBitmap; 118 | 119 | /** 120 | * 指示器高 121 | */ 122 | private int mIndicatorHeight = -1; 123 | 124 | /** 125 | * 指示器宽 126 | */ 127 | private int mIndicatorWidth = -1; 128 | 129 | /** 130 | * 手指滑动时的偏移量 131 | */ 132 | private float mTranslationX; 133 | 134 | /** 135 | * 指示器风格 136 | */ 137 | private int mIndicatorStyle = STYLE_LINE; 138 | 139 | /** 140 | * 曲线path 141 | */ 142 | private Path mPath; 143 | 144 | /** 145 | * viewPage初始下标 146 | */ 147 | private int mPosition = 0; 148 | 149 | /** 150 | * 线性指示器左右padding,默认根据数量均分屏幕 151 | */ 152 | private int mStyleLinePadding = 0; 153 | 154 | public RVPIndicator(Context context) { 155 | this(context, null); 156 | } 157 | 158 | public RVPIndicator(Context context, AttributeSet attrs) { 159 | super(context, attrs); 160 | setGravity(Gravity.CENTER_VERTICAL); 161 | // 获得自定义属性 162 | TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RVPIndicator); 163 | mTabVisibleCount = a.getInt(R.styleable.RVPIndicator_indicator_visible_count, D_TAB_COUNT); 164 | mTextColorNormal = a.getColor(R.styleable.RVPIndicator_text_color_normal, D_TEXT_COLOR_NORMAL); 165 | mTextColorHighlight = a.getColor(R.styleable.RVPIndicator_text_color_selected, D_TEXT_COLOR_HIGHLIGHT); 166 | mTextSize = a.getDimensionPixelSize(R.styleable.RVPIndicator_text_size, dip2px(context, 16)); 167 | mIndicatorColor = a.getColor(R.styleable.RVPIndicator_indicator_color, D_INDICATOR_COLOR); 168 | mIndicatorStyle = a.getInt(R.styleable.RVPIndicator_indicator_style, STYLE_LINE); 169 | mStyleLinePadding = a.getDimensionPixelSize(R.styleable.RVPIndicator_style_line_padding, 0); 170 | mIndicatorHeight = a.getDimensionPixelSize(R.styleable.RVPIndicator_indicator_height, -1); 171 | mIndicatorWidth = a.getDimensionPixelSize(R.styleable.RVPIndicator_indicator_width, -1); 172 | Drawable drawable = a.getDrawable(R.styleable.RVPIndicator_style_bitmap_src); 173 | 174 | if (drawable != null) { 175 | if (drawable instanceof BitmapDrawable) { 176 | mBitmap = ((BitmapDrawable) drawable).getBitmap(); 177 | } else if (drawable instanceof NinePatchDrawable) { 178 | // .9图处理 179 | Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Config.ARGB_8888); 180 | Canvas canvas = new Canvas(bitmap); 181 | drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight()); 182 | drawable.draw(canvas); 183 | mBitmap = bitmap; 184 | } 185 | } else { 186 | mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.icon); 187 | } 188 | 189 | a.recycle(); 190 | 191 | /** 192 | * 设置画笔 193 | */ 194 | mPaint = new Paint(); 195 | mPaint.setAntiAlias(true); 196 | mPaint.setColor(mIndicatorColor); 197 | mPaint.setStyle(Style.FILL); 198 | 199 | } 200 | 201 | /** 202 | * 初始化指示器 203 | */ 204 | @Override 205 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 206 | super.onSizeChanged(w, h, oldw, oldh); 207 | switch (mIndicatorStyle) { 208 | case STYLE_LINE: 209 | /** 210 | * 下划线指示器:宽与item相等,高:0 没有指示器;未设置:item的1/10 ;具体值 211 | */ 212 | mIndicatorWidth = mIndicatorWidth < 0 ? (w / mTabVisibleCount) - (mStyleLinePadding * 2) : mIndicatorWidth; 213 | mIndicatorHeight = mIndicatorHeight < 0 ? h / 10 : mIndicatorHeight; 214 | mTranslationX = 0; 215 | mRectF = new Rect(mStyleLinePadding, 0, mIndicatorWidth + mStyleLinePadding, mIndicatorHeight); 216 | break; 217 | case STYLE_BITMAP: 218 | /** 219 | * 方形/图标指示器:宽,高与item相等 220 | */ 221 | mIndicatorWidth = mIndicatorWidth < 0 ? w / mTabVisibleCount : mIndicatorWidth; 222 | mIndicatorHeight = mIndicatorHeight < 0 ? h : mIndicatorHeight; 223 | mTranslationX = 0; 224 | mRectF = new Rect(0, 0, mIndicatorWidth, mIndicatorHeight); 225 | break; 226 | case STYLE_TRIANGLE: 227 | /** 228 | * 三角形指示器:宽与item(1/5)相等,高item的1/5 229 | */ 230 | mIndicatorWidth = mIndicatorWidth < 0 ? w / mTabVisibleCount / 5 : mIndicatorWidth; 231 | mIndicatorHeight = mIndicatorHeight < 0 ? h / 5 : mIndicatorHeight; 232 | mTranslationX = 0; 233 | break; 234 | } 235 | // 初始化tabItem 236 | setTabItem(); 237 | 238 | // 高亮 239 | setSelectedTextView(mPosition); 240 | } 241 | 242 | /** 243 | * 绘制指示器(子view) 244 | */ 245 | @Override 246 | protected void dispatchDraw(Canvas canvas) { 247 | // 保存画布 248 | canvas.save(); 249 | switch (mIndicatorStyle) { 250 | case STYLE_BITMAP: 251 | canvas.translate(mTranslationX, 0); 252 | canvas.drawBitmap(mBitmap, null, mRectF, mPaint); 253 | break; 254 | case STYLE_LINE: 255 | canvas.translate(mTranslationX, getHeight() - mIndicatorHeight); 256 | canvas.drawRect(mRectF, mPaint); 257 | break; 258 | case STYLE_TRIANGLE: 259 | canvas.translate(mTranslationX, 0); 260 | // 笔锋圆滑度 261 | // mPaint.setPathEffect(new CornerPathEffect(10)); 262 | mPath = new Path(); 263 | int midOfTab = getWidth() / mTabVisibleCount / 2; 264 | mPath.moveTo(midOfTab, getHeight() - mIndicatorHeight); 265 | mPath.lineTo(midOfTab - mIndicatorWidth / 2, getHeight()); 266 | mPath.lineTo(midOfTab + mIndicatorWidth / 2, getHeight()); 267 | mPath.close(); 268 | canvas.drawPath(mPath, mPaint); 269 | break; 270 | } 271 | // 恢复画布 272 | canvas.restore(); 273 | super.dispatchDraw(canvas); 274 | } 275 | 276 | /** 277 | * 设置titles 278 | * 279 | * @param titleList 280 | */ 281 | public void setTitleList(List titleList) { 282 | mTabTitles.clear(); 283 | mTabTitles.addAll(titleList); 284 | setTabItem(); 285 | } 286 | 287 | /** 288 | * 设置title 289 | * 290 | * @param title 291 | * @param pos 292 | */ 293 | public void setTitle(String title, int pos) { 294 | int size = mTabTitles.size(); 295 | if (pos >= size) { 296 | return; 297 | } 298 | for (int i = 0; i < size; i++) { 299 | if (pos == i) { 300 | mTabTitles.set(i, title); 301 | } 302 | } 303 | setTabItem(); 304 | } 305 | 306 | /** 307 | * 设置关联viewPager 308 | * 309 | * @param viewPager 310 | * @param pos 311 | */ 312 | @SuppressWarnings("deprecation") 313 | public void setViewPager(ViewPager viewPager, int pos) { 314 | this.mViewPager = viewPager; 315 | mViewPager.setOnPageChangeListener(new OnPageChangeListener() { 316 | @Override 317 | public void onPageSelected(int position) { 318 | // 设置文本高亮 319 | setSelectedTextView(position); 320 | 321 | // 回调 322 | if (onPageChangeListener != null) { 323 | onPageChangeListener.onPageSelected(position); 324 | } 325 | 326 | //回调 327 | if (mOnIndicatorSelected != null) { 328 | mOnIndicatorSelected.setOnIndicatorSelected(position, 329 | mTabTitles == null ? "" : mTabTitles.get(position)); 330 | } 331 | 332 | } 333 | 334 | @Override 335 | public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { 336 | // scoll 337 | onScoll(position, positionOffset); 338 | // 回调 339 | if (onPageChangeListener != null) { 340 | onPageChangeListener.onPageScrolled(position, 341 | positionOffset, positionOffsetPixels); 342 | } 343 | } 344 | 345 | @Override 346 | public void onPageScrollStateChanged(int state) { 347 | // 回调 348 | if (onPageChangeListener != null) { 349 | onPageChangeListener.onPageScrollStateChanged(state); 350 | } 351 | } 352 | }); 353 | 354 | // 设置当前页 355 | mViewPager.setCurrentItem(pos); 356 | mPosition = pos; 357 | 358 | //回调 359 | if (mOnIndicatorSelected != null) { 360 | mOnIndicatorSelected.setOnIndicatorSelected(pos, 361 | mTabTitles == null ? "" : mTabTitles.get(pos)); 362 | } 363 | 364 | } 365 | 366 | /** 367 | * 线性指示器左右padding 368 | * 369 | * @param styleLinePadding 370 | */ 371 | public void setStyleLinePadding(int styleLinePadding) { 372 | this.mStyleLinePadding = dip2px(getContext(), styleLinePadding); 373 | /** 374 | * 重绘指示器 375 | */ 376 | mIndicatorWidth = mIndicatorWidth < 0 ? (getWidth() / mTabVisibleCount) - (mStyleLinePadding * 2) : mIndicatorWidth; 377 | mIndicatorHeight = mIndicatorHeight < 0 ? getHeight() / 10 : mIndicatorHeight; 378 | mRectF = new Rect(mStyleLinePadding, 0, mIndicatorWidth + mStyleLinePadding, mIndicatorHeight); 379 | invalidate(); 380 | } 381 | 382 | /** 383 | * 设置tabItem 384 | */ 385 | private void setTabItem() { 386 | if (mTabTitles != null && mTabTitles.size() > 0) { 387 | if (this.getChildCount() != 0) { 388 | this.removeAllViews(); 389 | } 390 | for (String title : mTabTitles) { 391 | addView(createTextView(title)); 392 | measure(0, 0); 393 | } 394 | 395 | // 设置点击事件 396 | setItemClickEvent(); 397 | } 398 | 399 | } 400 | 401 | /** 402 | * 设置点击事件 403 | */ 404 | private void setItemClickEvent() { 405 | int cCount = getChildCount(); 406 | for (int i = 0; i < cCount; i++) { 407 | final int j = i; 408 | View view = getChildAt(i); 409 | view.setOnClickListener(new OnClickListener() { 410 | @Override 411 | public void onClick(View v) { 412 | mViewPager.setCurrentItem(j); 413 | } 414 | }); 415 | } 416 | } 417 | 418 | /** 419 | * 设置文本高亮 420 | * 421 | * @param position 422 | */ 423 | private void setSelectedTextView(int position) { 424 | for (int i = 0; i < getChildCount(); i++) { 425 | View view = getChildAt(i); 426 | if (view instanceof TextView) { 427 | if (i == position) { 428 | ((TextView) view).setTextColor(mTextColorHighlight); 429 | } else { 430 | ((TextView) view).setTextColor(mTextColorNormal); 431 | } 432 | } 433 | } 434 | } 435 | 436 | /** 437 | * 滚动 438 | * 439 | * @param position 440 | * @param offset 441 | */ 442 | private void onScoll(int position, float offset) { 443 | 444 | // 不断改变偏移量,invalidate 445 | mTranslationX = getWidth() / mTabVisibleCount * (position + offset); 446 | 447 | int tabWidth = getWidth() / mTabVisibleCount; 448 | 449 | // 容器滚动,当移动到倒数第二个的时候,开始滚动 450 | if (offset > 0 && position >= (mTabVisibleCount - 2) 451 | && getChildCount() > mTabVisibleCount 452 | && position < (getChildCount() - 2)) { 453 | if (mTabVisibleCount != 1) { 454 | 455 | int xValue = (position - (mTabVisibleCount - 2)) * tabWidth 456 | + (int) (tabWidth * offset); 457 | this.scrollTo(xValue, 0); 458 | 459 | } else { 460 | // 为count为1时 的特殊处理 461 | this.scrollTo(position * tabWidth + (int) (tabWidth * offset), 462 | 0); 463 | } 464 | } 465 | 466 | invalidate(); 467 | } 468 | 469 | /** 470 | * 创建标题view 471 | * 472 | * @param text 473 | * @return 474 | */ 475 | private TextView createTextView(String text) { 476 | TextView tv = new TextView(getContext()); 477 | LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); 478 | lp.width = getWidth() / mTabVisibleCount; 479 | tv.setGravity(Gravity.CENTER); 480 | tv.setTextColor(mTextColorNormal); 481 | tv.setText(text); 482 | tv.setTextSize(TypedValue.COMPLEX_UNIT_PX, mTextSize); 483 | tv.setLayoutParams(lp); 484 | return tv; 485 | } 486 | 487 | 488 | public interface PageChangeListener { 489 | 490 | void onPageSelected(int position); 491 | 492 | void onPageScrollStateChanged(int state); 493 | 494 | void onPageScrolled(int position, float positionOffset, int positionOffsetPixels); 495 | } 496 | 497 | public interface OnIndicatorSelected { 498 | void setOnIndicatorSelected(int position, String title); 499 | } 500 | 501 | private PageChangeListener onPageChangeListener; 502 | 503 | private OnIndicatorSelected mOnIndicatorSelected; 504 | 505 | public void setOnPageChangeListener(PageChangeListener pageChangeListener) { 506 | this.onPageChangeListener = pageChangeListener; 507 | } 508 | 509 | public void setOnIndicatorSelected(OnIndicatorSelected mOnIndicatorSelected) { 510 | this.mOnIndicatorSelected = mOnIndicatorSelected; 511 | } 512 | 513 | private int dip2px(Context context, float dpValue) { 514 | final float scale = context.getResources().getDisplayMetrics().density; 515 | return (int) (dpValue * scale + 0.5f); 516 | } 517 | } 518 | -------------------------------------------------------------------------------- /library/src/main/res/drawable-xxhdpi/icon.9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/RuffianZhong/RVPIndicator/c38a9223c36aa92f2f5fd0c0b1a5a0b357a54ae3/library/src/main/res/drawable-xxhdpi/icon.9.png -------------------------------------------------------------------------------- /library/src/main/res/values/attr.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':library' 2 | --------------------------------------------------------------------------------