├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── mipmap-xxxhdpi │ │ │ │ ├── icon.png │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-hdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-mdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_launcher_round.png │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── layout │ │ │ │ ├── fragment_test.xml │ │ │ │ └── activity_main.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── drawable │ │ │ │ └── ic_launcher_background.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── yangfan │ │ │ │ ├── modifytablayout │ │ │ │ ├── TestFragment.java │ │ │ │ └── MainActivity.java │ │ │ │ └── widget │ │ │ │ ├── CustomFragmentPagerAdapter.java │ │ │ │ └── ModifyTabLayout.java │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── yangfan │ │ │ └── modifytablayout │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── yangfan │ │ └── modifytablayout │ │ └── ExampleInstrumentedTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── readme1.png ├── readme2.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .idea ├── caches │ └── build_file_checksums.ser ├── encodings.xml ├── runConfigurations.xml ├── gradle.xml ├── misc.xml └── codeStyles │ └── Project.xml ├── .gitignore ├── gradle.properties ├── README.md ├── gradlew.bat └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /readme1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfanCode/ModifyTabLayout/HEAD/readme1.png -------------------------------------------------------------------------------- /readme2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfanCode/ModifyTabLayout/HEAD/readme2.png -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfanCode/ModifyTabLayout/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfanCode/ModifyTabLayout/HEAD/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfanCode/ModifyTabLayout/HEAD/app/src/main/res/mipmap-xxxhdpi/icon.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfanCode/ModifyTabLayout/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfanCode/ModifyTabLayout/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfanCode/ModifyTabLayout/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfanCode/ModifyTabLayout/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfanCode/ModifyTabLayout/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfanCode/ModifyTabLayout/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfanCode/ModifyTabLayout/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfanCode/ModifyTabLayout/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfanCode/ModifyTabLayout/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yangfanCode/ModifyTabLayout/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/libraries 5 | /.idea/modules.xml 6 | /.idea/workspace.xml 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | ModifyTabLayout 3 | 4 | 5 | Hello blank fragment 6 | 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Jun 25 14:44:03 CST 2018 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-4.4-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #14805E 7 | #666666 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/yangfan/modifytablayout/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.yangfan.modifytablayout; 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() { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_test.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 9 | 13 | 14 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 18 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx1536m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | -------------------------------------------------------------------------------- /app/src/main/java/com/yangfan/modifytablayout/TestFragment.java: -------------------------------------------------------------------------------- 1 | package com.yangfan.modifytablayout; 2 | 3 | 4 | import android.os.Bundle; 5 | import android.support.v4.app.Fragment; 6 | import android.view.LayoutInflater; 7 | import android.view.View; 8 | import android.view.ViewGroup; 9 | 10 | 11 | /** 12 | * A simple {@link Fragment} subclass. 13 | */ 14 | public class TestFragment extends Fragment { 15 | 16 | 17 | public TestFragment() { 18 | // Required empty public constructor 19 | } 20 | 21 | 22 | @Override 23 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 24 | Bundle savedInstanceState) { 25 | // Inflate the layout for this fragment 26 | return inflater.inflate(R.layout.fragment_test, container, false); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 15 | 16 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/yangfan/modifytablayout/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.yangfan.modifytablayout; 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 | * Instrumented 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() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.yangfan.modifytablayout", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | //apply plugin: 'com.android.application' 2 | apply plugin: 'com.android.library' 3 | 4 | android { 5 | compileSdkVersion 27 6 | defaultConfig { 7 | // applicationId "com.yangfan.modifytablayout" 8 | minSdkVersion 15 9 | targetSdkVersion 27 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 | } 21 | 22 | dependencies { 23 | implementation fileTree(dir: 'libs', include: ['*.jar']) 24 | implementation 'com.android.support:appcompat-v7:27.1.1' 25 | implementation 'com.android.support:support-v4:27.1.1' 26 | testImplementation 'junit:junit:4.12' 27 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 28 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 29 | } 30 | -------------------------------------------------------------------------------- /app/src/main/java/com/yangfan/widget/CustomFragmentPagerAdapter.java: -------------------------------------------------------------------------------- 1 | package com.yangfan.widget; 2 | 3 | import android.support.v4.app.Fragment; 4 | import android.support.v4.app.FragmentManager; 5 | import android.support.v4.app.FragmentPagerAdapter; 6 | 7 | import java.util.ArrayList; 8 | import java.util.List; 9 | 10 | /** 11 | * Created by yangfan 12 | * nrainyseason@163.com 13 | */ 14 | 15 | public class CustomFragmentPagerAdapter extends FragmentPagerAdapter { 16 | 17 | private final List mFragmentList = new ArrayList<>(); 18 | private final List mFragmentTitleList = new ArrayList<>(); 19 | 20 | public CustomFragmentPagerAdapter(FragmentManager manager) { 21 | super(manager); 22 | } 23 | 24 | @Override 25 | public Fragment getItem(int position) { 26 | return (mFragmentList != null && mFragmentList.size() > position) ? mFragmentList.get(position) : null; 27 | } 28 | 29 | @Override 30 | public int getCount() { 31 | return mFragmentList != null ? mFragmentList.size() : 0; 32 | } 33 | 34 | public void addFrag(Fragment fragment, CharSequence title) { 35 | mFragmentList.add(fragment); 36 | mFragmentTitleList.add(title); 37 | } 38 | 39 | public void cleanFrag() { 40 | mFragmentList.clear(); 41 | mFragmentTitleList.clear(); 42 | } 43 | 44 | @Override 45 | public CharSequence getPageTitle(int position) { 46 | return mFragmentTitleList.get(position); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ModifyTabLayout 2 | android自定义下划线的TabLayout 3 | 4 | ![image](https://github.com/yangfanCode/ModifyTabLayout/blob/master/readme1.png) 5 | 6 | ![image](https://github.com/yangfanCode/ModifyTabLayout/blob/master/readme2.png) 7 | 8 | 使用 9 | 10 | ModifyTabLayout tabLayout=findViewById(R.id.modiftTabLayout); 11 | ViewPager vp=findViewById(R.id.vp); 12 | tabLayout.setViewHeight(dp2px(35)); 13 | tabLayout.setBottomLineWidth(dp2px(10)); 14 | tabLayout.setBottomLineHeight(dp2px(3)); 15 | tabLayout.setBottomLineHeightBgResId(R.color.color_14805E); 16 | tabLayout.setItemInnerPaddingLeft(dp2px(6)); 17 | tabLayout.setItemInnerPaddingRight(dp2px(6)); 18 | tabLayout.setmTextColorSelect(ContextCompat.getColor(this,R.color.color_14805E)); 19 | tabLayout.setmTextColorUnSelect(ContextCompat.getColor(this,R.color.color_666666)); 20 | tabLayout.setTextSize(16); 21 | int width=getResources().getDisplayMetrics().widthPixels; 22 | tabLayout.setNeedEqual(true,width); 23 | CustomFragmentPagerAdapter adapter = new CustomFragmentPagerAdapter(getSupportFragmentManager()); 24 | adapter.addFrag(new TestFragment(), "巴西"); 25 | adapter.addFrag(new TestFragment(), "西班牙"); 26 | adapter.addFrag(new TestFragment(), "阿根廷"); 27 | adapter.addFrag(new TestFragment(), "葡萄牙"); 28 | adapter.addFrag(new TestFragment(), "俄罗斯"); 29 | adapter.addFrag(new TestFragment(), "巴西"); 30 | adapter.addFrag(new TestFragment(), "西班牙"); 31 | adapter.addFrag(new TestFragment(), "阿根廷"); 32 | adapter.addFrag(new TestFragment(), "葡萄牙"); 33 | adapter.addFrag(new TestFragment(), "俄罗斯"); 34 | vp.setAdapter(adapter); 35 | vp.setOffscreenPageLimit(adapter.getCount()); 36 | tabLayout.setupWithViewPager(vp); 37 | 38 | 如果不配合ViewPager使用可直接使用setTabData()方法 39 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/yangfan/modifytablayout/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.yangfan.modifytablayout; 2 | 3 | import android.os.Bundle; 4 | import android.support.v4.content.ContextCompat; 5 | import android.support.v4.view.ViewPager; 6 | import android.support.v7.app.AppCompatActivity; 7 | 8 | import com.yangfan.widget.CustomFragmentPagerAdapter; 9 | import com.yangfan.widget.ModifyTabLayout; 10 | 11 | public class MainActivity extends AppCompatActivity { 12 | 13 | @Override 14 | protected void onCreate(Bundle savedInstanceState) { 15 | super.onCreate(savedInstanceState); 16 | setContentView(R.layout.activity_main); 17 | ModifyTabLayout tabLayout=findViewById(R.id.modiftTabLayout); 18 | ViewPager vp=findViewById(R.id.vp); 19 | tabLayout.setViewHeight(dp2px(35)); 20 | tabLayout.setBottomLineWidth(dp2px(10)); 21 | tabLayout.setBottomLineHeight(dp2px(3)); 22 | tabLayout.setBottomLineHeightBgResId(R.color.color_14805E); 23 | tabLayout.setItemInnerPaddingLeft(dp2px(6)); 24 | tabLayout.setItemInnerPaddingRight(dp2px(6)); 25 | tabLayout.setmTextColorSelect(ContextCompat.getColor(this,R.color.color_14805E)); 26 | tabLayout.setmTextColorUnSelect(ContextCompat.getColor(this,R.color.color_666666)); 27 | tabLayout.setTextSize(16); 28 | // int width=getResources().getDisplayMetrics().widthPixels; 29 | // tabLayout.setNeedEqual(true,width); 30 | CustomFragmentPagerAdapter adapter = new CustomFragmentPagerAdapter(getSupportFragmentManager()); 31 | adapter.addFrag(new TestFragment(), "巴西"); 32 | adapter.addFrag(new TestFragment(), "西班牙"); 33 | adapter.addFrag(new TestFragment(), "阿根廷"); 34 | adapter.addFrag(new TestFragment(), "葡萄牙"); 35 | adapter.addFrag(new TestFragment(), "俄罗斯"); 36 | adapter.addFrag(new TestFragment(), "巴西"); 37 | adapter.addFrag(new TestFragment(), "西班牙"); 38 | adapter.addFrag(new TestFragment(), "阿根廷"); 39 | adapter.addFrag(new TestFragment(), "葡萄牙"); 40 | adapter.addFrag(new TestFragment(), "俄罗斯"); 41 | vp.setAdapter(adapter); 42 | vp.setOffscreenPageLimit(adapter.getCount()); 43 | tabLayout.setupWithViewPager(vp); 44 | } 45 | 46 | /** 47 | * dp转换成px 48 | */ 49 | public int dp2px( float dpValue){ 50 | float scale=getResources().getDisplayMetrics().density; 51 | return (int)(dpValue*scale+0.5f); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /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 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 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 Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/java/com/yangfan/widget/ModifyTabLayout.java: -------------------------------------------------------------------------------- 1 | package com.yangfan.widget; 2 | 3 | import android.content.Context; 4 | import android.graphics.Color; 5 | import android.os.Handler; 6 | import android.os.Message; 7 | import android.support.annotation.Nullable; 8 | import android.support.v4.view.PagerAdapter; 9 | import android.support.v4.view.ViewPager; 10 | import android.util.AttributeSet; 11 | import android.view.Gravity; 12 | import android.view.View; 13 | import android.widget.FrameLayout; 14 | import android.widget.HorizontalScrollView; 15 | import android.widget.LinearLayout; 16 | import android.widget.TextView; 17 | 18 | import java.lang.ref.WeakReference; 19 | import java.util.ArrayList; 20 | import java.util.List; 21 | 22 | import static android.support.v4.view.ViewPager.SCROLL_STATE_IDLE; 23 | 24 | /** 25 | * Created by yangfan 26 | * nrainyseason@163.com 27 | */ 28 | 29 | public class ModifyTabLayout extends HorizontalScrollView { 30 | private OnTabLayoutItemSelectListener onTabLayoutItemSelectListener; 31 | private List mViewsList; 32 | private int mTextColorUnSelect; 33 | private int mTextColorSelect; 34 | private int mTextBgUnSelectResId; 35 | private int mTextBgSelectResId; 36 | private List mTabList; 37 | private LinearLayout layContent; 38 | private View bottomLine; 39 | private int bottomLineHeight, bottomLineWidth;// 底部 线 宽高 40 | private int bottomLineHeightBgResId;// 底部 线 背景 41 | 42 | private int mScrollViewWidth = 0, mScrollViewMiddle = 0, selectedTabPosition = -1, tabCount; 43 | private Handler mHandler = null; 44 | private int viewHeight, innerLeftMargin, innerRightMargin, itemInnerPaddingLeft, itemInnerPaddingRight;// item高度,item距左,item距右,item 内部左padding,item 内部右padding 45 | private float textSize = 14; 46 | private ViewPager mViewPager; 47 | private TabLayoutOnPageChangeListener mPageChangeListener; 48 | private boolean needEqual; // 是否等分 49 | //总宽 50 | private int mWidth; 51 | 52 | 53 | private static class StaticHandler extends Handler { 54 | private final WeakReference mWeakContext; 55 | private final WeakReference mParent; 56 | 57 | public StaticHandler(Context context, ModifyTabLayout view) { 58 | mWeakContext = new WeakReference<>(context); 59 | mParent = new WeakReference<>(view); 60 | } 61 | 62 | @Override 63 | public void handleMessage(Message msg) { 64 | Context context = mWeakContext.get(); 65 | ModifyTabLayout parent = mParent.get(); 66 | if (null != context && null != parent) { 67 | switch (msg.what) { 68 | case 0: 69 | parent.changeTextLocation(parent.selectedTabPosition); 70 | break; 71 | 72 | default: 73 | break; 74 | } 75 | 76 | super.handleMessage(msg); 77 | } 78 | } 79 | } 80 | 81 | 82 | public ModifyTabLayout(Context context) { 83 | this(context, null); 84 | } 85 | 86 | public ModifyTabLayout(Context context, AttributeSet attrs) { 87 | super(context, attrs); 88 | viewHeight = dip2px(getContext(), 40); 89 | innerLeftMargin = dip2px(getContext(), 15); 90 | innerRightMargin = dip2px(getContext(), 15); 91 | mHandler = new StaticHandler(context, this); 92 | mViewsList = new ArrayList<>(); 93 | mTextColorUnSelect = Color.BLACK; 94 | mTextColorSelect = Color.RED; 95 | 96 | setHorizontalScrollBarEnabled(false); 97 | setHorizontalFadingEdgeEnabled(false); 98 | 99 | FrameLayout layParent = new FrameLayout(context); 100 | addView(layParent); 101 | layContent = new LinearLayout(context); 102 | layParent.addView(layContent); 103 | bottomLine = new View(context); 104 | layParent.addView(bottomLine); 105 | 106 | } 107 | 108 | public int getViewHeight() { 109 | return viewHeight; 110 | } 111 | 112 | public void setInnerLeftMargin(int innerLeftMargin) { 113 | this.innerLeftMargin = innerLeftMargin; 114 | } 115 | 116 | public void setInnerRightMargin(int innerRightMargin) { 117 | this.innerRightMargin = innerRightMargin; 118 | } 119 | 120 | public void setmTextBgUnSelectResId(int mTextBgUnSelectResId) { 121 | this.mTextBgUnSelectResId = mTextBgUnSelectResId; 122 | } 123 | 124 | public void setmTextBgSelectResId(int mTextBgSelectResId) { 125 | this.mTextBgSelectResId = mTextBgSelectResId; 126 | } 127 | 128 | public void setmTextColorSelect(int mTextColorSelect) { 129 | this.mTextColorSelect = mTextColorSelect; 130 | } 131 | 132 | public void setmTextColorSelectId(int colorId) { 133 | this.mTextColorSelect = getResources().getColor(colorId); 134 | } 135 | 136 | public void setmTextColorUnSelect(int mTextColorUnSelect) { 137 | this.mTextColorUnSelect = mTextColorUnSelect; 138 | } 139 | 140 | public void setmTextColorUnSelectId(int colorId) { 141 | this.mTextColorUnSelect = getResources().getColor(colorId); 142 | } 143 | 144 | public void setViewHeight(int viewHeightPx) { 145 | this.viewHeight = viewHeightPx; 146 | } 147 | 148 | public void setTextSize(float textSize) { 149 | this.textSize = textSize; 150 | } 151 | 152 | public void setNeedEqual(boolean needEqual, int mWidth) { 153 | this.needEqual = needEqual; 154 | this.mWidth = mWidth; 155 | } 156 | 157 | public void setItemInnerPaddingLeft(int itemInnerPaddingLeft) { 158 | this.itemInnerPaddingLeft = itemInnerPaddingLeft; 159 | } 160 | 161 | public void setItemInnerPaddingRight(int itemInnerPaddingRight) { 162 | this.itemInnerPaddingRight = itemInnerPaddingRight; 163 | } 164 | 165 | public void setBottomLineHeight(int bottomLineHeight) { 166 | this.bottomLineHeight = bottomLineHeight; 167 | } 168 | 169 | public void setBottomLineWidth(int bottomLineWidth) { 170 | this.bottomLineWidth = bottomLineWidth; 171 | } 172 | 173 | public void setBottomLineHeightBgResId(int bottomLineHeightBgResId) { 174 | this.bottomLineHeightBgResId = bottomLineHeightBgResId; 175 | } 176 | 177 | public View getBottomLine() { 178 | return bottomLine; 179 | } 180 | 181 | private void setData(List mTabList) { 182 | if (mTabList == null || mTabList.size() == 0) return; 183 | this.mTabList = mTabList; 184 | initView(); 185 | } 186 | //不使用viewpager 187 | public void setTabData(List mTabList, int defaultPos){ 188 | if (mTabList == null || mTabList.size() == 0) return; 189 | this.mTabList = mTabList; 190 | if(defaultPos >= 0 && defaultPos < mTabList.size()){ 191 | selectedTabPosition = defaultPos; 192 | }else{ 193 | selectedTabPosition =0 ; 194 | } 195 | initView(); 196 | clickTabWithItem(selectedTabPosition); 197 | } 198 | 199 | private void initView() { 200 | if (mTabList == null || mTabList.size() == 0) return; 201 | mViewsList = new ArrayList<>(); 202 | layContent.removeAllViews(); 203 | 204 | for (int i = 0; i < mTabList.size(); i++) { 205 | TextView textView = new TextView(getContext()); 206 | if (needEqual) { 207 | LinearLayout linearLayout = new LinearLayout(getContext()); 208 | linearLayout.setGravity(Gravity.CENTER); 209 | LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 210 | if (mWidth > 0) 211 | layoutParams.width = mWidth / mTabList.size(); 212 | linearLayout.addView(textView); 213 | layContent.addView(linearLayout, layoutParams); 214 | } else { 215 | layContent.addView(textView); 216 | } 217 | 218 | textView.setTextSize(textSize); 219 | textView.setGravity(Gravity.CENTER_VERTICAL); 220 | if (i == selectedTabPosition) { 221 | textView.setBackgroundResource(mTextBgSelectResId); 222 | textView.setTextColor(mTextColorSelect); 223 | } else { 224 | textView.setBackgroundResource(mTextBgUnSelectResId); 225 | textView.setTextColor(mTextColorUnSelect); 226 | } 227 | textView.setTag(i); 228 | textView.setText(mTabList.get(i)); 229 | textView.setOnClickListener(new OnClickListener() { 230 | @Override 231 | public void onClick(View view) { 232 | int position = Integer.parseInt(view.getTag().toString()); 233 | if (mViewPager != null) 234 | mViewPager.setCurrentItem(position); 235 | else 236 | clickTabWithItem(position); 237 | } 238 | }); 239 | 240 | LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 241 | if (!needEqual) { 242 | layoutParams.rightMargin = innerRightMargin; 243 | layoutParams.leftMargin = innerLeftMargin; 244 | } 245 | layoutParams.height = viewHeight; 246 | textView.setLayoutParams(layoutParams); 247 | 248 | textView.setPadding(itemInnerPaddingLeft, 0, itemInnerPaddingRight, 0); 249 | // 4.4 系统 设置 Background 后,padding失效, 故 设置 padding 在 设置 Background 后。 250 | mViewsList.add(textView); 251 | } 252 | initBottomLine(); 253 | mHandler.sendEmptyMessageDelayed(0, 200); 254 | } 255 | 256 | private void initBottomLine() { 257 | bottomLine.setBackgroundResource(bottomLineHeightBgResId); 258 | LayoutParams fl = (LayoutParams) bottomLine.getLayoutParams(); 259 | fl.width = bottomLineWidth; 260 | fl.height = bottomLineHeight; 261 | fl.gravity = Gravity.BOTTOM; 262 | bottomLine.setLayoutParams(fl); 263 | } 264 | 265 | private void clickTabWithItem(int position) { 266 | 267 | if (mViewsList == null) return; 268 | selectedTabPosition = position; 269 | if (null != onTabLayoutItemSelectListener) 270 | onTabLayoutItemSelectListener.onTabLayoutItemSelect(position); 271 | 272 | for (int i = 0; i < mViewsList.size(); i++) { 273 | TextView textView = mViewsList.get(i); 274 | if (Integer.parseInt(mViewsList.get(i).getTag().toString()) == position) { 275 | changeTextLocation(i); 276 | textView.setBackgroundResource(mTextBgSelectResId); 277 | textView.setTextColor(mTextColorSelect); 278 | } else { 279 | mViewsList.get(i).setBackgroundResource(mTextBgUnSelectResId); 280 | mViewsList.get(i).setTextColor(mTextColorUnSelect); 281 | } 282 | textView.setPadding(itemInnerPaddingLeft, 0, itemInnerPaddingRight, 0);// 4.4 系统 设置 Background 后,padding失效,重新设置 padding。 283 | } 284 | 285 | } 286 | 287 | public void setCurrentItem(int position) { 288 | if (mViewPager != null) { 289 | mViewPager.setCurrentItem(position); 290 | } else 291 | clickTabWithItem(position); 292 | } 293 | 294 | public TextView getTextView(int position) { 295 | if (mViewsList == null || position >= mViewsList.size()) 296 | throw new RuntimeException("mViewsList == null || position >= mViewsList.size()"); 297 | return mViewsList.get(position); 298 | } 299 | 300 | public LinearLayout getLayContent() { 301 | return layContent; 302 | } 303 | 304 | /** 305 | * 改变栏目位置 306 | * 307 | * @param clickPosition 308 | */ 309 | private void changeTextLocation(int clickPosition) { 310 | if (clickPosition >= 0 && clickPosition < mViewsList.size()) { 311 | changebottomLineLocation(); 312 | int x = (mViewsList.get(clickPosition).getLeft() - getScrollViewMiddle() + 313 | (getViewheight(mViewsList.get(clickPosition)) / 2)); 314 | smoothScrollTo(x, 0); 315 | } 316 | } 317 | 318 | /** 319 | * 改变底部 线位置 320 | */ 321 | private void changebottomLineLocation() { 322 | if (selectedTabPosition >= 0 && selectedTabPosition < mViewsList.size()) { 323 | TextView textView = getTextView(selectedTabPosition); 324 | int x; 325 | if (needEqual) { 326 | int[] position = new int[2]; 327 | textView.getLocationOnScreen(position); 328 | int l1 = position[0]; 329 | if (l1 == 0 ) { 330 | int sWidth=mWidth/mViewsList.size();//tab宽度 331 | int bWidth=sWidth/2;//半个tab宽度 332 | textView.measure(0,0); 333 | l1=sWidth*selectedTabPosition+bWidth-textView.getMeasuredWidth()/2; 334 | } 335 | x = l1 + (textView.getRight() - textView.getLeft() - bottomLineWidth) / 2; 336 | } else 337 | x = textView.getLeft() + (textView.getRight() - textView.getLeft() - bottomLineWidth) / 2; 338 | LayoutParams fl = (LayoutParams) bottomLine.getLayoutParams(); 339 | fl.leftMargin = x; 340 | bottomLine.setLayoutParams(fl); 341 | } 342 | } 343 | 344 | /** 345 | * 返回scrollview的中间位置 346 | * 347 | * @return 348 | */ 349 | private int getScrollViewMiddle() { 350 | if (mScrollViewMiddle == 0) 351 | mScrollViewMiddle = getScrollViewWidth() / 2; 352 | return mScrollViewMiddle; 353 | } 354 | 355 | /** 356 | * 返回ScrollView的宽度 357 | * 358 | * @return 359 | */ 360 | private int getScrollViewWidth() { 361 | if (mScrollViewWidth == 0) 362 | mScrollViewWidth = getRight() - getLeft(); 363 | return mScrollViewWidth; 364 | } 365 | 366 | /** 367 | * 返回view的宽度 368 | * 369 | * @param view 370 | * @return 371 | */ 372 | private int getViewheight(View view) { 373 | return view.getBottom() - view.getTop(); 374 | } 375 | 376 | /* 根据手机的分辨率从 dp 的单位 转成为 px(像素) */ 377 | private static int dip2px(Context context, float dpValue) { 378 | final float scale = context.getResources().getDisplayMetrics().density; 379 | return (int) (dpValue * scale + 0.5f); 380 | } 381 | 382 | public void setupWithViewPager(@Nullable ViewPager viewPager) { 383 | if (viewPager != null) { 384 | mViewPager = viewPager; 385 | // Add our custom OnPageChangeListener to the ViewPager 386 | if (mPageChangeListener == null) { 387 | mPageChangeListener = new TabLayoutOnPageChangeListener(this); 388 | } 389 | mPageChangeListener.reset(); 390 | viewPager.addOnPageChangeListener(mPageChangeListener); 391 | selectedTabPosition = viewPager.getCurrentItem(); 392 | 393 | final PagerAdapter adapter = viewPager.getAdapter(); 394 | if (adapter != null) { 395 | tabCount = adapter.getCount(); 396 | List tabList = new ArrayList<>(); 397 | for (int i = 0; i < tabCount; i++) { 398 | tabList.add(adapter.getPageTitle(i)); 399 | } 400 | setData(tabList); 401 | } else { 402 | tabCount = 0; 403 | } 404 | } 405 | } 406 | 407 | public static class TabLayoutOnPageChangeListener implements ViewPager.OnPageChangeListener { 408 | private final WeakReference mTabLayoutRef; 409 | private int mPreviousScrollState; 410 | private int mScrollState; 411 | 412 | public TabLayoutOnPageChangeListener(ModifyTabLayout tabLayout) { 413 | mTabLayoutRef = new WeakReference<>(tabLayout); 414 | } 415 | 416 | @Override 417 | public void onPageScrollStateChanged(final int state) { 418 | mPreviousScrollState = mScrollState; 419 | mScrollState = state; 420 | } 421 | 422 | @Override 423 | public void onPageScrolled(final int position, final float positionOffset, 424 | final int positionOffsetPixels) { 425 | // final ModifyTabLayout tabLayout = mTabLayoutRef.get(); 426 | // if (tabLayout != null) { 427 | // // Only update the text selection if we're not settling, or we are settling after 428 | // // being dragged 429 | // final boolean updateText = mScrollState != SCROLL_STATE_SETTLING || 430 | // mPreviousScrollState == SCROLL_STATE_DRAGGING; 431 | // // Update the indicator if we're not settling after being idle. This is caused 432 | // // from a setCurrentItem() call and will be handled by an animation from 433 | // // onPageSelected() instead. 434 | // final boolean updateIndicator = !(mScrollState == SCROLL_STATE_SETTLING 435 | // && mPreviousScrollState == SCROLL_STATE_IDLE); 436 | // tabLayout.setScrollPosition(position, positionOffset, updateText, updateIndicator); 437 | // } 438 | } 439 | 440 | @Override 441 | public void onPageSelected(final int position) { 442 | final ModifyTabLayout tabLayout = mTabLayoutRef.get(); 443 | if (tabLayout != null && tabLayout.getSelectedTabPosition() != position 444 | && position < tabLayout.getTabCount()) { 445 | // Select the tab, only updating the indicator if we're not being dragged/settled 446 | // (since onPageScrolled will handle that). 447 | // final boolean updateIndicator = mScrollState == SCROLL_STATE_IDLE 448 | // || (mScrollState == SCROLL_STATE_SETTLING 449 | // && mPreviousScrollState == SCROLL_STATE_IDLE); 450 | // tabLayout.selectTab(tabLayout.getTabAt(position), updateIndicator); 451 | tabLayout.clickTabWithItem(position); 452 | } 453 | } 454 | 455 | void reset() { 456 | mPreviousScrollState = mScrollState = SCROLL_STATE_IDLE; 457 | } 458 | } 459 | 460 | public int getSelectedTabPosition() { 461 | return selectedTabPosition; 462 | } 463 | 464 | public int getTabCount() { 465 | return tabCount; 466 | } 467 | 468 | public void setOnTabLayoutItemSelectListener(OnTabLayoutItemSelectListener listener) { 469 | onTabLayoutItemSelectListener = listener; 470 | } 471 | 472 | public interface OnTabLayoutItemSelectListener { 473 | 474 | void onTabLayoutItemSelect(int position);// 475 | 476 | } 477 | } 478 | --------------------------------------------------------------------------------