From dca9ddd09de8ff7c2d5960163b9dbef3744ee214 Mon Sep 17 00:00:00 2001 From: Ertugrul Uzun Date: Thu, 4 Aug 2022 12:12:40 +0300 Subject: [PATCH] viewpager2 support --- .idea/misc.xml | 16 +++- .idea/modules.xml | 13 ---- .idea/runConfigurations.xml | 12 --- README.md | 2 +- app/build.gradle | 22 +++--- .../ExampleInstrumentedTest.kt | 26 ------- app/src/main/AndroidManifest.xml | 3 +- build.gradle | 4 +- gradle.properties | 2 + gradle/wrapper/gradle-wrapper.properties | 2 +- library/build.gradle | 14 ++-- .../iammert/library/AnimatedTabItemView.kt | 2 +- .../com/iammert/library/AnimatedTabLayout.kt | 75 ++++++++++++++----- 13 files changed, 97 insertions(+), 96 deletions(-) delete mode 100644 .idea/modules.xml delete mode 100644 .idea/runConfigurations.xml delete mode 100644 app/src/androidTest/java/com/iammert/animatedtablayout/ExampleInstrumentedTest.kt diff --git a/.idea/misc.xml b/.idea/misc.xml index b9be38f..7fc2286 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -1,11 +1,19 @@ + + + + - + diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 231086a..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml deleted file mode 100644 index 7f68460..0000000 --- a/.idea/runConfigurations.xml +++ /dev/null @@ -1,12 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/README.md b/README.md index ed1a67a..1554307 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Thanks to [Igor](https://dribbble.com/motionigor) for sharing this awesome anima app:atl_tabs="@xml/tabs"/> ``` * Setup with viewpager or listen change on tab click. -```kotlin +```kot lin atl.setupViewPager(viewpager) atl.setTabChangeListener(object : AnimatedTabLayout.OnChangeListener { override fun onChanged(position: Int) { diff --git a/app/build.gradle b/app/build.gradle index eeddd3e..db9c96c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -2,14 +2,14 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' android { - compileSdkVersion 29 + compileSdkVersion 32 defaultConfig { applicationId "com.iammert.animatedtablayout" minSdkVersion 15 - targetSdkVersion 29 + targetSdkVersion 32 versionCode 1 versionName "1.0" - testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" + testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' } buildTypes { release { @@ -21,13 +21,15 @@ android { dependencies { implementation fileTree(include: ['*.jar'], dir: 'libs') - implementation 'androidx.appcompat:appcompat:1.1.0' - implementation 'androidx.core:core-ktx:1.1.0' - implementation 'com.google.android.material:material:1.0.0' - implementation 'com.android.support.constraint:constraint-layout:1.1.3' - testImplementation 'junit:junit:4.12' - androidTestImplementation 'com.android.support.test:runner:1.0.2' - androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' + implementation 'androidx.core:core-ktx:1.8.0' + implementation 'androidx.appcompat:appcompat:1.4.2' + implementation 'com.google.android.material:material:1.6.1' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation 'androidx.legacy:legacy-support-v4:1.0.0' + + testImplementation 'junit:junit:4.13.2' + androidTestImplementation 'androidx.test.ext:junit:1.1.3' + androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" implementation project(':library') } diff --git a/app/src/androidTest/java/com/iammert/animatedtablayout/ExampleInstrumentedTest.kt b/app/src/androidTest/java/com/iammert/animatedtablayout/ExampleInstrumentedTest.kt deleted file mode 100644 index c3bf7a7..0000000 --- a/app/src/androidTest/java/com/iammert/animatedtablayout/ExampleInstrumentedTest.kt +++ /dev/null @@ -1,26 +0,0 @@ -package com.iammert.animatedtablayout - -import android.content.Context -import android.support.test.InstrumentationRegistry -import android.support.test.runner.AndroidJUnit4 - -import org.junit.Test -import org.junit.runner.RunWith - -import org.junit.Assert.* - -/** - * Instrumented test, which will execute on an Android device. - * - * @see [Testing documentation](http://d.android.com/tools/testing) - */ -@RunWith(AndroidJUnit4::class) -class ExampleInstrumentedTest { - @Test - fun useAppContext() { - // Context of the app under test. - val appContext = InstrumentationRegistry.getTargetContext() - - assertEquals("com.iammert.animatedtablayout", appContext.packageName) - } -} diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 63e0afe..0d74930 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,7 +9,8 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> - + diff --git a/build.gradle b/build.gradle index 68b868f..489dbc0 100644 --- a/build.gradle +++ b/build.gradle @@ -1,14 +1,14 @@ // Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { - ext.kotlin_version = '1.2.41' + ext.kotlin_version = '1.7.10' repositories { google() jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:3.1.0' + classpath 'com.android.tools.build:gradle:7.2.1' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong diff --git a/gradle.properties b/gradle.properties index 743d692..8de5058 100644 --- a/gradle.properties +++ b/gradle.properties @@ -6,6 +6,8 @@ # http://www.gradle.org/docs/current/userguide/build_environment.html # Specifies the JVM arguments used for the daemon process. # The setting is particularly useful for tweaking memory settings. +android.enableJetifier=true +android.useAndroidX=true org.gradle.jvmargs=-Xmx1536m # When configured, Gradle will run in incubating parallel mode. # This option should only be used with decoupled projects. More details, visit diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 92facc4..c72fbb7 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip diff --git a/library/build.gradle b/library/build.gradle index 7c8c4cf..dc67779 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -2,15 +2,13 @@ apply plugin: 'com.android.library' apply plugin: 'kotlin-android' android { - compileSdkVersion 29 + compileSdkVersion 32 defaultConfig { minSdkVersion 15 - targetSdkVersion 29 - versionCode 1 - versionName "1.0" + targetSdkVersion 32 } @@ -26,9 +24,11 @@ android { dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) - implementation 'androidx.appcompat:appcompat:1.1.0' - implementation 'androidx.core:core-ktx:1.1.0' - implementation 'com.google.android.material:material:1.0.0' + implementation 'androidx.core:core-ktx:1.8.0' + implementation 'androidx.appcompat:appcompat:1.4.2' + implementation 'com.google.android.material:material:1.6.1' + implementation 'androidx.constraintlayout:constraintlayout:2.1.4' + implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" } repositories { diff --git a/library/src/main/java/com/iammert/library/AnimatedTabItemView.kt b/library/src/main/java/com/iammert/library/AnimatedTabItemView.kt index 60f98a0..e372a5f 100644 --- a/library/src/main/java/com/iammert/library/AnimatedTabItemView.kt +++ b/library/src/main/java/com/iammert/library/AnimatedTabItemView.kt @@ -99,7 +99,7 @@ class AnimatedTabItemView : LinearLayout { override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) { super.onSizeChanged(w, h, oldw, oldh) path?.reset() - path?.addRoundRect(rectF, radius, radius, Path.Direction.CCW) + path?.addRoundRect(rectF!!, radius, radius, Path.Direction.CCW) } fun setFromColor(fromColor: Int) { diff --git a/library/src/main/java/com/iammert/library/AnimatedTabLayout.kt b/library/src/main/java/com/iammert/library/AnimatedTabLayout.kt index 4c9ac3a..6eebd9f 100644 --- a/library/src/main/java/com/iammert/library/AnimatedTabLayout.kt +++ b/library/src/main/java/com/iammert/library/AnimatedTabLayout.kt @@ -2,17 +2,17 @@ package com.iammert.library import android.content.Context import android.os.Build -import android.os.Handler import android.util.AttributeSet import android.view.LayoutInflater import android.widget.FrameLayout import android.widget.LinearLayout import androidx.annotation.RequiresApi import androidx.viewpager.widget.ViewPager +import androidx.viewpager2.widget.ViewPager2 class AnimatedTabLayout : FrameLayout { - interface OnChangeListener{ + interface OnChangeListener { fun onChanged(position: Int) } @@ -22,7 +22,8 @@ class AnimatedTabLayout : FrameLayout { lateinit var selectedTab: AnimatedTabItemContainer - lateinit var viewPager: ViewPager + private var viewPager: ViewPager? = null + private var viewPager2: ViewPager2? = null private var onChangeListener: OnChangeListener? = null @@ -34,18 +35,32 @@ class AnimatedTabLayout : FrameLayout { init(context, attrs, 0, 0) } - constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { + constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super( + context, + attrs, + defStyleAttr + ) { init(context, attrs, defStyleAttr, 0) } @RequiresApi(Build.VERSION_CODES.LOLLIPOP) - constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) { + constructor( + context: Context, + attrs: AttributeSet?, + defStyleAttr: Int, + defStyleRes: Int + ) : super(context, attrs, defStyleAttr, defStyleRes) { init(context, attrs, defStyleAttr, defStyleRes) } private fun init(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) { - val typedArray = context?.theme?.obtainStyledAttributes(attrs, R.styleable.AnimatedTabLayout, defStyleAttr, defStyleRes) + val typedArray = context?.theme?.obtainStyledAttributes( + attrs, + R.styleable.AnimatedTabLayout, + defStyleAttr, + defStyleRes + ) val tabXmlResource = typedArray?.getResourceId(R.styleable.AnimatedTabLayout_atl_tabs, 0) tabs = AnimatedTabResourceParser(context, tabXmlResource!!).parse() @@ -59,35 +74,57 @@ class AnimatedTabLayout : FrameLayout { tab.setOnClickListener { val selectedIndex = tabs.indexOf(tab) onPageChangeListener.onPageSelected(selectedIndex) - viewPager.currentItem = selectedIndex + onPageChangeListenerPager2.onPageSelected(selectedIndex) + viewPager?.currentItem = selectedIndex + viewPager2?.currentItem = selectedIndex } } } - fun setTabChangeListener(onChangeListener: OnChangeListener?){ + fun setTabChangeListener(onChangeListener: OnChangeListener?) { this.onChangeListener = onChangeListener } fun setupViewPager(viewPager: ViewPager) { this.viewPager = viewPager - this.viewPager.addOnPageChangeListener(onPageChangeListener) + this.viewPager?.addOnPageChangeListener(onPageChangeListener) selectedTab = tabs[viewPager.currentItem] selectedTab.expand() } - private var onPageChangeListener: ViewPager.OnPageChangeListener = object : ViewPager.SimpleOnPageChangeListener() { - override fun onPageSelected(position: Int) { - super.onPageSelected(position) - if (tabs[position] == selectedTab) { - return + fun setupViewPager(viewPager: ViewPager2) { + this.viewPager2 = viewPager + this.viewPager2?.registerOnPageChangeCallback(onPageChangeListenerPager2) + selectedTab = tabs[viewPager.currentItem] + selectedTab.expand() + } + + private var onPageChangeListenerPager2: ViewPager2.OnPageChangeCallback = + object : ViewPager2.OnPageChangeCallback() { + override fun onPageSelected(position: Int) { + super.onPageSelected(position) + initSelectedTab(position) + } + } + + private var onPageChangeListener: ViewPager.OnPageChangeListener = + object : ViewPager.SimpleOnPageChangeListener() { + override fun onPageSelected(position: Int) { + super.onPageSelected(position) + initSelectedTab(position) } - selectedTab.collapse() - selectedTab = tabs[position] - selectedTab.expand() + } - this@AnimatedTabLayout.onChangeListener?.onChanged(position) + private fun initSelectedTab(position: Int) { + if (tabs[position] == selectedTab) { + return } - } + selectedTab.collapse() + selectedTab = tabs[position] + selectedTab.expand() + + this@AnimatedTabLayout.onChangeListener?.onChanged(position) + } }