Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

viewpager2 support #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 13 additions & 3 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 0 additions & 13 deletions .idea/modules.xml

This file was deleted.

12 changes: 0 additions & 12 deletions .idea/runConfigurations.xml

This file was deleted.

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
22 changes: 12 additions & 10 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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')
}
Expand Down

This file was deleted.

3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 2 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
14 changes: 7 additions & 7 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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

}

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
75 changes: 56 additions & 19 deletions library/src/main/java/com/iammert/library/AnimatedTabLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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

Expand All @@ -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()
Expand All @@ -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)

}
}