├── Ref
├── infer-out
├── .start
├── results.db-wal
├── results.db
├── results.db-shm
├── logs
└── toplevel.log
├── app
├── .gitignore
├── hold1.keystore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── dimens.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── drawable-xxhdpi
│ │ │ │ ├── ic_tabs.png
│ │ │ │ ├── tabs_web.png
│ │ │ │ ├── ic_adapter.png
│ │ │ │ ├── ic_colors.png
│ │ │ │ ├── ic_contact.png
│ │ │ │ ├── ic_divider.png
│ │ │ │ ├── ic_divider1.png
│ │ │ │ ├── ic_divider2.png
│ │ │ │ ├── ic_divider3.png
│ │ │ │ ├── tab_image.png
│ │ │ │ ├── tabs_custom.png
│ │ │ │ ├── tabs_text.png
│ │ │ │ ├── divider_diag.png
│ │ │ │ ├── ic_indicator.png
│ │ │ │ ├── tabs_custom_anim.png
│ │ │ │ └── ic_add_shopping_cart_black_24dp.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
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── drawable
│ │ │ │ ├── solid_color.xml
│ │ │ │ └── ic_launcher_background.xml
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ ├── demo_fragment.xml
│ │ │ │ ├── contact_fragment.xml
│ │ │ │ ├── tab_item.xml
│ │ │ │ ├── activity_main.xml
│ │ │ │ ├── activity_viewpager_2.xml
│ │ │ │ ├── activity_navigation.xml
│ │ │ │ ├── activity_start.xml
│ │ │ │ ├── colors_fragment.xml
│ │ │ │ ├── divider_fragment.xml
│ │ │ │ ├── adapters_fragment.xml
│ │ │ │ └── tabs_fragment.xml
│ │ │ ├── menu
│ │ │ │ └── menu.xml
│ │ │ ├── navigation
│ │ │ │ └── nav_graph.xml
│ │ │ └── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── hold1
│ │ │ │ └── pagertabsdemo
│ │ │ │ ├── fragments
│ │ │ │ ├── FragmentPresenter.kt
│ │ │ │ ├── DemoFragment.kt
│ │ │ │ ├── ContactFragment.kt
│ │ │ │ ├── AdaptersFragment.kt
│ │ │ │ ├── ColorsFragment.kt
│ │ │ │ ├── DividerFragment.kt
│ │ │ │ └── TabsFragment.kt
│ │ │ │ ├── TabbedActivity.kt
│ │ │ │ ├── StartActivity.kt
│ │ │ │ ├── TabItemView.kt
│ │ │ │ ├── Util.kt
│ │ │ │ ├── JetpackNavigationActivity.kt
│ │ │ │ ├── ViewPagerActivity.kt
│ │ │ │ └── ViewPager2Activity.kt
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── com
│ │ │ └── hold1
│ │ │ └── pagertabsdemo
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── hold1
│ │ └── pagertabsdemo
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── pagertabsindicator
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ └── values
│ │ │ ├── strings.xml
│ │ │ ├── colors.xml
│ │ │ ├── config.xml
│ │ │ └── attrs.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── hold1
│ │ └── pagertabsindicator
│ │ ├── TabInfo.kt
│ │ ├── TabViewProvider.kt
│ │ ├── TabView.kt
│ │ ├── Util.java
│ │ ├── adapters
│ │ ├── ViewPagerTabsAdapter.kt
│ │ ├── ViewPager2TabsAdapter.kt
│ │ └── TabsAdapter.kt
│ │ └── PagerTabsIndicator.kt
├── proguard-rules.pro
└── build.gradle
├── .idea
├── .gitignore
├── codeStyles
│ ├── codeStyleConfig.xml
│ └── Project.xml
├── compiler.xml
├── vcs.xml
├── modules.xml
├── gradle.xml
├── misc.xml
├── jarRepositories.xml
└── navEditor.xml
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── settings.gradle
├── gradle.properties
├── gradlew.bat
├── Readme.md
├── scripts
└── gradle-publishing.gradle
├── gradlew
└── LICENSE
/Ref:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/infer-out/.start:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/infer-out/results.db-wal:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/pagertabsindicator/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/.idea/.gitignore:
--------------------------------------------------------------------------------
1 | # Default ignored files
2 | /shelf/
3 | /workspace.xml
4 |
--------------------------------------------------------------------------------
/app/hold1.keystore:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/hold1.keystore
--------------------------------------------------------------------------------
/infer-out/results.db:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/infer-out/results.db
--------------------------------------------------------------------------------
/infer-out/results.db-shm:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/infer-out/results.db-shm
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | PagerTabsIndicator
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/pagertabsindicator/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | PagerTabsIndicator
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/ic_tabs.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/drawable-xxhdpi/ic_tabs.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/tabs_web.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/drawable-xxhdpi/tabs_web.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/ic_adapter.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/drawable-xxhdpi/ic_adapter.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/ic_colors.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/drawable-xxhdpi/ic_colors.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/ic_contact.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/drawable-xxhdpi/ic_contact.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/ic_divider.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/drawable-xxhdpi/ic_divider.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/ic_divider1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/drawable-xxhdpi/ic_divider1.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/ic_divider2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/drawable-xxhdpi/ic_divider2.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/ic_divider3.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/drawable-xxhdpi/ic_divider3.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/tab_image.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/drawable-xxhdpi/tab_image.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/tabs_custom.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/drawable-xxhdpi/tabs_custom.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/tabs_text.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/drawable-xxhdpi/tabs_text.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/divider_diag.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/drawable-xxhdpi/divider_diag.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/ic_indicator.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/drawable-xxhdpi/ic_indicator.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/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/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/tabs_custom_anim.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/drawable-xxhdpi/tabs_custom_anim.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/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/Crysis21/PagerTabIndicator/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/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/pagertabsindicator/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/.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/codeStyles/codeStyleConfig.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-xxhdpi/ic_add_shopping_cart_black_24dp.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/Crysis21/PagerTabIndicator/HEAD/app/src/main/res/drawable-xxhdpi/ic_add_shopping_cart_black_24dp.png
--------------------------------------------------------------------------------
/app/src/main/res/values/dimens.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 64dp
4 | 56dp
5 |
--------------------------------------------------------------------------------
/.idea/compiler.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/pagertabsindicator/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #ff7b16
4 | #a1a1a1
5 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/solid_color.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #737373
4 | #9b9b9b
5 | #ffa940
6 |
7 |
--------------------------------------------------------------------------------
/pagertabsindicator/src/main/java/com/hold1/pagertabsindicator/TabInfo.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsindicator
2 |
3 | open class TabInfo(
4 | val destinationId: Int,
5 | val name: String?,
6 | val imageUrl: String?,
7 | val imageResource: Int?
8 | )
9 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Nov 24 23:14:42 EET 2020
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-7.0.2-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hold1/pagertabsdemo/fragments/FragmentPresenter.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsdemo.fragments
2 |
3 | /**
4 | * Created by Cristian Holdunu on 16/11/2017.
5 | */
6 | interface FragmentPresenter {
7 | val tabName: String?
8 | val tabImage: Int
9 | val tabImageUrl: String?
10 | }
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/demo_fragment.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | dependencyResolutionManagement {
2 | repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
3 | repositories {
4 | google()
5 | mavenCentral()
6 | maven { url = 'https://dl.bintray.com/yuancloud/maven/' }
7 | maven { url 'https://jitpack.io' }
8 | }
9 | }
10 | include ':app', ':pagertabsindicator'
11 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hold1/pagertabsdemo/TabbedActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsdemo
2 |
3 | import com.hold1.pagertabsindicator.PagerTabsIndicator
4 |
5 | interface TabbedActivity {
6 |
7 | enum class TabAdapterType {
8 | TEXT, IMAGE, WEB, CUSTOM, CUSTOM_ANIM
9 | }
10 |
11 | val tabsIndicator: PagerTabsIndicator
12 |
13 | fun changeTabAdapter(adapterType: TabAdapterType?)
14 |
15 | fun addDummyTab()
16 |
17 | }
--------------------------------------------------------------------------------
/app/src/test/java/com/hold1/pagertabsdemo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsdemo;
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 | }
--------------------------------------------------------------------------------
/pagertabsindicator/src/main/res/values/config.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #363636
4 | #595959
5 | 16dp
6 | 8dp
7 | 8dp
8 | 1px
9 | 10dp
10 | 12dp
11 |
--------------------------------------------------------------------------------
/pagertabsindicator/src/main/java/com/hold1/pagertabsindicator/TabViewProvider.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsindicator
2 |
3 | import android.net.Uri
4 | import android.view.View
5 |
6 | sealed interface TabViewProvider {
7 |
8 | interface ImageResource: TabViewProvider {
9 | fun getTabIconUri(position: Int): Uri?
10 | fun getTabIconResId(position: Int): Int
11 | }
12 |
13 | interface ViewResource: TabViewProvider {
14 | fun getTabView(position: Int): View
15 | }
16 |
17 | interface TextResource: TabViewProvider {
18 | fun getTabTitle(position: Int): String
19 | }
20 | }
--------------------------------------------------------------------------------
/app/src/main/res/menu/menu.xml:
--------------------------------------------------------------------------------
1 |
2 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
16 |
17 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/pagertabsindicator/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/java/com/hold1/pagertabsdemo/fragments/DemoFragment.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsdemo.fragments
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import android.view.View
6 | import android.view.ViewGroup
7 | import androidx.fragment.app.Fragment
8 | import com.hold1.pagertabsdemo.R
9 |
10 | /**
11 | * Created by Cristian Holdunu on 08/11/2017.
12 | */
13 | class DemoFragment : Fragment(), FragmentPresenter {
14 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
15 | return inflater.inflate(R.layout.demo_fragment, null)
16 | }
17 |
18 | override val tabName: String
19 | get() = "Dummy"
20 | override val tabImage: Int
21 | get() = R.drawable.ic_add_shopping_cart_black_24dp
22 | override val tabImageUrl: String?
23 | get() = null
24 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hold1/pagertabsdemo/StartActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsdemo
2 |
3 | import android.content.Intent
4 | import android.os.Bundle
5 | import androidx.appcompat.app.AppCompatActivity
6 | import com.hold1.pagertabsdemo.databinding.ActivityStartBinding
7 |
8 | class StartActivity: AppCompatActivity() {
9 |
10 | lateinit var binding: ActivityStartBinding
11 |
12 | override fun onCreate(savedInstanceState: Bundle?) {
13 | super.onCreate(savedInstanceState)
14 | binding = ActivityStartBinding.inflate(layoutInflater)
15 | setContentView(binding.root)
16 |
17 | binding.viewPager1.setOnClickListener {
18 | startActivity(Intent(this, ViewPagerActivity::class.java))
19 | }
20 |
21 | binding.viewPager2.setOnClickListener {
22 | startActivity(Intent(this, ViewPager2Activity::class.java))
23 | }
24 | }
25 | }
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/hold1/pagertabsdemo/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsdemo;
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() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.hold1.pagertabsindicator", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
21 |
22 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/contact_fragment.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
21 |
--------------------------------------------------------------------------------
/infer-out/logs:
--------------------------------------------------------------------------------
1 | [29592][environment] CWD = /Volumes/MacHD/Gradle/PagerTabsIndicator
2 | [29592][environment] No .inferconfig file found
3 | [29592][environment] Project root = /Volumes/MacHD/Gradle/PagerTabsIndicator
4 | [29592][environment] INFER_ARGS = @/var/folders/qn/mmtlv9ys0tgfj7rs48jw4dp40000gn/T/args_7effb6
5 | [29592][environment] Contents of '/var/folders/qn/mmtlv9ys0tgfj7rs48jw4dp40000gn/T/args_7effb6'
6 | [29592][environment] run
7 | [29592][environment] /Contents of '/var/folders/qn/mmtlv9ys0tgfj7rs48jw4dp40000gn/T/args_7effb6'
8 | [29592][environment] command line arguments: /usr/local/Cellar/infer/0.13.0/libexec/bin/infer^run^--^gradle^build
9 | [29592][environment] Analyzer: checkers
10 | [29592][environment] Active checkers: annotation reachability (Java), biabduction (C/C++/ObjC, Java), fragment retains view (Java), immutable cast (Java), liveness (C/C++/ObjC), printf args (Java), quandary (C/C++/ObjC, Java), RacerD (C/C++/ObjC, Java), SIOF (C/C++/ObjC)
11 | [29592][ progress] Capturing in gradle mode...
12 | [29592][environment] Infer version v0.13.0
13 | [29592][environment] Copyright 2009 - present Facebook. All Rights Reserved.
14 | [29592][environment]
--------------------------------------------------------------------------------
/app/src/main/res/navigation/nav_graph.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
15 |
19 |
23 |
27 |
--------------------------------------------------------------------------------
/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=-Xmx4096m -Dfile.encoding=UTF-8
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 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app"s APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 | # Kotlin code style for this project: "official" or "obsolete":
21 | kotlin.code.style=official
22 | kapt.use.worker.api=false
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
7 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
--------------------------------------------------------------------------------
/pagertabsindicator/src/main/java/com/hold1/pagertabsindicator/TabView.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsindicator
2 |
3 | import android.widget.FrameLayout
4 | import android.animation.ValueAnimator
5 | import android.content.Context
6 | import android.graphics.Color
7 | import android.util.AttributeSet
8 | import android.view.View
9 |
10 | /**
11 | * Created by Cristian Holdunu on 09/11/2017.
12 | */
13 | open class TabView : FrameLayout {
14 | private val bgAnimator: ValueAnimator? = null
15 | private val pressColor = Color.TRANSPARENT
16 | private val currentBgColor = pressColor
17 | private var offset = 0f
18 |
19 | constructor(context: Context) : super(context) {
20 | setWillNotDraw(false)
21 | }
22 |
23 | constructor(context: Context, child: View?) : this(context) {
24 | addView(child)
25 | }
26 |
27 | constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
28 | setWillNotDraw(false)
29 | }
30 |
31 | constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
32 | context,
33 | attrs,
34 | defStyleAttr
35 | ) {
36 | setWillNotDraw(false)
37 | }
38 |
39 | open fun onOffset(offset: Float) {
40 | this.offset = offset
41 | invalidate()
42 | }
43 |
44 | companion object {
45 | private val TAG = TabView::class.java.simpleName
46 | }
47 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hold1/pagertabsdemo/TabItemView.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsdemo
2 |
3 | import android.content.Context
4 | import com.hold1.pagertabsdemo.Util.getColorWithOpacity
5 | import com.hold1.pagertabsdemo.Util.mixTwoColors
6 | import android.widget.TextView
7 | import android.view.LayoutInflater
8 | import android.widget.ImageView
9 | import com.hold1.pagertabsdemo.R
10 | import com.hold1.pagertabsindicator.TabView
11 |
12 | /**
13 | * Created by Cristian Holdunu on 17/11/2017.
14 | */
15 | class TabItemView(context: Context, title: String?, imgRes: Int, tabColor: Int, activeColor: Int) : TabView(context) {
16 | private val tabTitle: TextView
17 | private val tabIcon: ImageView
18 | private val activeColor: Int
19 | private val tabColor: Int
20 | override fun onOffset(offset: Float) {
21 | tabIcon.setColorFilter(getColorWithOpacity(activeColor, (100 * offset).toInt()))
22 | tabTitle.setTextColor(mixTwoColors(activeColor, tabColor, offset))
23 | }
24 |
25 | init {
26 | LayoutInflater.from(context).inflate(R.layout.tab_item, this)
27 | tabTitle = findViewById(R.id.tab_name)
28 | tabIcon = findViewById(R.id.tab_icon)
29 | tabTitle.text = title
30 | tabIcon.setImageResource(imgRes)
31 | this.tabColor = tabColor
32 | this.activeColor = activeColor
33 | tabIcon.setColorFilter(tabColor)
34 | tabTitle.setTextColor(tabColor)
35 | }
36 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hold1/pagertabsdemo/fragments/ContactFragment.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsdemo.fragments
2 |
3 | import android.content.Intent
4 | import android.net.Uri
5 | import android.os.Bundle
6 | import android.view.LayoutInflater
7 | import android.view.View
8 | import android.view.ViewGroup
9 | import androidx.fragment.app.Fragment
10 | import com.hold1.pagertabsdemo.R
11 | import com.hold1.pagertabsdemo.databinding.ContactFragmentBinding
12 |
13 | /**
14 | * Created by Cristian Holdunu on 08/11/2017.
15 | */
16 | class ContactFragment : Fragment(), FragmentPresenter {
17 |
18 | private lateinit var binding: ContactFragmentBinding
19 |
20 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
21 | binding = ContactFragmentBinding.inflate(inflater, container, false)
22 | return binding.root
23 | }
24 |
25 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
26 | super.onViewCreated(view, savedInstanceState)
27 |
28 | binding.openGit.setOnClickListener {
29 | val intent = Intent(Intent.ACTION_VIEW,
30 | Uri.parse("https://github.com/Crysis21/PagerTabIndicator"))
31 | startActivity(intent)
32 | }
33 | }
34 |
35 | override val tabName: String = "Contact"
36 | override val tabImage: Int = R.drawable.ic_contact
37 | override val tabImageUrl: String? = null
38 | }
--------------------------------------------------------------------------------
/pagertabsindicator/src/main/java/com/hold1/pagertabsindicator/Util.java:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsindicator;
2 |
3 | import android.graphics.Color;
4 |
5 | /**
6 | * Created by Cristian Holdunu on 20/11/2017.
7 | */
8 |
9 | public class Util {
10 | public static int getColorWithOpacity(int color, int opacity) {
11 | return Color.argb(opacity * 255 / 100, Color.red(color), Color.green(color), Color.blue(color));
12 | }
13 |
14 | public static int mixTwoColors(int color1, int color2, float amount) {
15 | final byte ALPHA_CHANNEL = 24;
16 | final byte RED_CHANNEL = 16;
17 | final byte GREEN_CHANNEL = 8;
18 | final byte BLUE_CHANNEL = 0;
19 |
20 | final float inverseAmount = 1.0f - amount;
21 |
22 | int a = ((int) (((float) (color1 >> ALPHA_CHANNEL & 0xff) * amount) +
23 | ((float) (color2 >> ALPHA_CHANNEL & 0xff) * inverseAmount))) & 0xff;
24 | int r = ((int) (((float) (color1 >> RED_CHANNEL & 0xff) * amount) +
25 | ((float) (color2 >> RED_CHANNEL & 0xff) * inverseAmount))) & 0xff;
26 | int g = ((int) (((float) (color1 >> GREEN_CHANNEL & 0xff) * amount) +
27 | ((float) (color2 >> GREEN_CHANNEL & 0xff) * inverseAmount))) & 0xff;
28 | int b = ((int) (((float) (color1 & 0xff) * amount) +
29 | ((float) (color2 & 0xff) * inverseAmount))) & 0xff;
30 |
31 | return a << ALPHA_CHANNEL | r << RED_CHANNEL | g << GREEN_CHANNEL | b << BLUE_CHANNEL;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/.idea/jarRepositories.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 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/tab_item.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
22 |
23 |
35 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
21 |
22 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_viewpager_2.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
21 |
22 |
34 |
35 |
36 |
--------------------------------------------------------------------------------
/infer-out/toplevel.log:
--------------------------------------------------------------------------------
1 | [INFO] output of locale.getdefaultlocale(): ('en_RO', 'UTF-8')
2 | [INFO] encoding we chose in the end: ascii
3 | [INFO] Running command /usr/local/Cellar/infer/0.13.0/lib/infer/infer/bin/../lib/python/infer.py --analyzer checkers -j 8 --project-root /Volumes/MacHD/Gradle/PagerTabsIndicator --out /Volumes/MacHD/Gradle/PagerTabsIndicator/infer-out -- gradle build
4 | [INFO] Path to infer script /usr/local/Cellar/infer/0.13.0/lib/infer/infer/bin/../lib/python/infer.py (/usr/local/Cellar/infer/0.13.0/lib/infer/infer/lib/python/infer.py)
5 | [INFO] Platform: Darwin-17.2.0-x86_64-i386-64bit
6 | [INFO] PATH=/usr/local/php5/bin:/Users/cristi/Library/Python/2.7/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/php5/bin:/Users/cristi/Library/Python/2.7/bin:/Volumes/MacHD/android-sdk-macosx/platform-tools/:/Users/cristi/pear/bin:/Volumes/MacHD/android-sdk-macosx/build-tools/23.0.2:/Volumes/MacHD/android-sdk-macosx/emulator/:/Volumes/MacHD/android-sdk-macosx/platform-tools/:/Users/cristi/pear/bin:/Volumes/MacHD/android-sdk-macosx/build-tools/23.0.2:/Volumes/MacHD/android-sdk-macosx/emulator/
7 | [INFO] SHELL=/bin/bash
8 | [INFO] PWD=/Volumes/MacHD/Gradle/PagerTabsIndicator
9 | [INFO] Running with:
10 |
11 | ------------------------------------------------------------
12 | Gradle 4.2.1
13 | ------------------------------------------------------------
14 |
15 | Build time: 2017-10-02 15:36:21 UTC
16 | Revision: a88ebd6be7840c2e59ae4782eb0f27fbe3405ddf
17 |
18 | Groovy: 2.4.12
19 | Ant: Apache Ant(TM) version 1.9.6 compiled on June 29 2015
20 | JVM: 1.8.0_112 (Oracle Corporation 25.112-b16)
21 | OS: Mac OS X 10.13.1 x86_64
22 |
23 |
24 | [ERROR] Error during capture phase, exiting
25 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_navigation.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
23 |
24 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_start.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
17 |
18 |
28 |
29 |
39 |
--------------------------------------------------------------------------------
/pagertabsindicator/src/main/java/com/hold1/pagertabsindicator/adapters/ViewPagerTabsAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsindicator.adapters
2 |
3 | import android.database.DataSetObserver
4 | import androidx.viewpager.widget.PagerAdapter
5 | import androidx.viewpager.widget.ViewPager
6 |
7 | class ViewPagerTabsAdapter(viewPager: ViewPager) : TabsAdapter(), ViewPager.OnPageChangeListener {
8 | private var adapter: PagerAdapter? = null
9 |
10 | private var adapterChangeListener: ViewPager.OnAdapterChangeListener = ViewPager.OnAdapterChangeListener { _, _, newAdapter ->
11 | listenToAdapterChanges(newAdapter)
12 | }
13 | private var adapterObserver: DataSetObserver = object : DataSetObserver() {
14 | override fun onChanged() {
15 | pagerTabsIndicator?.invalidateViews()
16 | }
17 | }
18 |
19 | init {
20 | viewPager.addOnPageChangeListener(this)
21 | viewPager.addOnAdapterChangeListener(adapterChangeListener)
22 | listenToAdapterChanges(viewPager.adapter)
23 | }
24 |
25 | private fun listenToAdapterChanges(pagerAdapter: PagerAdapter?) {
26 | //remove old adapter if present
27 | adapter?.unregisterDataSetObserver(adapterObserver)
28 | adapter = pagerAdapter
29 | adapter?.registerDataSetObserver(adapterObserver)
30 | pagerTabsIndicator?.invalidateViews()
31 | }
32 |
33 | override fun getCount(): Int {
34 | return adapter?.count ?: 0
35 | }
36 |
37 | override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
38 | pagerTabsIndicator?.onPageScrolled(position, positionOffset, positionOffsetPixels)
39 | }
40 |
41 | override fun onPageSelected(position: Int) {
42 | pagerTabsIndicator?.onPageSelected(position)
43 | }
44 |
45 | override fun onPageScrollStateChanged(state: Int) {
46 | pagerTabsIndicator?.onPageScrollStateChanged(state)
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/pagertabsindicator/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'kotlin-android'
3 |
4 | ext {
5 | PUBLISH_GROUP_ID = 'ro.holdone'
6 | PUBLISH_ARTIFACT_ID = 'pagertabindicator'
7 | PUBLISH_VERSION = "2.0.0-alpha04"
8 |
9 | POM_NAME = 'pagertabindicator'
10 | POM_PACKAGING = 'aar'
11 |
12 | POM_DESCRIPTION = 'A flexible library for adding tabs to a view pager.'
13 | POM_INCEPTION_YEAR = '2017'
14 |
15 | POM_URL = 'https://github.com/Crysis21/PagerTabIndicator'
16 | POM_SCM_URL = 'https://github.com/Crysis21/PagerTabIndicator'
17 | POM_SCM_CONNECTION = 'scm:git@github.com:Crysis21/PagerTabIndicator.git'
18 | POM_SCM_DEV_CONNECTION = 'scm:git@github.com:Crysis21/PagerTabIndicator.git'
19 |
20 | POM_LICENCE_NAME = 'The Apache Software License, Version 2.0'
21 | POM_LICENCE_URL = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
22 | POM_LICENCE_DIST = 'repo'
23 |
24 | POM_DEVELOPER_ID = 'Crysis21'
25 | POM_DEVELOPER_NAME = 'Cristian Holdunu'
26 | POM_DEVELOPER_URL = 'https://holdone.ro'
27 | POM_DEVELOPER_EMAIL = "cristian.holdunu@gmail.com"
28 | }
29 |
30 | apply from: "${rootProject.projectDir}/scripts/gradle-publishing.gradle"
31 |
32 | android {
33 | compileSdkVersion 30
34 |
35 | defaultConfig {
36 | minSdkVersion 15
37 | }
38 |
39 | buildTypes {
40 | release {
41 | minifyEnabled false
42 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
43 | }
44 | }
45 |
46 | }
47 |
48 | dependencies {
49 | implementation 'androidx.appcompat:appcompat:1.3.1'
50 | implementation 'com.github.bumptech.glide:glide:4.12.0'
51 | annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
52 | implementation "androidx.core:core-ktx:1.6.0"
53 | implementation "androidx.viewpager2:viewpager2:1.0.0"
54 | implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
55 | }
56 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | id 'kotlin-android'
4 | }
5 | android {
6 | compileSdkVersion 30
7 | defaultConfig {
8 | applicationId "com.hold1.pagertabsdemo"
9 | minSdkVersion 16
10 | versionCode 107
11 | versionName "1.0.7"
12 | testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
13 | }
14 | signingConfigs {
15 | release {
16 | storeFile file("hold1.keystore")
17 | storePassword "itsmylif3"
18 | keyAlias "hold1"
19 | keyPassword "itsmylif3"
20 | }
21 | }
22 | buildTypes {
23 | release {
24 | minifyEnabled false
25 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
26 | signingConfig signingConfigs.release
27 | }
28 | }
29 | buildFeatures {
30 | viewBinding true
31 | }
32 | }
33 |
34 | dependencies {
35 | implementation fileTree(include: ['*.jar'], dir: 'libs')
36 | implementation 'androidx.appcompat:appcompat:1.3.1'
37 | implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
38 | implementation 'androidx.cardview:cardview:1.0.0'
39 |
40 | implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
41 | implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
42 |
43 | implementation 'com.github.QuadFlask:colorpicker:0.0.13'
44 | implementation 'com.google.android.flexbox:flexbox:3.0.0'
45 | implementation "androidx.viewpager2:viewpager2:1.0.0"
46 |
47 | implementation 'com.github.bumptech.glide:glide:4.12.0'
48 | annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
49 |
50 | testImplementation 'junit:junit:4.13.2'
51 | androidTestImplementation 'androidx.test.ext:junit:1.1.3'
52 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
53 | implementation project(':pagertabsindicator')
54 | }
55 |
--------------------------------------------------------------------------------
/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/hold1/pagertabsdemo/Util.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsdemo
2 |
3 | import android.graphics.Color
4 | import android.graphics.Rect
5 | import android.graphics.RectF
6 |
7 | /**
8 | * Created by Cristian Holdunu on 20/11/2017.
9 | */
10 | object Util {
11 | @JvmStatic
12 | fun getColorWithOpacity(color: Int, opacity: Int): Int {
13 | return Color.argb(
14 | opacity * 255 / 100,
15 | Color.red(color),
16 | Color.green(color),
17 | Color.blue(color)
18 | )
19 | }
20 |
21 | @JvmStatic
22 | fun mixTwoColors(color1: Int, color2: Int, amount: Float): Int {
23 | val ALPHA_CHANNEL: Byte = 24
24 | val RED_CHANNEL: Byte = 16
25 | val GREEN_CHANNEL: Byte = 8
26 | val BLUE_CHANNEL: Byte = 0
27 | val inverseAmount = 1.0f - amount
28 | val a = ((color1 shr ALPHA_CHANNEL.toInt() and 0xff).toFloat() * amount +
29 | (color2 shr ALPHA_CHANNEL.toInt() and 0xff).toFloat() * inverseAmount).toInt() and 0xff
30 | val r = ((color1 shr RED_CHANNEL.toInt() and 0xff).toFloat() * amount +
31 | (color2 shr RED_CHANNEL.toInt() and 0xff).toFloat() * inverseAmount).toInt() and 0xff
32 | val g = ((color1 shr GREEN_CHANNEL.toInt() and 0xff).toFloat() * amount +
33 | (color2 shr GREEN_CHANNEL.toInt() and 0xff).toFloat() * inverseAmount).toInt() and 0xff
34 | val b = ((color1 and 0xff).toFloat() * amount +
35 | (color2 and 0xff).toFloat() * inverseAmount).toInt() and 0xff
36 | return a shl ALPHA_CHANNEL.toInt() or (r shl RED_CHANNEL.toInt()) or (g shl GREEN_CHANNEL.toInt()) or (b shl BLUE_CHANNEL.toInt())
37 | }
38 | }
39 |
40 | fun Rect.toRectF(): RectF {
41 | return RectF(
42 | this.left.toFloat(), this.top.toFloat(), this.right.toFloat(),
43 | this.bottom.toFloat()
44 | )
45 | }
46 |
47 | fun RectF.toRect(): Rect {
48 | return Rect(
49 | this.left.toInt(), this.top.toInt(), this.right.toInt(),
50 | this.bottom.toInt()
51 | )
52 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hold1/pagertabsdemo/fragments/AdaptersFragment.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsdemo.fragments
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import android.view.View
6 | import android.view.ViewGroup
7 | import androidx.fragment.app.Fragment
8 | import com.hold1.pagertabsdemo.TabbedActivity
9 | import com.hold1.pagertabsdemo.R
10 | import com.hold1.pagertabsdemo.databinding.AdaptersFragmentBinding
11 | import com.hold1.pagertabsindicator.PagerTabsIndicator
12 |
13 | /**
14 | * Created by Cristian Holdunu on 08/11/2017.
15 | */
16 | class AdaptersFragment : Fragment(), FragmentPresenter {
17 | private var tabsIndicator: PagerTabsIndicator? = null
18 |
19 | private lateinit var binding: AdaptersFragmentBinding
20 |
21 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
22 | binding = AdaptersFragmentBinding.inflate(layoutInflater, container, false)
23 | return binding.root
24 | }
25 |
26 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
27 | super.onViewCreated(view, savedInstanceState)
28 | tabsIndicator = (activity as? TabbedActivity)?.tabsIndicator
29 | binding.tabsText.setOnClickListener { (activity as? TabbedActivity)?.changeTabAdapter(TabbedActivity.TabAdapterType.TEXT) }
30 | binding.tabsImage.setOnClickListener { (activity as? TabbedActivity)?.changeTabAdapter(TabbedActivity.TabAdapterType.IMAGE) }
31 | binding.tabsWeb.setOnClickListener { (activity as? TabbedActivity)?.changeTabAdapter(TabbedActivity.TabAdapterType.WEB) }
32 | binding.tabsCustom.setOnClickListener { (activity as? TabbedActivity)?.changeTabAdapter(TabbedActivity.TabAdapterType.CUSTOM) }
33 | binding.tabsCustomAnim.setOnClickListener { (activity as? TabbedActivity)?.changeTabAdapter(TabbedActivity.TabAdapterType.CUSTOM_ANIM) }
34 | }
35 |
36 | override val tabName: String = "Adapters"
37 | override val tabImage: Int = R.drawable.ic_adapter
38 | override val tabImageUrl: String? = null
39 | }
--------------------------------------------------------------------------------
/pagertabsindicator/src/main/res/values/attrs.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 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hold1/pagertabsdemo/fragments/ColorsFragment.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsdemo.fragments
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import android.view.View
6 | import android.view.ViewGroup
7 | import androidx.fragment.app.Fragment
8 | import com.hold1.pagertabsdemo.TabbedActivity
9 | import com.hold1.pagertabsdemo.R
10 | import com.hold1.pagertabsdemo.databinding.ColorsFragmentBinding
11 | import com.hold1.pagertabsindicator.PagerTabsIndicator
12 |
13 | /**
14 | * Created by Cristian Holdunu on 08/11/2017.
15 | */
16 | class ColorsFragment : Fragment(), FragmentPresenter {
17 | private var tabsIndicator: PagerTabsIndicator? = null
18 |
19 | private lateinit var binding: ColorsFragmentBinding
20 |
21 | private var colorClick: View.OnClickListener? = null
22 |
23 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
24 | binding = ColorsFragmentBinding.inflate(inflater, container, false)
25 | return binding.root
26 | }
27 |
28 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
29 | super.onViewCreated(view, savedInstanceState)
30 | tabsIndicator = (activity as? TabbedActivity)?.tabsIndicator
31 | colorClick = View.OnClickListener { v ->
32 | when (v.id) {
33 | R.id.color_text -> tabsIndicator?.textColor = binding.colorPickerView.selectedColor
34 | R.id.color_ind_bg -> tabsIndicator?.indicatorBgColor = binding.colorPickerView.selectedColor
35 | R.id.color_ind -> tabsIndicator?.indicatorColor = binding.colorPickerView.selectedColor
36 | R.id.color_div -> tabsIndicator?.dividerColor = binding.colorPickerView.selectedColor
37 | R.id.color_bg -> tabsIndicator?.setBackgroundColor(binding.colorPickerView.selectedColor)
38 | R.id.highlight_color -> tabsIndicator?.highlightTextColor = binding.colorPickerView.selectedColor
39 | }
40 | }
41 | for (i in 0 until binding.elements.childCount) binding.elements.getChildAt(i).setOnClickListener(colorClick)
42 | }
43 |
44 | override val tabName: String = "Colors"
45 | override val tabImage: Int = R.drawable.ic_colors
46 | override val tabImageUrl: String? = null
47 | }
--------------------------------------------------------------------------------
/pagertabsindicator/src/main/java/com/hold1/pagertabsindicator/adapters/ViewPager2TabsAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsindicator.adapters
2 |
3 | import android.view.View
4 | import androidx.recyclerview.widget.RecyclerView
5 | import androidx.viewpager2.widget.ViewPager2
6 | import com.bumptech.glide.Glide
7 | import com.hold1.pagertabsindicator.TabViewProvider
8 |
9 | class ViewPager2TabsAdapter(val viewPager2: ViewPager2) : TabsAdapter() {
10 | private val pagerAdapter: RecyclerView.Adapter = viewPager2.adapter!!
11 |
12 | private val onPageChangeCallback = object : ViewPager2.OnPageChangeCallback() {
13 | override fun onPageScrolled(
14 | position: Int,
15 | positionOffset: Float,
16 | positionOffsetPixels: Int
17 | ) {
18 | pagerTabsIndicator?.onPageScrolled(position, positionOffset, positionOffsetPixels)
19 | }
20 |
21 | override fun onPageSelected(position: Int) {
22 | pagerTabsIndicator?.onPageSelected(position)
23 | }
24 |
25 | override fun onPageScrollStateChanged(state: Int) {
26 | pagerTabsIndicator?.onPageScrollStateChanged(state)
27 | }
28 | }
29 |
30 | private val adapterDataObserver = object : RecyclerView.AdapterDataObserver() {
31 | override fun onChanged() {
32 | pagerTabsIndicator?.invalidateViews()
33 | }
34 |
35 | override fun onItemRangeChanged(positionStart: Int, itemCount: Int) {
36 | pagerTabsIndicator?.invalidateViews()
37 | }
38 |
39 | override fun onItemRangeChanged(positionStart: Int, itemCount: Int, payload: Any?) {
40 | pagerTabsIndicator?.invalidateViews()
41 | }
42 |
43 | override fun onItemRangeInserted(positionStart: Int, itemCount: Int) {
44 | pagerTabsIndicator?.invalidateViews()
45 | }
46 |
47 | override fun onItemRangeRemoved(positionStart: Int, itemCount: Int) {
48 | pagerTabsIndicator?.invalidateViews()
49 | }
50 |
51 | override fun onItemRangeMoved(fromPosition: Int, toPosition: Int, itemCount: Int) {
52 | pagerTabsIndicator?.invalidateViews()
53 | }
54 | }
55 |
56 | init {
57 | pagerAdapter.registerAdapterDataObserver(adapterDataObserver)
58 | viewPager2.registerOnPageChangeCallback(onPageChangeCallback)
59 | }
60 |
61 | override fun getCount(): Int {
62 | return pagerAdapter.itemCount
63 | }
64 | }
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/.idea/navEditor.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
77 |
78 |
79 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hold1/pagertabsdemo/fragments/DividerFragment.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsdemo.fragments
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import android.view.View
6 | import android.view.ViewGroup
7 | import android.widget.SeekBar
8 | import android.widget.SeekBar.OnSeekBarChangeListener
9 | import androidx.fragment.app.Fragment
10 | import com.hold1.pagertabsdemo.TabbedActivity
11 | import com.hold1.pagertabsdemo.R
12 | import com.hold1.pagertabsdemo.databinding.DividerFragmentBinding
13 | import com.hold1.pagertabsindicator.PagerTabsIndicator
14 |
15 | /**
16 | * Created by Cristian Holdunu on 08/11/2017.
17 | */
18 | class DividerFragment : Fragment(), FragmentPresenter {
19 | private var tabsIndicator: PagerTabsIndicator? = null
20 |
21 | private lateinit var binding: DividerFragmentBinding
22 |
23 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
24 | binding = DividerFragmentBinding.inflate(inflater, container, false)
25 | return binding.root
26 | }
27 |
28 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
29 | super.onViewCreated(view, savedInstanceState)
30 | tabsIndicator = (activity as? TabbedActivity)?.tabsIndicator
31 | binding.divWidth.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
32 | override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
33 | tabsIndicator!!.dividerWidth = progress
34 | }
35 |
36 | override fun onStartTrackingTouch(seekBar: SeekBar) {}
37 | override fun onStopTrackingTouch(seekBar: SeekBar) {}
38 | })
39 | binding.divMargin.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
40 | override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
41 | tabsIndicator?.dividerMargin = progress
42 | }
43 |
44 | override fun onStartTrackingTouch(seekBar: SeekBar) {}
45 | override fun onStopTrackingTouch(seekBar: SeekBar) {}
46 | })
47 | binding.showDivider.setOnCheckedChangeListener { _, isChecked -> tabsIndicator!!.isShowDivider = isChecked }
48 | binding.solidColor.setOnClickListener {
49 | tabsIndicator?.dividerResource = -1
50 | tabsIndicator?.dividerWidth = 1
51 | }
52 | binding.divider1.setOnClickListener {
53 | tabsIndicator?.dividerResource = R.drawable.ic_divider1
54 | tabsIndicator?.dividerWidth = 20
55 | tabsIndicator?.dividerMargin = 40
56 | }
57 | binding.divider2.setOnClickListener {
58 | tabsIndicator?.dividerResource = R.drawable.ic_divider2
59 | tabsIndicator?.dividerWidth = 18
60 | tabsIndicator?.dividerMargin = 30
61 | }
62 | binding.divider3.setOnClickListener {
63 | tabsIndicator?.dividerResource = R.drawable.ic_divider3
64 | tabsIndicator?.dividerWidth = 40
65 | tabsIndicator?.dividerMargin = 36
66 | }
67 | }
68 |
69 | override val tabName: String
70 | get() = "Divider"
71 | override val tabImage: Int
72 | get() = R.drawable.ic_divider
73 | override val tabImageUrl: String
74 | get() = "https://s3-us-west-2.amazonaws.com/anaface-pictures/ic_divider.png"
75 | }
--------------------------------------------------------------------------------
/pagertabsindicator/src/main/java/com/hold1/pagertabsindicator/adapters/TabsAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsindicator.adapters
2 |
3 | import android.content.Context
4 | import android.util.TypedValue
5 | import android.view.Gravity
6 | import android.view.View
7 | import android.widget.ImageView
8 | import android.widget.TextView
9 | import com.bumptech.glide.Glide
10 | import com.hold1.pagertabsindicator.PagerTabsIndicator
11 | import com.hold1.pagertabsindicator.TabViewProvider
12 |
13 | abstract class TabsAdapter() {
14 |
15 | private var context: Context? = null
16 |
17 | var pagerTabsIndicator: PagerTabsIndicator? = null
18 | set(value) {
19 | field = value
20 | context = value?.context
21 | onAttachedToTabsIndicator()
22 | }
23 |
24 | var tabViewProvider: TabViewProvider? = null
25 | set(value) {
26 | field = value
27 | pagerTabsIndicator?.invalidateViews()
28 | }
29 |
30 | abstract fun getCount(): Int
31 |
32 | open fun getTabAt(position: Int): View {
33 | tabViewProvider?.let { viewProvider ->
34 | when (viewProvider) {
35 | is TabViewProvider.ImageResource -> {
36 | val imageView = createImageView()
37 | viewProvider.getTabIconUri(position)?.let { uri ->
38 | context?.let {
39 | Glide.with(it)
40 | .load(uri)
41 | .into(imageView)
42 | }
43 | } ?: run {
44 | imageView.setImageResource(viewProvider.getTabIconResId(position))
45 | }
46 | return@getTabAt imageView
47 | }
48 | is TabViewProvider.ViewResource -> {
49 | return@getTabAt viewProvider.getTabView(position)
50 | }
51 | is TabViewProvider.TextResource -> {
52 | return@getTabAt createTextView(viewProvider.getTabTitle(position))
53 | }
54 | }
55 | }
56 | throw NotImplementedError("TabViewProvider not set to adapter")
57 | }
58 |
59 | private fun onAttachedToTabsIndicator() {
60 | pagerTabsIndicator?.invalidateViews()
61 | }
62 |
63 | protected fun createImageView(): ImageView {
64 | val imageView = ImageView(context)
65 | imageView.scaleType = ImageView.ScaleType.FIT_XY
66 | imageView.adjustViewBounds = true
67 | return imageView
68 | }
69 |
70 | protected fun createTextView(text: String): View {
71 | val textView = TextView(context)
72 | textView.text = text
73 | textView.gravity = Gravity.CENTER
74 | textView.setSingleLine()
75 | pagerTabsIndicator?.let {
76 | textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, (it.textSize).toFloat())
77 | textView.setTextColor(it.textColor)
78 |
79 | }
80 | // return object : TabView(this@PagerTabsIndicator.context, textView) {
81 | // override fun onOffset(offset: Float) {
82 | // super.onOffset(offset)
83 | // if (isHighlightText) {
84 | // (getChildAt(0) as TextView).setTextColor(
85 | // Util.mixTwoColors(
86 | // highlightTextColor,
87 | // textColor,
88 | // offset
89 | // )
90 | // )
91 | // }
92 | // }
93 | // }
94 | return textView
95 | }
96 | }
97 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/colors_fragment.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
10 |
17 |
18 |
28 |
29 |
33 |
34 |
38 |
39 |
40 |
47 |
48 |
55 |
56 |
62 |
63 |
64 |
70 |
71 |
77 |
78 |
84 |
85 |
91 |
92 |
98 |
99 |
100 |
101 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 | xmlns:android
17 |
18 | ^$
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 | xmlns:.*
28 |
29 | ^$
30 |
31 |
32 | BY_NAME
33 |
34 |
35 |
36 |
37 |
38 |
39 | .*:id
40 |
41 | http://schemas.android.com/apk/res/android
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 | .*:name
51 |
52 | http://schemas.android.com/apk/res/android
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 | name
62 |
63 | ^$
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 | style
73 |
74 | ^$
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 | .*
84 |
85 | ^$
86 |
87 |
88 | BY_NAME
89 |
90 |
91 |
92 |
93 |
94 |
95 | .*
96 |
97 | http://schemas.android.com/apk/res/android
98 |
99 |
100 | ANDROID_ATTRIBUTE_ORDER
101 |
102 |
103 |
104 |
105 |
106 |
107 | .*
108 |
109 | .*
110 |
111 |
112 | BY_NAME
113 |
114 |
115 |
116 |
117 |
118 |
119 |
120 |
121 |
122 |
--------------------------------------------------------------------------------
/Readme.md:
--------------------------------------------------------------------------------
1 | # Pager Tabs Indicator
2 |
3 | PagerTabIndicator started as a customisable android library that designed to work with the ViewPager component. After a few years I decided to revamp the project for my current needs so I decided to update it to also support `ViewPager2` and Jetpack navigation. I'm updating the project as my time allows it. PRs are always welcome.
4 |
5 | #### Features
6 | * Support dynamic adapter switch of the ViewPager / ViewPager2 / Jetpack Navigation
7 | * Customizable tab indicator (bottom/top/background)
8 | * Built in support for text/image/text+image tabs
9 | * Support for custom views in tabs
10 |
11 |
12 | #### Installation
13 | Project has been moved on maven central
14 |
15 | ```gradle
16 | implementation 'ro.holdone:pagertabindicator:2.0.0-alpha04'
17 | ```
18 |
19 | Inside your layout insert a PagerTabIndicator element
20 |
21 | ```xml
22 |
29 | ```
30 |
31 | In your java code you should create a PagerAdapter for your ViewPager. After initialization, make sure you attach the ViewPager to PagerTabsIndicator component.
32 |
33 | ```java
34 | viewPager = findViewById(R.id.view_pager);
35 | tabsIndicator = findViewById(R.id.tabs_indicator);
36 |
37 | viewTextAdapter = new TextAdapter(getSupportFragmentManager());
38 |
39 | viewPager.setAdapter(viewTextAdapter);
40 | tabsIndicator.setViewPager(viewPager);
41 | ```
42 |
43 |
44 | #### Supported Params
45 |
46 | * `tab_padding` - set the left/right padding of a tab
47 | * `tab_text_color` - set text color for default PagerAdapter implementation
48 | * `tab_text_size` - set text size
49 | * `tab_show_divider` - display tabs divider
50 | * `tab_lock_expanded` - make tabs expand to their container size in equal sizes
51 | * `tab_indicator` - specify tab indicator type: `top_bar`,`bottom_bar`,`background`
52 | * `tab_indicator_resource` - use this param to specify a drawable resource that shold be used to draw the indicator if you want something more complext than a simple color. You can also specify an image as indicator.
53 | * `tab_indicator_scale_type` - this param works in conjuction with `tab_indicator_resource`, and you can use it to specify if the indicator drawable should expand on the all available space, of it it should center inside indicator area to maintain a correct aspect. Possible values: `fitXY`, `centerInside`
54 | * `tab_indicator_height` - define indicator height
55 | * `tab_indicator_bg_height` - define indicator bar height
56 | * `tab_indicator_margin` - indicator margin. with this param you can make the indicator float above indicator bar
57 | * `tab_indicator_color` - indicator color. mutual exclusive with `tab_indicator_resource`
58 | * `tab_indicator_bg_color` - indicator bar color
59 | * `tab_divider_width` - divider width
60 | * `tab_divider_margin` - divider vertical margin
61 | * `tab_divider_color`
62 | * `tab_divider_resource` - used to specify a drawable resource that should be used to be drawn as a divider
63 | * `tab_show_bar_indicator` - param to show/hide the indicator bar. default set tot true
64 | * `tab_elevation` - set tabElevation to the tab bar. In case of bottom bar placement, you should use a higher value to make the shadow more visible.
65 | * `tab_disable_animation` - disable indicator animation while switching tabs
66 | * `tab_highlight_text` - highlight the text color of the current tab for basic PagerAdapters. Defaults to indicator color.
67 | * `tab_highlight_text_color` - set a custom color for highlighted tab text.
68 |
69 | #### Changing params at runtime
70 |
71 | In order to change the TabsIndicator parameters at run-time, you can call setters for all supported paramteres. To make things easier, setters can be chained. After setting the properties, it's mandatory to call `refresh()` in order to apply changes.
72 |
73 | ```java
74 | tabsIndicator.setShowDivider(false)
75 | .setIndicatorBgHeight(0)
76 | .setShowBarIndicator(false)
77 | .setHeight(getResources().getDimensionPixelSize(R.dimen.tab_height_min))
78 | .refresh();
79 |
80 | ```
81 |
82 | #### TabViewProvider
83 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hold1/pagertabsdemo/JetpackNavigationActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsdemo
2 |
3 | import android.net.Uri
4 | import android.os.Bundle
5 | import android.view.View
6 | import android.widget.ImageView
7 | import android.widget.TextView
8 | import androidx.appcompat.app.AppCompatActivity
9 | import androidx.navigation.NavController
10 | import androidx.navigation.findNavController
11 | import com.hold1.pagertabsdemo.databinding.ActivityNavigationBinding
12 | import com.hold1.pagertabsindicator.TabInfo
13 | import com.hold1.pagertabsindicator.PagerTabsIndicator
14 | import com.hold1.pagertabsindicator.TabViewProvider
15 | import com.hold1.pagertabsindicator.adapters.TabsAdapter
16 |
17 | class JetpackNavigationActivity : AppCompatActivity() {
18 |
19 | lateinit var binding: ActivityNavigationBinding
20 | lateinit var navController: NavController
21 |
22 | val tabInfos = listOf(
23 | TabInfo(R.id.tabsFragment,"Tabs", null, R.drawable.ic_tabs),
24 | TabInfo(R.id.dividerFragment,"Divider", null, R.drawable.ic_divider),
25 | TabInfo(R.id.colorsFragment,"Colors", null, R.drawable.ic_colors),
26 | TabInfo(R.id.adaptersFragment,"Adapters", null, R.drawable.ic_adapter),
27 | TabInfo(R.id.contactFragment,"Contact", null, R.drawable.ic_contact),
28 | )
29 |
30 | override fun onCreate(savedInstanceState: Bundle?) {
31 | super.onCreate(savedInstanceState)
32 | binding = ActivityNavigationBinding.inflate(layoutInflater)
33 | setContentView(binding.root)
34 | navController = findNavController(R.id.nav_host)
35 |
36 | tabsAdapter.tabViewProvider = viewProvider
37 | binding.tabsIndicator.setAdapter(tabsAdapter)
38 | binding.tabsIndicator.onItemSelectedListener = object: PagerTabsIndicator.OnItemSelectedListener {
39 | override fun onItemSelected(position: Int) {
40 | navController.navigate(tabInfos[position].destinationId)
41 | }
42 | }
43 | navController.addOnDestinationChangedListener { controller, destination, arguments ->
44 | tabInfos.forEachIndexed { index, info ->
45 | if (info.destinationId == destination.id) {
46 | binding.tabsIndicator.setTabSelected(index)
47 | }
48 | }
49 | }
50 | }
51 |
52 | val tabsAdapter = object : TabsAdapter() {
53 | override fun getCount(): Int {
54 | return 5
55 | }
56 | }
57 |
58 | private val imageProvider = object : TabViewProvider.ImageResource {
59 | override fun getTabIconUri(position: Int): Uri? {
60 | return null
61 | }
62 |
63 | override fun getTabIconResId(position: Int): Int {
64 | return tabInfos[position].imageResource ?: R.drawable.ic_add_shopping_cart_black_24dp
65 | }
66 | }
67 |
68 | private val webImageProvider = object : TabViewProvider.ImageResource {
69 | override fun getTabIconUri(position: Int): Uri? {
70 | return Uri.parse(
71 | tabInfos[position].imageUrl
72 | ?: "https://icons8.github.io/flat-color-icons/svg/opened_folder.svg"
73 | )
74 | }
75 |
76 | override fun getTabIconResId(position: Int): Int {
77 | return -1
78 | }
79 | }
80 |
81 |
82 | private val textProvider = object : TabViewProvider.TextResource {
83 |
84 | override fun getTabTitle(position: Int): String {
85 | return tabInfos[position].name ?: "Tab $position"
86 | }
87 | }
88 |
89 |
90 | private val viewProvider = object : TabViewProvider.ViewResource {
91 | override fun getTabView(position: Int): View {
92 | val tabInfo = tabInfos[position]
93 | val view = layoutInflater.inflate(R.layout.tab_item, null)
94 |
95 | val title = view.findViewById(R.id.tab_name)
96 | title.text = tabInfo.name
97 |
98 | val icon = view.findViewById(R.id.tab_icon)
99 | tabInfo.imageResource?.let { icon.setImageResource(it) }
100 | return view
101 | }
102 | }
103 |
104 | private val customAnimationProvider = object : TabViewProvider.ViewResource {
105 |
106 | override fun getTabView(position: Int): View {
107 | val tabInfo = tabInfos[position]
108 | return TabItemView(
109 | applicationContext,
110 | tabInfo.name,
111 | tabInfo.imageResource ?: -1,
112 | -0xc9c9ca,
113 | -0x10000
114 | )
115 | }
116 | }
117 |
118 | }
--------------------------------------------------------------------------------
/scripts/gradle-publishing.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'maven-publish'
2 | apply plugin: 'signing'
3 |
4 | task androidSourcesJar(type: Jar) {
5 | classifier = 'sources'
6 | from android.sourceSets.main.java.source
7 | }
8 |
9 | artifacts {
10 | archives androidSourcesJar
11 | }
12 |
13 | ext["signing.keyId"] = ''
14 | ext["signing.password"] = ''
15 | ext["signing.secretKeyRingFile"] = ''
16 | ext["ossrhUsername"] = ''
17 | ext["ossrhPassword"] = ''
18 |
19 | File secretPropsFile = project.rootProject.file('local.properties')
20 | if (secretPropsFile.exists()) {
21 | println "Found secret props file, loading props"
22 | Properties p = new Properties()
23 | p.load(new FileInputStream(secretPropsFile))
24 | p.each { name, value ->
25 | println(name)
26 | ext[name] = value
27 | }
28 | } else {
29 | println "No props file, loading env vars"
30 | ext["signing.keyId"] = System.getenv('SIGNING_KEY_ID')
31 | ext["signing.password"] = System.getenv('SIGNING_PASSWORD')
32 | ext["signing.secretKeyRingFile"] = System.getenv('SIGNING_SECRET_KEY_RING_FILE')
33 | ext["ossrhUsername"] = System.getenv('OSSRH_USERNAME')
34 | ext["ossrhPassword"] = System.getenv('OSSRH_PASSWORD')
35 | }
36 |
37 | publishing {
38 | publications {
39 | release(MavenPublication) {
40 | // The coordinates of the library, being set from variables that
41 | // we'll set up in a moment
42 | groupId PUBLISH_GROUP_ID
43 | artifactId PUBLISH_ARTIFACT_ID
44 | version PUBLISH_VERSION
45 |
46 | // Two artifacts, the `aar` and the sources
47 | artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
48 | artifact androidSourcesJar
49 |
50 | // Self-explanatory metadata for the most part
51 | pom {
52 | name = PUBLISH_ARTIFACT_ID
53 | description = POM_DESCRIPTION
54 | // If your project has a dedicated site, use its URL here
55 | url = POM_URL
56 | licenses {
57 | license {
58 | name = POM_LICENCE_NAME
59 | url = POM_LICENCE_URL
60 | }
61 | }
62 | developers {
63 | developer {
64 | id = POM_DEVELOPER_ID
65 | name = POM_DEVELOPER_NAME
66 | email = POM_DEVELOPER_EMAIL
67 | url = POM_DEVELOPER_URL
68 | }
69 | }
70 | // Version control info, if you're using GitHub, follow the format as seen here
71 | scm {
72 | connection = POM_SCM_CONNECTION
73 | developerConnection = POM_SCM_DEV_CONNECTION
74 | url = POM_SCM_URL
75 | }
76 | // A slightly hacky fix so that your POM will include any transitive dependencies
77 | // that your library builds upon
78 | withXml {
79 | def dependenciesNode = asNode().appendNode('dependencies')
80 |
81 | project.configurations.implementation.allDependencies.each {
82 | def dependencyNode = dependenciesNode.appendNode('dependency')
83 | dependencyNode.appendNode('groupId', it.group)
84 | dependencyNode.appendNode('artifactId', it.name)
85 | dependencyNode.appendNode('version', it.version)
86 | }
87 | }
88 | }
89 | }
90 | }
91 | repositories {
92 | // The repository to publish to, Sonatype/MavenCentral
93 | maven {
94 | // This is an arbitrary name, you may also use "mavencentral" or
95 | // any other name that's descriptive for you
96 | name = "sonatype"
97 |
98 | def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
99 | def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
100 | // You only need this if you want to publish snapshots, otherwise just set the URL
101 | // to the release repo directly
102 | url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
103 |
104 | // The username and password we've fetched earlier
105 | credentials {
106 | username ossrhUsername
107 | password ossrhPassword
108 | }
109 | }
110 | }
111 | }
112 |
113 | signing {
114 | sign publishing.publications
115 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hold1/pagertabsdemo/fragments/TabsFragment.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsdemo.fragments
2 |
3 | import android.graphics.Color
4 | import android.os.Bundle
5 | import android.view.LayoutInflater
6 | import android.view.View
7 | import android.view.ViewGroup
8 | import android.widget.SeekBar
9 | import android.widget.SeekBar.OnSeekBarChangeListener
10 | import androidx.fragment.app.Fragment
11 | import com.hold1.pagertabsdemo.TabbedActivity
12 | import com.hold1.pagertabsdemo.R
13 | import com.hold1.pagertabsdemo.databinding.TabsFragmentBinding
14 | import com.hold1.pagertabsindicator.PagerTabsIndicator
15 |
16 | /**
17 | * Created by Cristian Holdunu on 09/11/2017.
18 | */
19 | class TabsFragment : Fragment(), FragmentPresenter {
20 | var tabsIndicator: PagerTabsIndicator? = null
21 |
22 | private lateinit var binding: TabsFragmentBinding
23 |
24 | override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
25 | binding = TabsFragmentBinding.inflate(inflater, container, false)
26 | return binding.root
27 | }
28 |
29 | override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
30 | super.onViewCreated(view, savedInstanceState)
31 | tabsIndicator = (activity as? TabbedActivity)?.tabsIndicator
32 | binding.textSize.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
33 | override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
34 | tabsIndicator?.textSize = progress
35 | }
36 |
37 | override fun onStartTrackingTouch(seekBar: SeekBar) {}
38 | override fun onStopTrackingTouch(seekBar: SeekBar) {}
39 | })
40 | binding.tabPadding.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
41 | override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
42 | tabsIndicator?.tabPadding = progress
43 | }
44 |
45 | override fun onStartTrackingTouch(seekBar: SeekBar) {}
46 | override fun onStopTrackingTouch(seekBar: SeekBar) {}
47 | })
48 | binding.tabElevation.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
49 | override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
50 | tabsIndicator?.tabElevation = progress
51 | }
52 |
53 | override fun onStartTrackingTouch(seekBar: SeekBar) {}
54 | override fun onStopTrackingTouch(seekBar: SeekBar) {}
55 | })
56 | binding.indicatorHeight.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
57 | override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
58 | tabsIndicator?.indicatorHeight = progress
59 | }
60 |
61 | override fun onStartTrackingTouch(seekBar: SeekBar) {}
62 | override fun onStopTrackingTouch(seekBar: SeekBar) {}
63 | })
64 | binding.indicatorBgHeight.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
65 | override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
66 | tabsIndicator?.indicatorBackgroundHeight = progress
67 | }
68 |
69 | override fun onStartTrackingTouch(seekBar: SeekBar) {}
70 | override fun onStopTrackingTouch(seekBar: SeekBar) {}
71 | })
72 | binding.indicatorMargin.setOnSeekBarChangeListener(object : OnSeekBarChangeListener {
73 | override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
74 | tabsIndicator?.indicatorVerticalOffset = progress
75 | }
76 |
77 | override fun onStartTrackingTouch(seekBar: SeekBar) {}
78 | override fun onStopTrackingTouch(seekBar: SeekBar) {}
79 | })
80 | binding.lockExpanded.setOnCheckedChangeListener { _, isChecked -> tabsIndicator?.isLockExpanded = isChecked }
81 | binding.indicatorType.setOnCheckedChangeListener { _, checkedId ->
82 | if (checkedId == R.id.bar_top) {
83 | tabsIndicator?.indicatorType = PagerTabsIndicator.TAB_INDICATOR_TOP
84 | } else {
85 | tabsIndicator?.indicatorType = PagerTabsIndicator.TAB_INDICATOR_BOTTOM
86 | }
87 | }
88 | binding.addDummy.setOnClickListener { (activity as? TabbedActivity)?.addDummyTab() }
89 | binding.showBarIndicator.setOnCheckedChangeListener { _, isChecked -> tabsIndicator?.showBarIndicator = isChecked }
90 | binding.highlightText.setOnCheckedChangeListener { _, isChecked ->
91 | tabsIndicator?.isHighlightText = isChecked
92 | tabsIndicator?.highlightTextColor = Color.RED
93 | }
94 | }
95 |
96 | override val tabName: String
97 | get() = "Tabs"
98 | override val tabImage: Int
99 | get() = R.drawable.ic_tabs
100 | override val tabImageUrl: String
101 | get() = "https://s3-us-west-2.amazonaws.com/anaface-pictures/ic_tab.png"
102 | }
--------------------------------------------------------------------------------
/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/res/layout/divider_fragment.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
18 |
19 |
24 |
25 |
31 |
32 |
37 |
38 |
45 |
46 |
50 |
51 |
58 |
59 |
63 |
64 |
71 |
72 |
73 |
74 |
80 |
81 |
82 |
83 |
84 |
90 |
91 |
96 |
97 |
103 |
104 |
109 |
110 |
117 |
118 |
119 |
125 |
126 |
132 |
133 |
139 |
140 |
146 |
147 |
148 |
149 |
150 |
151 |
152 |
--------------------------------------------------------------------------------
/app/src/main/java/com/hold1/pagertabsdemo/ViewPagerActivity.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsdemo
2 |
3 | import android.net.Uri
4 | import android.os.Bundle
5 | import android.view.Menu
6 | import android.view.MenuItem
7 | import android.view.View
8 | import android.widget.ImageView
9 | import android.widget.TextView
10 | import androidx.appcompat.app.AppCompatActivity
11 | import androidx.fragment.app.Fragment
12 | import androidx.fragment.app.FragmentManager
13 | import androidx.fragment.app.FragmentPagerAdapter
14 | import androidx.viewpager.widget.PagerAdapter
15 | import com.hold1.pagertabsdemo.databinding.ActivityMainBinding
16 | import com.hold1.pagertabsdemo.fragments.*
17 | import com.hold1.pagertabsindicator.PagerTabsIndicator
18 | import com.hold1.pagertabsindicator.TabViewProvider
19 | import com.hold1.pagertabsindicator.TabViewProvider.ViewResource
20 | import com.hold1.pagertabsindicator.adapters.ViewPagerTabsAdapter
21 |
22 | class ViewPagerActivity : AppCompatActivity(), TabbedActivity {
23 | lateinit var binding: ActivityMainBinding
24 |
25 | //Just for easing the demo :)
26 | override val tabsIndicator: PagerTabsIndicator
27 | get() {
28 | return binding.tabsIndicator
29 | }
30 |
31 | private val demoFragments = mutableListOf()
32 | private var viewPagerTabsAdapter: ViewPagerTabsAdapter? = null
33 |
34 | override fun onCreate(savedInstanceState: Bundle?) {
35 | super.onCreate(savedInstanceState)
36 | binding = ActivityMainBinding.inflate(layoutInflater)
37 | setContentView(binding.root)
38 | demoFragments.add(TabsFragment())
39 | demoFragments.add(DividerFragment())
40 | demoFragments.add(ColorsFragment())
41 | demoFragments.add(AdaptersFragment())
42 | demoFragments.add(ContactFragment())
43 |
44 | binding.viewPager.adapter = DemoPagerAdapter(supportFragmentManager)
45 |
46 | viewPagerTabsAdapter = ViewPagerTabsAdapter(binding.viewPager)
47 | viewPagerTabsAdapter?.tabViewProvider = viewProvider
48 |
49 | tabsIndicator.setAdapter(viewPagerTabsAdapter)
50 | tabsIndicator.onItemSelectedListener = object : PagerTabsIndicator.OnItemSelectedListener {
51 | override fun onItemSelected(position: Int) {
52 | binding.viewPager.setCurrentItem(position, true)
53 | }
54 |
55 | }
56 | }
57 |
58 | override fun onCreateOptionsMenu(menu: Menu): Boolean {
59 | val inflater = menuInflater
60 | inflater.inflate(R.menu.menu, menu)
61 | return true
62 | }
63 |
64 | override fun onOptionsItemSelected(item: MenuItem): Boolean {
65 | when (item.itemId) {
66 | R.id.text -> changeTabAdapter(TabbedActivity.TabAdapterType.TEXT)
67 | R.id.image -> changeTabAdapter(TabbedActivity.TabAdapterType.IMAGE)
68 | R.id.web -> changeTabAdapter(TabbedActivity.TabAdapterType.WEB)
69 | R.id.custom -> changeTabAdapter(TabbedActivity.TabAdapterType.CUSTOM)
70 | R.id.custom_anim -> changeTabAdapter(TabbedActivity.TabAdapterType.CUSTOM_ANIM)
71 | }
72 | return true
73 | }
74 |
75 | override fun changeTabAdapter(adapterType: TabbedActivity.TabAdapterType?) {
76 | when (adapterType) {
77 | TabbedActivity.TabAdapterType.TEXT -> viewPagerTabsAdapter?.tabViewProvider = textProvider
78 | TabbedActivity.TabAdapterType.IMAGE -> viewPagerTabsAdapter?.tabViewProvider = imageProvider
79 | TabbedActivity.TabAdapterType.WEB -> viewPagerTabsAdapter?.tabViewProvider = webImageProvider
80 | TabbedActivity.TabAdapterType.CUSTOM -> viewPagerTabsAdapter?.tabViewProvider = viewProvider
81 | TabbedActivity.TabAdapterType.CUSTOM_ANIM -> viewPagerTabsAdapter?.tabViewProvider = customAnimationProvider
82 | }
83 | }
84 |
85 | internal inner class DemoPagerAdapter(fm: FragmentManager?) : FragmentPagerAdapter(fm!!) {
86 | override fun getItem(position: Int): Fragment {
87 | return demoFragments[position]
88 | }
89 |
90 | override fun getCount(): Int {
91 | return demoFragments.size
92 | }
93 |
94 | override fun getPageTitle(position: Int): CharSequence {
95 | return "Tab $position"
96 | }
97 | }
98 |
99 | private val imageProvider = object : TabViewProvider.ImageResource {
100 | override fun getTabIconUri(position: Int): Uri? {
101 | return null
102 | }
103 |
104 | override fun getTabIconResId(position: Int): Int {
105 | return (demoFragments[position] as? FragmentPresenter)?.tabImage
106 | ?: R.drawable.ic_add_shopping_cart_black_24dp
107 | }
108 | }
109 |
110 | private val webImageProvider = object : TabViewProvider.ImageResource {
111 | override fun getTabIconUri(position: Int): Uri? {
112 | val fragment = demoFragments[position]
113 | return Uri.parse(
114 | (fragment as? FragmentPresenter)?.tabImageUrl
115 | ?: "https://icons8.github.io/flat-color-icons/svg/opened_folder.svg"
116 | )
117 | }
118 |
119 | override fun getTabIconResId(position: Int): Int {
120 | val fragment = demoFragments[position]
121 | return if (fragment is FragmentPresenter) {
122 | (fragment as FragmentPresenter).tabImage
123 | } else R.drawable.ic_add_shopping_cart_black_24dp
124 | }
125 | }
126 |
127 |
128 | private val textProvider = object : TabViewProvider.TextResource {
129 |
130 | override fun getTabTitle(position: Int): String {
131 | return (demoFragments[position] as? FragmentPresenter)?.tabName ?: "Tab $position"
132 | }
133 | }
134 |
135 |
136 | private val viewProvider = object : ViewResource {
137 | override fun getTabView(position: Int): View {
138 | val fragment = demoFragments[position]
139 | if (fragment is FragmentPresenter) {
140 | val view = layoutInflater.inflate(R.layout.tab_item, null)
141 | val title = view.findViewById(R.id.tab_name)
142 | title.text = (fragment as FragmentPresenter).tabName
143 | val icon = view.findViewById(R.id.tab_icon)
144 | icon.setImageResource((fragment as FragmentPresenter).tabImage)
145 | return view
146 | }
147 | throw NotImplementedError("Improper implementation of CustomView adapter")
148 | }
149 | }
150 |
151 | private val customAnimationProvider = object : ViewResource {
152 |
153 | override fun getTabView(position: Int): View {
154 | val fragment = demoFragments[position]
155 | return if (fragment is FragmentPresenter) {
156 | TabItemView(
157 | applicationContext,
158 | (fragment as FragmentPresenter).tabName,
159 | (fragment as FragmentPresenter).tabImage,
160 | -0xc9c9ca,
161 | -0x10000
162 | )
163 | } else throw NotImplementedError("Improper implementation of CustomView adapter")
164 | }
165 | }
166 |
167 | override fun addDummyTab() {
168 | demoFragments.add(DemoFragment())
169 | binding.viewPager.adapter?.notifyDataSetChanged()
170 | }
171 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/hold1/pagertabsdemo/ViewPager2Activity.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsdemo
2 |
3 | import android.net.Uri
4 | import android.os.Bundle
5 | import android.view.Menu
6 | import android.view.MenuItem
7 | import android.view.View
8 | import android.widget.ImageView
9 | import android.widget.TextView
10 | import androidx.appcompat.app.AppCompatActivity
11 | import androidx.fragment.app.Fragment
12 | import androidx.viewpager2.adapter.FragmentStateAdapter
13 | import com.hold1.pagertabsdemo.databinding.ActivityViewpager2Binding
14 | import com.hold1.pagertabsdemo.fragments.*
15 | import com.hold1.pagertabsindicator.PagerTabsIndicator
16 | import com.hold1.pagertabsindicator.TabViewProvider
17 | import com.hold1.pagertabsindicator.adapters.ViewPager2TabsAdapter
18 | import com.hold1.pagertabsindicator.adapters.ViewPagerTabsAdapter
19 |
20 | class ViewPager2Activity : AppCompatActivity(), TabbedActivity {
21 |
22 | private lateinit var binding: ActivityViewpager2Binding
23 | private val demoFragments = mutableListOf()
24 | private var viewPagerTabsAdapter: ViewPager2TabsAdapter? = null
25 |
26 | //Just for easing the demo :)
27 | override val tabsIndicator: PagerTabsIndicator
28 | get() {
29 | return binding.tabsIndicator
30 | }
31 |
32 | override fun onCreate(savedInstanceState: Bundle?) {
33 | super.onCreate(savedInstanceState)
34 | binding = ActivityViewpager2Binding.inflate(layoutInflater)
35 | setContentView(binding.root)
36 |
37 | demoFragments.add(TabsFragment())
38 | demoFragments.add(DividerFragment())
39 | demoFragments.add(ColorsFragment())
40 | demoFragments.add(AdaptersFragment())
41 | demoFragments.add(ContactFragment())
42 |
43 | binding.viewPager.adapter = DemoPagerAdapter(this)
44 |
45 | viewPagerTabsAdapter = ViewPager2TabsAdapter(binding.viewPager)
46 | viewPagerTabsAdapter?.tabViewProvider = viewProvider
47 |
48 | binding.tabsIndicator.setAdapter(viewPagerTabsAdapter)
49 | binding.tabsIndicator.onItemSelectedListener = object: PagerTabsIndicator.OnItemSelectedListener {
50 | override fun onItemSelected(position: Int) {
51 | binding.viewPager.setCurrentItem(position, true)
52 | }
53 | }
54 | }
55 |
56 | override fun onCreateOptionsMenu(menu: Menu): Boolean {
57 | val inflater = menuInflater
58 | inflater.inflate(R.menu.menu, menu)
59 | return true
60 | }
61 |
62 | override fun onOptionsItemSelected(item: MenuItem): Boolean {
63 | when (item.itemId) {
64 | R.id.text -> changeTabAdapter(TabbedActivity.TabAdapterType.TEXT)
65 | R.id.image -> changeTabAdapter(TabbedActivity.TabAdapterType.IMAGE)
66 | R.id.web -> changeTabAdapter(TabbedActivity.TabAdapterType.WEB)
67 | R.id.custom -> changeTabAdapter(TabbedActivity.TabAdapterType.CUSTOM)
68 | R.id.custom_anim -> changeTabAdapter(TabbedActivity.TabAdapterType.CUSTOM_ANIM)
69 | }
70 | return true
71 | }
72 |
73 | override fun changeTabAdapter(adapterType: TabbedActivity.TabAdapterType?) {
74 | when (adapterType) {
75 | TabbedActivity.TabAdapterType.TEXT -> viewPagerTabsAdapter?.tabViewProvider = textProvider
76 | TabbedActivity.TabAdapterType.IMAGE -> viewPagerTabsAdapter?.tabViewProvider = imageProvider
77 | TabbedActivity.TabAdapterType.WEB -> viewPagerTabsAdapter?.tabViewProvider = webImageProvider
78 | TabbedActivity.TabAdapterType.CUSTOM -> viewPagerTabsAdapter?.tabViewProvider = viewProvider
79 | TabbedActivity.TabAdapterType.CUSTOM_ANIM -> viewPagerTabsAdapter?.tabViewProvider = customAnimationProvider
80 | }
81 | }
82 |
83 | inner class DemoPagerAdapter(activity: AppCompatActivity) : FragmentStateAdapter(activity), TabViewProvider.TextResource {
84 |
85 | override fun getItemCount(): Int {
86 | return demoFragments.size
87 | }
88 |
89 | override fun createFragment(position: Int): Fragment {
90 | return demoFragments[position]
91 | }
92 |
93 | override fun getTabTitle(position: Int): String {
94 | return (demoFragments[position] as? FragmentPresenter)?.tabName ?: "Tab $position"
95 | }
96 | }
97 |
98 |
99 | private val imageProvider = object : TabViewProvider.ImageResource {
100 | override fun getTabIconUri(position: Int): Uri? {
101 | return null
102 | }
103 |
104 | override fun getTabIconResId(position: Int): Int {
105 | return (demoFragments[position] as? FragmentPresenter)?.tabImage
106 | ?: R.drawable.ic_add_shopping_cart_black_24dp
107 | }
108 | }
109 |
110 | private val webImageProvider = object : TabViewProvider.ImageResource {
111 | override fun getTabIconUri(position: Int): Uri? {
112 | val fragment = demoFragments[position]
113 | return Uri.parse(
114 | (fragment as? FragmentPresenter)?.tabImageUrl
115 | ?: "https://icons8.github.io/flat-color-icons/svg/opened_folder.svg"
116 | )
117 | }
118 |
119 | override fun getTabIconResId(position: Int): Int {
120 | val fragment = demoFragments[position]
121 | return if (fragment is FragmentPresenter) {
122 | (fragment as FragmentPresenter).tabImage
123 | } else R.drawable.ic_add_shopping_cart_black_24dp
124 | }
125 | }
126 |
127 |
128 | private val textProvider = object : TabViewProvider.TextResource {
129 |
130 | override fun getTabTitle(position: Int): String {
131 | return (demoFragments[position] as? FragmentPresenter)?.tabName ?: "Tab $position"
132 | }
133 | }
134 |
135 |
136 | private val viewProvider = object : TabViewProvider.ViewResource {
137 | override fun getTabView(position: Int): View {
138 | val fragment = demoFragments[position]
139 | if (fragment is FragmentPresenter) {
140 | val view = layoutInflater.inflate(R.layout.tab_item, null)
141 | val title = view.findViewById(R.id.tab_name)
142 | title.text = (fragment as FragmentPresenter).tabName
143 | val icon = view.findViewById(R.id.tab_icon)
144 | icon.setImageResource((fragment as FragmentPresenter).tabImage)
145 | return view
146 | }
147 | throw NotImplementedError("Improper implementation of CustomView adapter")
148 | }
149 | }
150 |
151 | private val customAnimationProvider = object : TabViewProvider.ViewResource {
152 |
153 | override fun getTabView(position: Int): View {
154 | val fragment = demoFragments[position]
155 | return if (fragment is FragmentPresenter) {
156 | TabItemView(
157 | applicationContext,
158 | (fragment as FragmentPresenter).tabName,
159 | (fragment as FragmentPresenter).tabImage,
160 | -0xc9c9ca,
161 | -0x10000
162 | )
163 | } else throw NotImplementedError("Improper implementation of CustomView adapter")
164 | }
165 | }
166 |
167 | override fun addDummyTab() {
168 | demoFragments.add(DemoFragment())
169 | binding.viewPager.adapter?.notifyDataSetChanged()
170 | }
171 | }
172 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/adapters_fragment.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
18 |
19 |
24 |
25 |
31 |
32 |
37 |
38 |
45 |
46 |
50 |
51 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
71 |
72 |
77 |
78 |
84 |
85 |
90 |
91 |
98 |
99 |
103 |
104 |
105 |
113 |
114 |
119 |
120 |
128 |
129 |
130 |
131 |
132 |
133 |
139 |
140 |
145 |
146 |
152 |
153 |
158 |
159 |
166 |
167 |
171 |
172 |
173 |
181 |
182 |
187 |
188 |
195 |
196 |
197 |
198 |
199 |
200 |
201 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/tabs_fragment.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
11 |
12 |
18 |
19 |
24 |
25 |
31 |
32 |
37 |
38 |
45 |
46 |
50 |
51 |
58 |
62 |
63 |
70 |
71 |
75 |
76 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
95 |
96 |
101 |
102 |
108 |
109 |
114 |
115 |
122 |
123 |
127 |
128 |
135 |
136 |
141 |
142 |
149 |
150 |
155 |
156 |
163 |
164 |
165 |
166 |
167 |
168 |
174 |
175 |
180 |
181 |
187 |
188 |
193 |
194 |
201 |
202 |
208 |
209 |
216 |
221 |
222 |
226 |
227 |
232 |
233 |
239 |
240 |
241 |
247 |
248 |
249 |
255 |
256 |
257 |
258 |
259 |
260 |
261 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/pagertabsindicator/src/main/java/com/hold1/pagertabsindicator/PagerTabsIndicator.kt:
--------------------------------------------------------------------------------
1 | package com.hold1.pagertabsindicator
2 |
3 | import android.animation.ValueAnimator
4 | import android.annotation.SuppressLint
5 | import android.content.Context
6 | import android.graphics.*
7 | import android.graphics.drawable.Drawable
8 | import android.util.AttributeSet
9 | import android.util.Log
10 | import android.view.MotionEvent
11 | import android.view.View
12 | import android.view.ViewGroup
13 | import android.view.animation.LinearInterpolator
14 | import android.widget.*
15 | import androidx.annotation.RestrictTo
16 | import androidx.core.graphics.toRectF
17 | import androidx.core.view.ViewCompat
18 | import androidx.viewpager.widget.ViewPager
19 | import com.hold1.pagertabsindicator.adapters.TabsAdapter
20 | import kotlin.math.max
21 | import kotlin.math.roundToInt
22 |
23 | /**
24 | * Created by Cristian Holdunu on 08/11/2017.
25 | */
26 |
27 | class PagerTabsIndicator @JvmOverloads constructor(
28 | context: Context, attrs: AttributeSet? = null
29 | ) : HorizontalScrollView(context, attrs) {
30 | private var tabsContainer: LinearLayout = LinearLayout(context)
31 |
32 | // Callbacks
33 | interface OnItemSelectedListener {
34 | fun onItemSelected(position: Int)
35 | }
36 |
37 | interface OnItemReselectedListener {
38 | fun onItemReselected(position: Int)
39 | }
40 |
41 | var onItemSelectedListener: OnItemSelectedListener? = null
42 | var onItemReselectedListener: OnItemReselectedListener? = null
43 |
44 |
45 | private var adapter: TabsAdapter? = null
46 |
47 |
48 | // Indicator Properties
49 | var showBarIndicator = true
50 | set(value) {
51 | field = value
52 | invalidate()
53 | }
54 | var indicatorType = TAB_INDICATOR_BOTTOM
55 | set(value) {
56 | field = value
57 | requestLayout()
58 | }
59 | var indicatorHeight = resources.getDimensionPixelSize(R.dimen.tab_default_indicator_height)
60 | set(value) {
61 | field = value
62 | invalidate()
63 | }
64 | var indicatorBackgroundHeight =
65 | resources.getDimensionPixelSize(R.dimen.tab_default_indicator_bg_height)
66 | set(value) {
67 | field = value
68 | invalidate()
69 | }
70 | var indicatorWidth: Int = -1
71 | set(value) {
72 | field = value
73 | invalidate()
74 | }
75 | var indicatorWidthPercent: Float = 0.0F
76 | set(value) {
77 | field = value
78 | invalidate()
79 | }
80 | var indicatorVerticalOffset = 0
81 | set(value) {
82 | field = value
83 | invalidate()
84 | }
85 | var indicatorCornerRadius: Float = 0.0f
86 | set(value) {
87 | field = value
88 | invalidate()
89 | }
90 | var indicatorColor = resources.getColor(R.color.tab_indicator_color)
91 | set(value) {
92 | field = value
93 | tintPaint.color = value
94 | invalidate()
95 | }
96 | var indicatorBgColor = resources.getColor(R.color.tab_indicator_bg_color)
97 | set(value) {
98 | field = value
99 | backgroundPaint.color = value
100 | invalidate()
101 | }
102 | var indicatorDrawable: Drawable? = null
103 | set(value) {
104 | field = value
105 | invalidate()
106 | }
107 | var indicatorResource = -1
108 | @SuppressLint("UseCompatLoadingForDrawables")
109 | set(value) {
110 | field = value
111 | indicatorDrawable = if (value == -1) {
112 | null
113 | } else {
114 | resources.getDrawable(value)
115 | }
116 | }
117 | var indicatorScaleType = SCALE_CENTER_INSIDE
118 | set(value) {
119 | field = value
120 | invalidate()
121 | }
122 |
123 | // Painting
124 | private var backgroundPaint: Paint = Paint().apply {
125 | this.color = indicatorBgColor
126 | this.style = Paint.Style.FILL
127 | this.isAntiAlias = true
128 | }
129 | private var tintPaint: Paint = Paint().apply {
130 | this.color = indicatorColor
131 | this.style = Paint.Style.FILL
132 | this.isAntiAlias = true
133 | }
134 | private var dividerPaint: Paint = Paint().apply {
135 | this.color = dividerColor
136 | this.style = Paint.Style.FILL
137 | this.isAntiAlias = true
138 | }
139 |
140 | //Divider resources
141 | var isShowDivider = true
142 | set(value) {
143 | field = value
144 | invalidate()
145 | }
146 | var dividerWidth = resources.getDimensionPixelSize(R.dimen.tab_default_divider_width)
147 | set(value) {
148 | field = value
149 | invalidate()
150 | }
151 | var dividerMargin = resources.getDimensionPixelSize(R.dimen.tab_default_divider_margin)
152 | set(value) {
153 | field = value
154 | invalidate()
155 | }
156 | var tabElevation = 0
157 | set(value) {
158 | field = value
159 | ViewCompat.setElevation(this, value.toFloat())
160 | }
161 | var dividerColor = resources.getColor(R.color.tab_default_divider_color)
162 | set(value) {
163 | field = value
164 | dividerPaint.color = value
165 | invalidate()
166 | }
167 | var dividerResource = -1
168 | @SuppressLint("UseCompatLoadingForDrawables")
169 | set(value) {
170 | field = value
171 | dividerDrawable = if (value == -1) {
172 | null
173 | } else {
174 | resources.getDrawable(value)
175 | }
176 | }
177 | var dividerDrawable: Drawable? = null
178 | set(value) {
179 | field = value
180 | invalidate()
181 | }
182 |
183 | // Tab properties
184 | var isHighlightText = false
185 | set(value) {
186 | field = value
187 | invalidate()
188 | }
189 | var textSize = resources.getDimensionPixelSize(R.dimen.tab_default_text_size)
190 | set(value) {
191 | field = value
192 | invalidateViews()
193 | }
194 | var textColor = resources.getColor(R.color.tab_default_text_color)
195 | set(value) {
196 | field = value
197 | invalidateViews()
198 | }
199 | var highlightTextColor = resources.getColor(R.color.tab_indicator_color)
200 | set(value) {
201 | field = value
202 | invalidateViews()
203 | }
204 | var tabPadding = resources.getDimensionPixelSize(R.dimen.tab_default_padding)
205 | set(value) {
206 | field = value
207 | invalidate()
208 | }
209 |
210 | // Generic properties
211 | var isLockExpanded = false
212 | set(value) {
213 | field = value
214 | requestLayout()
215 | }
216 | var isDisableTabAnimation = false
217 | set(value) {
218 | field = value
219 | invalidate()
220 | }
221 |
222 | private var position = -1
223 | private var targetPosition = -1
224 | private var positionOffset = 0f
225 | private var lastScrollX = 0
226 | private var tabWidth = ViewGroup.LayoutParams.WRAP_CONTENT
227 | private var runnable: Runnable? = null
228 | private val indicatorBackgroundRect = RectF()
229 | private val indicatorRect = Rect()
230 | private val valueAnimator = ValueAnimator.ofInt().apply {
231 | this.interpolator = LinearInterpolator()
232 | this.duration = 200
233 | }
234 |
235 | init {
236 | isHorizontalScrollBarEnabled = false
237 | addView(tabsContainer, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)
238 |
239 | val typedArray = context.obtainStyledAttributes(attrs, R.styleable.PagerTabsIndicator)
240 | tabPadding =
241 | typedArray.getDimensionPixelSize(R.styleable.PagerTabsIndicator_tab_padding, tabPadding)
242 | textSize =
243 | typedArray.getDimensionPixelSize(R.styleable.PagerTabsIndicator_tab_text_size, textSize)
244 | textColor = typedArray.getColor(R.styleable.PagerTabsIndicator_tab_text_color, textColor)
245 | highlightTextColor = typedArray.getColor(
246 | R.styleable.PagerTabsIndicator_tab_highlight_text_color,
247 | highlightTextColor
248 | )
249 | isHighlightText = typedArray.getBoolean(
250 | R.styleable.PagerTabsIndicator_tab_highlight_text_color,
251 | isHighlightText
252 | )
253 | isShowDivider =
254 | typedArray.getBoolean(R.styleable.PagerTabsIndicator_tab_show_divider, isShowDivider)
255 | isLockExpanded =
256 | typedArray.getBoolean(R.styleable.PagerTabsIndicator_tab_lock_expanded, isLockExpanded)
257 | indicatorType =
258 | typedArray.getInt(R.styleable.PagerTabsIndicator_tab_indicator, indicatorType)
259 | indicatorResource = typedArray.getResourceId(
260 | R.styleable.PagerTabsIndicator_tab_indicator_resource,
261 | indicatorResource
262 | )
263 | indicatorHeight = typedArray.getDimensionPixelSize(
264 | R.styleable.PagerTabsIndicator_tab_indicator_height,
265 | indicatorHeight
266 | )
267 | indicatorBackgroundHeight = typedArray.getDimensionPixelSize(
268 | R.styleable.PagerTabsIndicator_tab_indicator_background_height,
269 | indicatorBackgroundHeight
270 | )
271 | indicatorVerticalOffset = typedArray.getDimensionPixelSize(
272 | R.styleable.PagerTabsIndicator_tab_indicator_vertical_offset,
273 | indicatorVerticalOffset
274 | )
275 | indicatorColor =
276 | typedArray.getColor(R.styleable.PagerTabsIndicator_tab_indicator_color, indicatorColor)
277 | indicatorBgColor = typedArray.getColor(
278 | R.styleable.PagerTabsIndicator_tab_indicator_background_color,
279 | indicatorBgColor
280 | )
281 | dividerWidth = typedArray.getDimensionPixelSize(
282 | R.styleable.PagerTabsIndicator_tab_divider_width,
283 | dividerWidth
284 | )
285 | dividerMargin = typedArray.getDimensionPixelSize(
286 | R.styleable.PagerTabsIndicator_tab_divider_margin,
287 | dividerMargin
288 | )
289 | dividerColor =
290 | typedArray.getColor(R.styleable.PagerTabsIndicator_tab_divider_color, dividerColor)
291 | dividerResource = typedArray.getResourceId(
292 | R.styleable.PagerTabsIndicator_tab_divider_resource,
293 | dividerResource
294 | )
295 | showBarIndicator = typedArray.getBoolean(
296 | R.styleable.PagerTabsIndicator_tab_show_bar_indicator,
297 | showBarIndicator
298 | )
299 | tabElevation = typedArray.getDimensionPixelSize(
300 | R.styleable.PagerTabsIndicator_tab_elevation,
301 | tabElevation
302 | )
303 | indicatorScaleType = typedArray.getInt(
304 | R.styleable.PagerTabsIndicator_tab_indicator_scale_type,
305 | indicatorScaleType
306 | )
307 | isDisableTabAnimation = typedArray.getBoolean(
308 | R.styleable.PagerTabsIndicator_tab_disable_animation,
309 | isDisableTabAnimation
310 | )
311 | indicatorCornerRadius = typedArray.getDimensionPixelSize(
312 | R.styleable.PagerTabsIndicator_tab_indicator_corner_radius,
313 | 0
314 | ).toFloat()
315 | indicatorWidth = typedArray.getDimensionPixelSize(
316 | R.styleable.PagerTabsIndicator_tab_indicator_width,
317 | indicatorWidth
318 | )
319 | indicatorWidthPercent = typedArray.getFloat(
320 | R.styleable.PagerTabsIndicator_tab_indicator_width_percent,
321 | indicatorWidthPercent
322 | )
323 | typedArray.recycle()
324 |
325 | valueAnimator.addUpdateListener {
326 | setIndicatorBounds(it.animatedValue as Int)
327 | invalidate()
328 | }
329 | }
330 |
331 | override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
332 | return !isLockExpanded && super.onInterceptTouchEvent(ev)
333 | }
334 |
335 | override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
336 | super.onLayout(changed, l, t, r, b)
337 | Log.d(TAG, "layout width=" + (r - l))
338 | getTabAt(position)?.let { setIndicatorBounds(it.getCenterX()) }
339 | if (!changed) return
340 | val newWidth = r - l
341 | val childCount = tabsContainer.childCount
342 | if (isLockExpanded && childCount > 0) {
343 | val newTabWidth = newWidth / childCount
344 | if (newTabWidth == 0) return
345 | tabWidth = newTabWidth
346 | Log.d(TAG, "newWidth=$newWidth tabWidth=$tabWidth")
347 | for (i in 0 until tabsContainer.childCount) {
348 | val view = tabsContainer.getChildAt(i)
349 | view.layoutParams.width = tabWidth
350 | }
351 | tabsContainer.layoutParams = tabsContainer.layoutParams
352 | tabsContainer.requestLayout()
353 | }
354 | }
355 |
356 | override fun measureChildWithMargins(
357 | child: View,
358 | parentWidthMeasureSpec: Int,
359 | widthUsed: Int,
360 | parentHeightMeasureSpec: Int,
361 | heightUsed: Int
362 | ) {
363 | val lp = child.layoutParams as MarginLayoutParams
364 | if (showBarIndicator) when (indicatorType) {
365 | TAB_INDICATOR_TOP -> {
366 | lp.topMargin = indicatorBackgroundHeight
367 | lp.bottomMargin = 0
368 | }
369 | TAB_INDICATOR_BOTTOM -> {
370 | lp.bottomMargin = indicatorBackgroundHeight
371 | lp.topMargin = 0
372 | }
373 | } else {
374 | lp.topMargin = paddingTop
375 | lp.bottomMargin = paddingBottom
376 | }
377 | super.measureChildWithMargins(
378 | child,
379 | parentWidthMeasureSpec,
380 | widthUsed,
381 | parentHeightMeasureSpec,
382 | heightUsed
383 | )
384 | }
385 |
386 | @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
387 | fun invalidateViews() {
388 | tabsContainer.removeAllViews()
389 | adapter?.let { tabsAdapter ->
390 | for (i in 0 until tabsAdapter.getCount()) {
391 | addTabView(tabsAdapter.getTabAt(i), i)
392 | }
393 | }
394 | }
395 |
396 | fun setAdapter(adapter: TabsAdapter?) {
397 | this.adapter = adapter
398 | this.adapter?.pagerTabsIndicator = this
399 | }
400 |
401 | private fun addTabView(view: View?, position: Int) {
402 | tabsContainer.addView(
403 | view,
404 | LinearLayout.LayoutParams(tabWidth, ViewGroup.LayoutParams.MATCH_PARENT)
405 | )
406 | view?.setPadding(tabPadding, 0, tabPadding, 0)
407 | view?.setOnClickListener {
408 | Log.d(TAG, "tab click $position")
409 | if (this.position == position) {
410 | onItemReselectedListener?.onItemReselected(position)
411 | } else {
412 | onItemSelectedListener?.onItemSelected(position)
413 | }
414 | targetPosition = position
415 | }
416 | }
417 |
418 | override fun onDraw(canvas: Canvas) {
419 | super.onDraw(canvas)
420 | drawIndicatorBackground(canvas)
421 | drawIndicator(canvas)
422 | drawDivider(canvas)
423 | }
424 |
425 | private fun drawIndicatorBackground(canvas: Canvas) {
426 | indicatorBackgroundRect.left = 0f
427 | indicatorBackgroundRect.right = max(right, tabsContainer.width).toFloat()
428 |
429 | when (indicatorType) {
430 | TAB_INDICATOR_TOP -> {
431 | indicatorBackgroundRect.top = 0f
432 | indicatorBackgroundRect.bottom = indicatorBackgroundHeight.toFloat()
433 | }
434 | TAB_INDICATOR_BOTTOM -> {
435 | indicatorBackgroundRect.top = (height - indicatorBackgroundHeight).toFloat()
436 | indicatorBackgroundRect.bottom = height.toFloat()
437 | }
438 | else -> {
439 | indicatorBackgroundRect.top = (height - indicatorBackgroundHeight).toFloat()
440 | indicatorBackgroundRect.bottom = height.toFloat()
441 | }
442 | }
443 | canvas.drawRoundRect(
444 | indicatorBackgroundRect,
445 | indicatorCornerRadius,
446 | indicatorCornerRadius,
447 | backgroundPaint
448 | )
449 | }
450 |
451 | private fun drawIndicator(canvas: Canvas) {
452 | if (!showBarIndicator) return
453 |
454 | //TODO: move to layout part
455 | when (indicatorType) {
456 | TAB_INDICATOR_TOP -> {
457 | indicatorRect.top = indicatorVerticalOffset
458 | indicatorRect.bottom = indicatorHeight + indicatorVerticalOffset
459 | }
460 | TAB_INDICATOR_BOTTOM -> {
461 | indicatorRect.top = height - indicatorHeight - indicatorVerticalOffset
462 | indicatorRect.bottom = height - indicatorVerticalOffset
463 | }
464 | else -> {
465 | indicatorRect.top = height - indicatorHeight - indicatorVerticalOffset
466 | indicatorRect.bottom = height - indicatorVerticalOffset
467 | }
468 | }
469 |
470 | indicatorDrawable?.let { drawable ->
471 | if (indicatorScaleType == SCALE_CENTER_INSIDE) {
472 | //Adjust the indicator bounds aspect ratio to keep the indicator resource intact
473 | val ratio =
474 | (drawable.intrinsicHeight / drawable.intrinsicWidth).toFloat()
475 | val scaledWidth = indicatorHeight * ratio
476 | indicatorRect.left = (indicatorRect.centerX() - scaledWidth / 2).toInt()
477 | indicatorRect.right = (indicatorRect.left + scaledWidth).toInt()
478 | }
479 | drawable.bounds = indicatorRect
480 | drawable.draw(canvas)
481 | } ?: run {
482 | canvas.drawRoundRect(
483 | indicatorRect.toRectF(),
484 | indicatorCornerRadius,
485 | indicatorCornerRadius,
486 | tintPaint
487 | )
488 | }
489 | }
490 |
491 | private fun drawDivider(canvas: Canvas) {
492 | if (!isShowDivider) return
493 | for (i in 0 until tabsContainer.childCount - 1) {
494 | val tab = tabsContainer.getChildAt(i)
495 | val startX = tab.right - dividerWidth / 2
496 | val endX = startX + dividerWidth
497 | var startY = 0
498 | var endY = height
499 | if (indicatorType == TAB_INDICATOR_BOTTOM) {
500 | startY = dividerMargin
501 | endY = height - indicatorBackgroundHeight - dividerMargin
502 | } else if (indicatorType == TAB_INDICATOR_TOP) {
503 | startY = indicatorBackgroundHeight + dividerMargin
504 | endY = height - dividerMargin
505 | }
506 |
507 | dividerDrawable?.let { drawable ->
508 | drawable.bounds = Rect(startX, startY, endX, endY)
509 | drawable.draw(canvas)
510 | } ?: run {
511 | canvas.drawRect(
512 | startX.toFloat(),
513 | startY.toFloat(),
514 | endX.toFloat(),
515 | endY.toFloat(),
516 | dividerPaint
517 | )
518 | }
519 | }
520 | }
521 |
522 | @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
523 | fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
524 | Log.d(
525 | TAG,
526 | "onPageScrolled:position=" + position + ";positionOffset:" + positionOffset + ";tabsContainer child count:" + tabsContainer.childCount
527 | )
528 | this.position = position
529 | this.positionOffset = positionOffset
530 | if (targetPosition == -1 && tabsContainer.childCount > position) {
531 | scrollToChild(
532 | position,
533 | (positionOffset * tabsContainer.getChildAt(position).width).toInt()
534 | )
535 | }
536 | val targetPosition = (position + positionOffset).roundToInt()
537 | val targetOffset = Math.abs(0.5 - positionOffset).toFloat() * 2
538 | for (i in 0 until tabsContainer.childCount) {
539 | val child = tabsContainer.getChildAt(i) as? TabView ?: continue
540 | if (i == targetPosition) {
541 | child.onOffset(targetOffset)
542 | } else {
543 | child.onOffset(0f)
544 | }
545 | }
546 |
547 | if (isDisableTabAnimation) {
548 | tabsContainer.getChildAt((position + positionOffset).roundToInt())?.let { nextTab ->
549 | setIndicatorBounds(nextTab.getCenterX())
550 |
551 | }
552 | } else {
553 | tabsContainer.getChildAt(position)?.let { currentTab ->
554 | setIndicatorBounds(currentTab.getCenterX())
555 | }
556 | adapter?.let {
557 | if (positionOffset > 0f && position < it.getCount() - 1 && !isDisableTabAnimation) {
558 | val nextTab = tabsContainer.getChildAt(position + 1)
559 | setIndicatorBounds((positionOffset * nextTab.getCenterX() + (1f - positionOffset) * indicatorRect.centerX()).toInt())
560 | }
561 | }
562 | }
563 | invalidate()
564 | }
565 |
566 | @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
567 | fun onPageSelected(position: Int) {
568 | Log.d(TAG, "onPageSelected=$position")
569 | }
570 |
571 | @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
572 | fun onPageScrollStateChanged(state: Int) {
573 | Log.d(TAG, "onPageScrollStateChanged $state")
574 | if (state == ViewPager.SCROLL_STATE_IDLE) {
575 | if (targetPosition != -1) setTabSelected(targetPosition, false)
576 | targetPosition = -1
577 | }
578 | }
579 |
580 | fun setTabSelected(position: Int, animated: Boolean = true) {
581 | Log.d(TAG, "setTabSelected $position")
582 | var startX = -1
583 | var endX = -1
584 | getTabAt(this.position)?.let {
585 | startX = it.getCenterX()
586 | }
587 | getTabAt(position)?.let {
588 | endX = it.getCenterX()
589 | }
590 | this.position = position
591 | val tabCount = tabsContainer.childCount
592 | for (i in 0 until tabCount) {
593 | val child = tabsContainer.getChildAt(i)
594 | val isSelected = i == position
595 | if (indicatorType == TAB_INDICATOR_BACKGROUND) child.isSelected = isSelected
596 | if (isSelected) {
597 | animateToTab(position)
598 | }
599 | }
600 | if (animated && startX != -1 && endX != -1) {
601 | valueAnimator.cancel()
602 | valueAnimator.setIntValues(indicatorRect.centerX(), endX)
603 | valueAnimator.start()
604 | } else {
605 | setIndicatorBounds(endX)
606 | }
607 | }
608 |
609 | private fun setIndicatorBounds(withCenter: Int) {
610 | Log.d(TAG, "setIndicatorBounds center=$withCenter")
611 | val drawWidth = when {
612 | indicatorWidthPercent > 0 -> (tabWidth * indicatorWidthPercent).toInt()
613 | indicatorWidth > -1 -> indicatorWidth
614 | else -> tabWidth
615 | }
616 |
617 | indicatorRect.left = (withCenter - drawWidth / 2)
618 | indicatorRect.right = indicatorRect.left + drawWidth
619 | }
620 |
621 | private fun animateToTab(position: Int) {
622 | val tabView = tabsContainer.getChildAt(position)
623 | if (runnable != null) {
624 | removeCallbacks(runnable)
625 | }
626 | runnable = Runnable {
627 | val scrollPos = tabView.left - (width - tabView.width) / 2
628 | smoothScrollTo(scrollPos, 0)
629 | runnable = null
630 | }
631 | post(runnable)
632 | }
633 |
634 | private fun scrollToChild(position: Int, offset: Int) {
635 | val tabView = tabsContainer.getChildAt(position)
636 | val newScrollX = tabView.left - (width - tabView.width) / 2 + offset
637 | if (newScrollX != lastScrollX) {
638 | lastScrollX = newScrollX
639 | scrollTo(newScrollX, 0)
640 | }
641 | }
642 |
643 | fun setHeight(height: Int): PagerTabsIndicator {
644 | layoutParams.height = height
645 | requestLayout()
646 | return this
647 | }
648 |
649 | fun getTabAt(position: Int): View? {
650 | return if (position < tabsContainer.childCount) {
651 | tabsContainer.getChildAt(position)
652 | } else null
653 | }
654 |
655 | companion object {
656 | //CONSTANTS
657 | private val TAG = PagerTabsIndicator::class.java.simpleName
658 | const val TAB_INDICATOR_TOP = 0
659 | const val TAB_INDICATOR_BOTTOM = 1
660 | const val TAB_INDICATOR_BACKGROUND = 2
661 | const val SCALE_FIT_XY = 0
662 | const val SCALE_CENTER_INSIDE = 1
663 | }
664 | }
665 |
666 | fun View.getCenterX(): Int {
667 | return right - measuredWidth / 2
668 | }
--------------------------------------------------------------------------------