├── TabSync ├── README.md ├── .gitignore ├── consumer-rules.pro ├── src │ ├── main │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── ahmadhamwi │ │ │ └── tabsync │ │ │ ├── TabViewCompositeClickListener.kt │ │ │ └── TabbedListMediator.kt │ └── test │ │ └── java │ │ └── com │ │ └── ahmadhamwi │ │ └── tabsync │ │ └── ExampleUnitTest.kt ├── gradle.properties ├── proguard-rules.pro └── build.gradle ├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ └── themes.xml │ │ │ ├── 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 │ │ │ ├── mipmap-anydpi-v26 │ │ │ │ ├── ic_launcher.xml │ │ │ │ └── ic_launcher_round.xml │ │ │ ├── values-night │ │ │ │ └── themes.xml │ │ │ ├── layout │ │ │ │ ├── item_item.xml │ │ │ │ ├── item_category.xml │ │ │ │ └── activity_main.xml │ │ │ ├── drawable-v24 │ │ │ │ └── ic_launcher_foreground.xml │ │ │ └── drawable │ │ │ │ └── ic_launcher_background.xml │ │ ├── java │ │ │ └── com │ │ │ │ └── ahmadhamwi │ │ │ │ └── tabsync_example │ │ │ │ ├── model │ │ │ │ ├── Item.kt │ │ │ │ └── Category.kt │ │ │ │ └── ui │ │ │ │ ├── ItemsAdapter.kt │ │ │ │ ├── CategoriesAdapter.kt │ │ │ │ └── MainActivity.kt │ │ └── AndroidManifest.xml │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── ahmadhamwi │ │ │ └── tabsync_example │ │ │ └── ExampleUnitTest.kt │ └── androidTest │ │ └── java │ │ └── com │ │ └── ahmadhamwi │ │ └── tabsync_example │ │ └── ExampleInstrumentedTest.kt ├── proguard-rules.pro └── build.gradle ├── compose-app ├── .gitignore ├── src │ └── main │ │ ├── res │ │ ├── values │ │ │ ├── strings.xml │ │ │ ├── themes.xml │ │ │ └── colors.xml │ │ ├── mipmap-hdpi │ │ │ ├── ic_launcher.webp │ │ │ └── ic_launcher_round.webp │ │ ├── mipmap-mdpi │ │ │ ├── ic_launcher.webp │ │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xhdpi │ │ │ ├── ic_launcher.webp │ │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxhdpi │ │ │ ├── ic_launcher.webp │ │ │ └── ic_launcher_round.webp │ │ ├── mipmap-xxxhdpi │ │ │ ├── ic_launcher.webp │ │ │ └── ic_launcher_round.webp │ │ ├── mipmap-anydpi-v26 │ │ │ ├── ic_launcher.xml │ │ │ └── ic_launcher_round.xml │ │ ├── drawable-v24 │ │ │ └── ic_launcher_foreground.xml │ │ └── drawable │ │ │ └── ic_launcher_background.xml │ │ ├── java │ │ └── com │ │ │ └── ahmadhamwi │ │ │ └── tabsync_compose_example │ │ │ ├── model │ │ │ ├── Item.kt │ │ │ └── Category.kt │ │ │ └── ui │ │ │ ├── theme │ │ │ ├── Color.kt │ │ │ ├── Shape.kt │ │ │ ├── Type.kt │ │ │ └── Theme.kt │ │ │ ├── MainActivity.kt │ │ │ └── components │ │ │ ├── ItemCard.kt │ │ │ ├── TabSyncComposeScreen.kt │ │ │ ├── ItemCategory.kt │ │ │ ├── MyTabBar.kt │ │ │ └── SyncedList.kt │ │ └── AndroidManifest.xml ├── proguard-rules.pro └── build.gradle ├── TabSyncCompose ├── .gitignore ├── consumer-rules.pro ├── src │ └── main │ │ ├── AndroidManifest.xml │ │ └── java │ │ └── com │ │ └── ahmadhamwi │ │ └── tabsync_compose │ │ ├── LazyListStateKtx.kt │ │ ├── TabSyncState.kt │ │ └── tabSyncMediator.kt ├── gradle.properties ├── proguard-rules.pro └── build.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── settings.gradle ├── gradle.properties ├── gradlew.bat ├── .gitignore ├── gradlew ├── README.md └── LICENSE /TabSync/README.md: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /TabSync/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /TabSync/consumer-rules.pro: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /compose-app/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /TabSyncCompose/.gitignore: -------------------------------------------------------------------------------- 1 | /build -------------------------------------------------------------------------------- /TabSyncCompose/consumer-rules.pro: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | tabsync 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /compose-app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | TabSync Compose 3 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | rootProject.name = "tabsync" 2 | include ':TabSync' 3 | include ':app' 4 | include ':TabSyncCompose' 5 | include ':compose-app' -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/java/com/ahmadhamwi/tabsync_example/model/Item.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_example.model 2 | 3 | class Item(val content: String) { 4 | } -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/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/Ahmad-Hamwi/TabSync/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher_round.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/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/Ahmad-Hamwi/TabSync/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/Ahmad-Hamwi/TabSync/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png -------------------------------------------------------------------------------- /compose-app/src/main/res/mipmap-hdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/HEAD/compose-app/src/main/res/mipmap-hdpi/ic_launcher.webp -------------------------------------------------------------------------------- /compose-app/src/main/res/mipmap-mdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/HEAD/compose-app/src/main/res/mipmap-mdpi/ic_launcher.webp -------------------------------------------------------------------------------- /compose-app/src/main/res/mipmap-xhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/HEAD/compose-app/src/main/res/mipmap-xhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /compose-app/src/main/res/mipmap-xxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/HEAD/compose-app/src/main/res/mipmap-xxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /compose-app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/HEAD/compose-app/src/main/res/mipmap-xxxhdpi/ic_launcher.webp -------------------------------------------------------------------------------- /compose-app/src/main/res/mipmap-hdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/HEAD/compose-app/src/main/res/mipmap-hdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /compose-app/src/main/res/mipmap-mdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/HEAD/compose-app/src/main/res/mipmap-mdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /TabSyncCompose/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /compose-app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/HEAD/compose-app/src/main/res/mipmap-xhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /compose-app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/HEAD/compose-app/src/main/res/mipmap-xxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /compose-app/src/main/java/com/ahmadhamwi/tabsync_compose_example/model/Item.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_compose_example.model 2 | 3 | class Item(val content: String) { 4 | } -------------------------------------------------------------------------------- /compose-app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Ahmad-Hamwi/TabSync/HEAD/compose-app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.webp -------------------------------------------------------------------------------- /TabSync/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | -------------------------------------------------------------------------------- /app/src/main/java/com/ahmadhamwi/tabsync_example/model/Category.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_example.model 2 | 3 | class Category(val name: String, vararg item: Item) { 4 | 5 | val listOfItems: List = item.toList() 6 | 7 | } -------------------------------------------------------------------------------- /compose-app/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Thu Sep 23 23:27:37 EEST 2021 2 | distributionBase=GRADLE_USER_HOME 3 | distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip 4 | distributionPath=wrapper/dists 5 | zipStorePath=wrapper/dists 6 | zipStoreBase=GRADLE_USER_HOME 7 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /compose-app/src/main/java/com/ahmadhamwi/tabsync_compose_example/ui/theme/Color.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_compose_example.ui.theme 2 | 3 | import androidx.compose.ui.graphics.Color 4 | 5 | val Purple200 = Color(0xFFBB86FC) 6 | val Purple500 = Color(0xFF6200EE) 7 | val Purple700 = Color(0xFF3700B3) 8 | val Teal200 = Color(0xFF03DAC5) -------------------------------------------------------------------------------- /app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /compose-app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /compose-app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | -------------------------------------------------------------------------------- /compose-app/src/main/java/com/ahmadhamwi/tabsync_compose_example/ui/theme/Shape.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_compose_example.ui.theme 2 | 3 | import androidx.compose.foundation.shape.RoundedCornerShape 4 | import androidx.compose.material.Shapes 5 | import androidx.compose.ui.unit.dp 6 | 7 | val Shapes = Shapes( 8 | small = RoundedCornerShape(4.dp), 9 | medium = RoundedCornerShape(4.dp), 10 | large = RoundedCornerShape(0.dp) 11 | ) -------------------------------------------------------------------------------- /compose-app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF03DAC5 7 | #FF018786 8 | #FF000000 9 | #FFFFFFFF 10 | -------------------------------------------------------------------------------- /TabSync/src/test/java/com/ahmadhamwi/tabsync/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/test/java/com/ahmadhamwi/tabsync_example/ExampleUnitTest.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_example 2 | 3 | import org.junit.Test 4 | 5 | import org.junit.Assert.* 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * See [testing documentation](http://d.android.com/tools/testing). 11 | */ 12 | class ExampleUnitTest { 13 | @Test 14 | fun addition_isCorrect() { 15 | assertEquals(4, 2 + 2) 16 | } 17 | } -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #FFBB86FC 4 | #FF6200EE 5 | #FF3700B3 6 | #FF03DAC5 7 | #FF018786 8 | #FF000000 9 | #FFFFFFFF 10 | #FFE0E0E0 11 | -------------------------------------------------------------------------------- /TabSync/gradle.properties: -------------------------------------------------------------------------------- 1 | GROUP=io.github.ahmad-hamwi 2 | POM_ARTIFACT_ID=tabsync 3 | VERSION_NAME=1.0.1 4 | 5 | POM_NAME=tabsync 6 | POM_PACKAGING=aar 7 | 8 | POM_DESCRIPTION=A lightweight synchronizer between Android's TabLayout and RecyclerView. 9 | POM_INCEPTION_YEAR=2021 10 | 11 | POM_URL=https://github.com/Ahmad-Hamwi/TabSync 12 | POM_SCM_URL=https://github.com/Ahmad-Hamwi/TabSync 13 | POM_SCM_CONNECTION=scm:git@github.com:Ahmad-Hamwi/tabsync.git 14 | POM_SCM_DEV_CONNECTION=scm:git@github.com:Ahmad-Hamwi/tabsync.git 15 | 16 | POM_LICENCE_NAME=The Apache Software License, Version 2.0 17 | POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt 18 | POM_LICENCE_DIST=repo 19 | 20 | POM_DEVELOPER_ID=Ahmad-Hamwi 21 | POM_DEVELOPER_NAME=Ahmad Hamwi 22 | POM_DEVELOPER_URL=https://github.com/Ahmad-Hamwi -------------------------------------------------------------------------------- /TabSyncCompose/gradle.properties: -------------------------------------------------------------------------------- 1 | GROUP=io.github.ahmad-hamwi 2 | POM_ARTIFACT_ID=tabsync-compose 3 | VERSION_NAME=1.0.1 4 | 5 | POM_NAME=tabsync-compose 6 | POM_PACKAGING=aar 7 | 8 | POM_DESCRIPTION=A lightweight synchronizer between Android's TabLayout and RecyclerView. 9 | POM_INCEPTION_YEAR=2021 10 | 11 | POM_URL=https://github.com/Ahmad-Hamwi/TabSync 12 | POM_SCM_URL=https://github.com/Ahmad-Hamwi/TabSync 13 | POM_SCM_CONNECTION=scm:git@github.com:Ahmad-Hamwi/tabsync.git 14 | POM_SCM_DEV_CONNECTION=scm:git@github.com:Ahmad-Hamwi/tabsync.git 15 | 16 | POM_LICENCE_NAME=The Apache Software License, Version 2.0 17 | POM_LICENCE_URL=http://www.apache.org/licenses/LICENSE-2.0.txt 18 | POM_LICENCE_DIST=repo 19 | 20 | POM_DEVELOPER_ID=Ahmad-Hamwi 21 | POM_DEVELOPER_NAME=Ahmad Hamwi 22 | POM_DEVELOPER_URL=https://github.com/Ahmad-Hamwi -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /TabSync/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 -------------------------------------------------------------------------------- /app/src/androidTest/java/com/ahmadhamwi/tabsync_example/ExampleInstrumentedTest.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_example 2 | 3 | import androidx.test.platform.app.InstrumentationRegistry 4 | import androidx.test.ext.junit.runners.AndroidJUnit4 5 | 6 | import org.junit.Test 7 | import org.junit.runner.RunWith 8 | 9 | import org.junit.Assert.* 10 | 11 | /** 12 | * Instrumented test, which will execute on an Android device. 13 | * 14 | * See [testing documentation](http://d.android.com/tools/testing). 15 | */ 16 | @RunWith(AndroidJUnit4::class) 17 | class ExampleInstrumentedTest { 18 | @Test 19 | fun useAppContext() { 20 | // Context of the app under test. 21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext 22 | assertEquals("com.ahmadhamwi.tabsync_test", appContext.packageName) 23 | } 24 | } -------------------------------------------------------------------------------- /compose-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 -------------------------------------------------------------------------------- /TabSyncCompose/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 -------------------------------------------------------------------------------- /app/src/main/res/values-night/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /compose-app/src/main/java/com/ahmadhamwi/tabsync_compose_example/ui/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_compose_example.ui 2 | 3 | import android.os.Bundle 4 | import androidx.activity.ComponentActivity 5 | import androidx.activity.compose.setContent 6 | import androidx.compose.runtime.Composable 7 | import androidx.compose.ui.tooling.preview.Preview 8 | import com.ahmadhamwi.tabsync_compose_example.model.dummyCategories 9 | import com.ahmadhamwi.tabsync_compose_example.ui.components.TabSyncComposeScreen 10 | import com.ahmadhamwi.tabsync_compose_example.ui.theme.TabsyncTheme 11 | 12 | class MainActivity : ComponentActivity() { 13 | override fun onCreate(savedInstanceState: Bundle?) { 14 | super.onCreate(savedInstanceState) 15 | setContent { TabSyncComposeApp() } 16 | } 17 | } 18 | 19 | @Preview 20 | @Composable 21 | fun TabSyncComposeApp() { 22 | TabsyncTheme { 23 | TabSyncComposeScreen(dummyCategories) 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /compose-app/src/main/java/com/ahmadhamwi/tabsync_compose_example/ui/theme/Type.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_compose_example.ui.theme 2 | 3 | import androidx.compose.material.Typography 4 | import androidx.compose.ui.text.TextStyle 5 | import androidx.compose.ui.text.font.FontFamily 6 | import androidx.compose.ui.text.font.FontWeight 7 | import androidx.compose.ui.unit.sp 8 | 9 | // Set of Material typography styles to start with 10 | val Typography = Typography( 11 | body1 = TextStyle( 12 | fontFamily = FontFamily.Default, 13 | fontWeight = FontWeight.Normal, 14 | fontSize = 16.sp 15 | ) 16 | /* Other default text styles to override 17 | button = TextStyle( 18 | fontFamily = FontFamily.Default, 19 | fontWeight = FontWeight.W500, 20 | fontSize = 14.sp 21 | ), 22 | caption = TextStyle( 23 | fontFamily = FontFamily.Default, 24 | fontWeight = FontWeight.Normal, 25 | fontSize = 12.sp 26 | ) 27 | */ 28 | ) -------------------------------------------------------------------------------- /TabSyncCompose/src/main/java/com/ahmadhamwi/tabsync_compose/LazyListStateKtx.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_compose 2 | 3 | import androidx.compose.foundation.lazy.LazyListState 4 | 5 | fun LazyListState.findFirstFullyVisibleItemIndex(): Int = findFullyVisibleItemIndex(reversed = false) 6 | 7 | fun LazyListState.findLastFullyVisibleItemIndex(): Int = findFullyVisibleItemIndex(reversed = true) 8 | 9 | fun LazyListState.findFullyVisibleItemIndex(reversed: Boolean): Int { 10 | layoutInfo.visibleItemsInfo.run { if (reversed) reversed() else this }.forEach { itemInfo -> 11 | val itemStartOffset = itemInfo.offset 12 | val itemEndOffset = itemInfo.offset + itemInfo.size 13 | val viewportStartOffset = layoutInfo.viewportStartOffset 14 | val viewportEndOffset = layoutInfo.viewportEndOffset 15 | if (itemStartOffset >= viewportStartOffset && itemEndOffset <= viewportEndOffset) { 16 | return itemInfo.index 17 | } 18 | } 19 | return -1 20 | } 21 | -------------------------------------------------------------------------------- /compose-app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 13 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /TabSync/src/main/java/com/ahmadhamwi/tabsync/TabViewCompositeClickListener.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync 2 | 3 | import com.google.android.material.tabs.TabLayout 4 | import java.util.* 5 | 6 | class TabViewCompositeClickListener(private val mTabLayout: TabLayout) { 7 | 8 | private val listeners: MutableList<(tab: TabLayout.Tab, position: Int) -> Unit> = ArrayList() 9 | 10 | fun addListener(listener: (tab: TabLayout.Tab, position: Int) -> Unit) { 11 | listeners.add(listener) 12 | } 13 | 14 | fun removeListener(listener: (tab: TabLayout.Tab, position: Int) -> Unit) { 15 | listeners.remove(listener) 16 | } 17 | 18 | fun build() { 19 | for (i in 0 until mTabLayout.tabCount) { 20 | mTabLayout.getTabAt(i)!!.view.setOnClickListener { 21 | for (listener in listeners) { 22 | listener(mTabLayout.getTabAt(i)!!, i) 23 | } 24 | } 25 | } 26 | } 27 | 28 | fun getListeners(): List<(tab: TabLayout.Tab, position: Int) -> Unit> { 29 | return listeners 30 | } 31 | } -------------------------------------------------------------------------------- /app/src/main/res/values/themes.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 20 | -------------------------------------------------------------------------------- /app/src/main/res/layout/item_item.xml: -------------------------------------------------------------------------------- 1 | 2 | 13 | 14 | 25 | 26 | -------------------------------------------------------------------------------- /compose-app/src/main/java/com/ahmadhamwi/tabsync_compose_example/ui/components/ItemCard.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_compose_example.ui.components 2 | 3 | import androidx.compose.foundation.layout.Box 4 | import androidx.compose.foundation.layout.defaultMinSize 5 | import androidx.compose.foundation.layout.fillMaxWidth 6 | import androidx.compose.material.Card 7 | import androidx.compose.material.Text 8 | import androidx.compose.runtime.Composable 9 | import androidx.compose.ui.Alignment 10 | import androidx.compose.ui.Modifier 11 | import androidx.compose.ui.text.style.TextAlign 12 | import androidx.compose.ui.tooling.preview.Preview 13 | import androidx.compose.ui.unit.dp 14 | import com.ahmadhamwi.tabsync_compose_example.model.Item 15 | 16 | @Composable 17 | fun ItemCard(item: Item) { 18 | Card( 19 | modifier = Modifier 20 | .fillMaxWidth() 21 | .defaultMinSize(minHeight = 64.dp), 22 | elevation = 8.dp 23 | ) { 24 | Box(contentAlignment = Alignment.Center) { 25 | Text(item.content, textAlign = TextAlign.Center) 26 | } 27 | } 28 | } 29 | 30 | @Preview 31 | @Composable 32 | private fun ItemCardPrev() { 33 | ItemCard(Item("Item 1")) 34 | } -------------------------------------------------------------------------------- /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=-Xmx2048m -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 -------------------------------------------------------------------------------- /app/src/main/java/com/ahmadhamwi/tabsync_example/ui/ItemsAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_example.ui 2 | 3 | import android.content.Context 4 | import android.view.LayoutInflater 5 | import android.view.View 6 | import android.view.ViewGroup 7 | import android.widget.TextView 8 | import androidx.recyclerview.widget.RecyclerView 9 | import com.ahmadhamwi.tabsync_example.R 10 | import com.ahmadhamwi.tabsync_example.model.Item 11 | 12 | class ItemsAdapter( 13 | private val context: Context, 14 | private var items: List 15 | ) : 16 | RecyclerView.Adapter() { 17 | 18 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder { 19 | return ItemViewHolder( 20 | LayoutInflater.from(context).inflate(R.layout.item_item, parent, false) 21 | ) 22 | } 23 | 24 | override fun onBindViewHolder(holder: ItemViewHolder, position: Int) { 25 | holder.bind(items[position]) 26 | } 27 | 28 | override fun getItemCount(): Int { 29 | return items.size 30 | } 31 | 32 | class ItemViewHolder(private val view: View) : 33 | RecyclerView.ViewHolder(view) { 34 | 35 | fun bind(item: Item) { 36 | view.findViewById(R.id.textViewContent).text = item.content 37 | } 38 | } 39 | } 40 | 41 | 42 | -------------------------------------------------------------------------------- /compose-app/src/main/java/com/ahmadhamwi/tabsync_compose_example/model/Category.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_compose_example.model 2 | 3 | class Category(val name: String, vararg item: Item) { 4 | 5 | val listOfItems: List = item.toList() 6 | 7 | } 8 | 9 | val dummyCategories = mutableListOf( 10 | Category( 11 | "Category 1", 12 | Item("Item 1"), 13 | Item("Item 2"), 14 | Item("Item 3"), 15 | Item("Item 4"), 16 | Item("Item 5"), 17 | Item("Item 6") 18 | ), 19 | Category( 20 | "Category 2", 21 | Item("Item 1"), 22 | Item("Item 2"), 23 | Item("Item 3"), 24 | Item("Item 4"), 25 | ), 26 | Category( 27 | "Category 3", 28 | Item("Item 1"), 29 | Item("Item 2"), 30 | Item("Item 3"), 31 | Item("Item 4"), 32 | Item("Item 5"), 33 | Item("Item 6"), 34 | Item("Item 7"), 35 | Item("Item 8"), 36 | ), 37 | Category( 38 | "Category 4", 39 | Item("Item 1"), 40 | Item("Item 2"), 41 | Item("Item 3"), 42 | Item("Item 4"), 43 | Item("Item 5"), 44 | Item("Item 6") 45 | ), 46 | Category( 47 | "Category 5", 48 | Item("Item 1"), 49 | Item("Item 2"), 50 | Item("Item 3"), 51 | Item("Item 4"), 52 | ), 53 | ) -------------------------------------------------------------------------------- /TabSyncCompose/src/main/java/com/ahmadhamwi/tabsync_compose/TabSyncState.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_compose 2 | 3 | import androidx.compose.foundation.lazy.LazyListState 4 | import androidx.compose.runtime.Stable 5 | import kotlinx.coroutines.CoroutineScope 6 | import kotlinx.coroutines.launch 7 | 8 | @Stable 9 | class TabSyncState( 10 | var selectedTabIndex: Int, 11 | var lazyListState: LazyListState, 12 | private var coroutineScope: CoroutineScope, 13 | private var syncedIndices: List, 14 | private var smoothScroll: Boolean, 15 | ) { 16 | operator fun component1(): Int = selectedTabIndex 17 | operator fun component2(): (Int) -> Unit = { 18 | require(it <= syncedIndices.size - 1) { 19 | "The selected tab's index is out of the bounds of the syncedIndices list provided. " + 20 | "Either add an index to syncedIndices that corresponds to an item to your lazy list, " + 21 | "or remove your excessive tab" 22 | } 23 | 24 | selectedTabIndex = it 25 | 26 | coroutineScope.launch { 27 | if (smoothScroll) { 28 | lazyListState.animateScrollToItem(syncedIndices[selectedTabIndex]) 29 | } else { 30 | lazyListState.scrollToItem(syncedIndices[selectedTabIndex]) 31 | } 32 | } 33 | } 34 | 35 | operator fun component3(): LazyListState = lazyListState 36 | } -------------------------------------------------------------------------------- /compose-app/src/main/java/com/ahmadhamwi/tabsync_compose_example/ui/theme/Theme.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_compose_example.ui.theme 2 | 3 | import androidx.compose.foundation.isSystemInDarkTheme 4 | import androidx.compose.foundation.layout.fillMaxSize 5 | import androidx.compose.material.* 6 | import androidx.compose.runtime.Composable 7 | import androidx.compose.ui.Modifier 8 | 9 | private val DarkColorPalette = darkColors( 10 | primary = Purple200, 11 | primaryVariant = Purple700, 12 | secondary = Teal200 13 | ) 14 | 15 | private val LightColorPalette = lightColors( 16 | primary = Purple500, 17 | primaryVariant = Purple700, 18 | secondary = Teal200 19 | 20 | /* Other default colors to override 21 | background = Color.White, 22 | surface = Color.White, 23 | onPrimary = Color.White, 24 | onSecondary = Color.Black, 25 | onBackground = Color.Black, 26 | onSurface = Color.Black, 27 | */ 28 | ) 29 | 30 | @Composable 31 | fun TabsyncTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: @Composable () -> Unit) { 32 | val colors = if (darkTheme) { 33 | DarkColorPalette 34 | } else { 35 | LightColorPalette 36 | } 37 | 38 | MaterialTheme( 39 | colors = colors, 40 | typography = Typography, 41 | shapes = Shapes 42 | ) { 43 | Surface( 44 | modifier = Modifier.fillMaxSize(), 45 | content = content 46 | ) 47 | } 48 | } -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'kotlin-android' 4 | } 5 | 6 | android { 7 | compileSdkVersion 31 8 | 9 | defaultConfig { 10 | applicationId "com.ahmadhamwi.tabsync_test" 11 | minSdkVersion 21 12 | targetSdkVersion 31 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | compileOptions { 26 | sourceCompatibility JavaVersion.VERSION_11 27 | targetCompatibility JavaVersion.VERSION_11 28 | } 29 | kotlinOptions { 30 | jvmTarget = '11' 31 | } 32 | } 33 | 34 | dependencies { 35 | 36 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 37 | implementation 'androidx.core:core-ktx:1.6.0' 38 | implementation 'androidx.appcompat:appcompat:1.3.0' 39 | implementation 'com.google.android.material:material:1.4.0' 40 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4' 41 | testImplementation 'junit:junit:4.+' 42 | androidTestImplementation 'androidx.test.ext:junit:1.1.3' 43 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0' 44 | 45 | implementation project(':TabSync') 46 | } -------------------------------------------------------------------------------- /compose-app/src/main/java/com/ahmadhamwi/tabsync_compose_example/ui/components/TabSyncComposeScreen.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_compose_example.ui.components 2 | 3 | import androidx.compose.foundation.layout.Column 4 | import androidx.compose.runtime.Composable 5 | import com.ahmadhamwi.tabsync_compose.lazyListTabSync 6 | import com.ahmadhamwi.tabsync_compose_example.model.Category 7 | 8 | @Composable 9 | fun TabSyncComposeScreen(categories: List) { 10 | val (selectedTabIndex, setSelectedTabIndex, listState) = lazyListTabSync(categories.indices.toList()) 11 | 12 | // val (selectedTabIndex, setSelectedTabIndex, listState) = tabSyncMediator( 13 | // mutableListOf(0, 2, 4), //Mandatory. The indices of lazy list items to sync the tabs with 14 | // tabsCount = 3, //Optional. To check for viability of the synchronization with the tabs. Optimal when equals the count of syncedIndices. 15 | // lazyListState = rememberLazyListState(), //Optional. To provide your own LazyListState. Defaults to rememberLazyListState(). 16 | // smoothScroll = true, // Optional. To make the auto scroll smooth or not when clicking tabs. Defaults to true 17 | // ) 18 | 19 | Column { 20 | MyTabBar( 21 | categories = categories, 22 | selectedTabIndex = selectedTabIndex, 23 | onTabClicked = { index, _ -> setSelectedTabIndex(index) } 24 | ) 25 | 26 | MyLazyList(categories, listState) 27 | } 28 | } 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /compose-app/src/main/java/com/ahmadhamwi/tabsync_compose_example/ui/components/ItemCategory.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_compose_example.ui.components 2 | 3 | import androidx.compose.foundation.layout.* 4 | import androidx.compose.foundation.lazy.LazyColumn 5 | import androidx.compose.foundation.lazy.itemsIndexed 6 | import androidx.compose.material.Text 7 | import androidx.compose.runtime.Composable 8 | import androidx.compose.ui.Modifier 9 | import androidx.compose.ui.input.nestedscroll.nestedScroll 10 | import androidx.compose.ui.tooling.preview.Preview 11 | import androidx.compose.ui.unit.dp 12 | import androidx.compose.ui.unit.sp 13 | import com.ahmadhamwi.tabsync_compose_example.model.Category 14 | import com.ahmadhamwi.tabsync_compose_example.model.Item 15 | 16 | @Composable 17 | fun ItemCategory( 18 | category: Category, 19 | ) { 20 | Column( 21 | modifier = Modifier.padding(horizontal = 16.dp) 22 | ) { 23 | Text(category.name, fontSize = 18.sp) 24 | Spacer(Modifier.height(8.dp)) 25 | Column( 26 | verticalArrangement = Arrangement.spacedBy(16.dp), 27 | ) { 28 | category.listOfItems.forEach { 29 | ItemCard(item = it) 30 | } 31 | } 32 | } 33 | } 34 | 35 | @Composable 36 | @Preview 37 | private fun ItemCategoryPreview() { 38 | ItemCategory( 39 | Category( 40 | "Category 1", 41 | Item("Item 1"), 42 | Item("Item 2"), 43 | ) 44 | ) 45 | } -------------------------------------------------------------------------------- /app/src/main/java/com/ahmadhamwi/tabsync_example/ui/CategoriesAdapter.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_example.ui 2 | 3 | import android.content.Context 4 | import android.view.LayoutInflater 5 | import android.view.View 6 | import android.view.ViewGroup 7 | import android.widget.TextView 8 | import androidx.recyclerview.widget.RecyclerView 9 | import com.ahmadhamwi.tabsync_example.R 10 | import com.ahmadhamwi.tabsync_example.model.Category 11 | 12 | class CategoriesAdapter( 13 | private val context: Context, 14 | private val listOfCategories: List 15 | ) : RecyclerView.Adapter() { 16 | 17 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder { 18 | return CategoryViewHolder( 19 | LayoutInflater.from(context).inflate(R.layout.item_category, parent, false) 20 | ) 21 | } 22 | 23 | override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) { 24 | holder.bind(listOfCategories[position]) 25 | } 26 | 27 | override fun getItemCount(): Int { 28 | return listOfCategories.size 29 | } 30 | 31 | class CategoryViewHolder( 32 | private val view: View 33 | ) : RecyclerView.ViewHolder(view) { 34 | 35 | fun bind(category: Category) { 36 | view.findViewById(R.id.categoryName).text = category.name 37 | view.findViewById(R.id.recyclerView).adapter = 38 | ItemsAdapter(view.context, category.listOfItems) 39 | } 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /compose-app/src/main/java/com/ahmadhamwi/tabsync_compose_example/ui/components/MyTabBar.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_compose_example.ui.components 2 | 3 | import androidx.compose.material.ScrollableTabRow 4 | import androidx.compose.material.Tab 5 | import androidx.compose.material.Text 6 | import androidx.compose.runtime.* 7 | import androidx.compose.ui.tooling.preview.Preview 8 | import androidx.compose.ui.unit.dp 9 | import com.ahmadhamwi.tabsync_compose_example.model.Category 10 | 11 | @Composable 12 | fun MyTabBar( 13 | categories: List, 14 | selectedTabIndex: Int, 15 | onTabClicked: (index: Int, category: Category) -> Unit 16 | ) { 17 | ScrollableTabRow( 18 | selectedTabIndex = selectedTabIndex, 19 | edgePadding = 0.dp 20 | ) { 21 | categories.forEachIndexed { index, category -> 22 | Tab( 23 | selected = index == selectedTabIndex, 24 | onClick = { onTabClicked(index, category) }, 25 | text = { Text(category.name.uppercase()) } 26 | ) 27 | } 28 | } 29 | } 30 | 31 | @Composable 32 | @Preview 33 | fun SyncedTabBarPreview() { 34 | var selectedIndex by remember { mutableStateOf(0) } 35 | 36 | MyTabBar( 37 | categories = mutableListOf( 38 | Category("Category 1"), 39 | Category("Category 2"), 40 | Category("Category 3"), 41 | Category("Category 4"), 42 | Category("Category 5"), 43 | ), 44 | selectedTabIndex = selectedIndex 45 | ) { index, _ -> 46 | selectedIndex = index 47 | } 48 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/item_category.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 22 | 23 | 35 | 36 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 19 | 20 | 32 | 33 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /compose-app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 9 | 15 | 18 | 21 | 22 | 23 | 24 | 30 | -------------------------------------------------------------------------------- /compose-app/src/main/java/com/ahmadhamwi/tabsync_compose_example/ui/components/SyncedList.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_compose_example.ui.components 2 | 3 | import androidx.compose.foundation.layout.Arrangement 4 | import androidx.compose.foundation.layout.PaddingValues 5 | import androidx.compose.foundation.lazy.LazyColumn 6 | import androidx.compose.foundation.lazy.LazyListState 7 | import androidx.compose.foundation.lazy.itemsIndexed 8 | import androidx.compose.foundation.lazy.rememberLazyListState 9 | import androidx.compose.runtime.Composable 10 | import androidx.compose.runtime.derivedStateOf 11 | import androidx.compose.runtime.remember 12 | import androidx.compose.ui.tooling.preview.Preview 13 | import androidx.compose.ui.unit.dp 14 | import com.ahmadhamwi.tabsync_compose_example.model.Category 15 | import com.ahmadhamwi.tabsync_compose_example.model.Item 16 | 17 | @Composable 18 | fun MyLazyList( 19 | categories: List, 20 | listState: LazyListState = rememberLazyListState(), 21 | ) { 22 | LazyColumn( 23 | state = listState, 24 | verticalArrangement = Arrangement.spacedBy(16.dp) 25 | ) { 26 | itemsIndexed(categories) { _, category -> 27 | ItemCategory(category) 28 | } 29 | } 30 | } 31 | 32 | @Composable 33 | @Preview 34 | fun SyncedLazyListPreview() { 35 | MyLazyList( 36 | listOf( 37 | Category( 38 | "Category 1", 39 | Item("Item 1"), 40 | Item("Item 2"), 41 | Item("Item 3"), 42 | Item("Item 4"), 43 | Item("Item 5"), 44 | Item("Item 6") 45 | ), 46 | Category( 47 | "Category 2", 48 | Item("Item 1"), 49 | Item("Item 2"), 50 | Item("Item 3"), 51 | Item("Item 4"), 52 | ), 53 | ), 54 | rememberLazyListState() 55 | ) 56 | } -------------------------------------------------------------------------------- /compose-app/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.application' 3 | id 'org.jetbrains.kotlin.android' 4 | } 5 | 6 | android { 7 | compileSdk 33 8 | 9 | defaultConfig { 10 | applicationId "com.ahmadhamwi.tabsync_compose_example" 11 | minSdk 24 12 | targetSdk 33 13 | versionCode 1 14 | versionName "1.0" 15 | 16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 17 | vectorDrawables { 18 | useSupportLibrary true 19 | } 20 | } 21 | 22 | buildTypes { 23 | release { 24 | minifyEnabled false 25 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 26 | } 27 | } 28 | compileOptions { 29 | sourceCompatibility JavaVersion.VERSION_1_8 30 | targetCompatibility JavaVersion.VERSION_1_8 31 | } 32 | kotlinOptions { 33 | jvmTarget = '1.8' 34 | } 35 | buildFeatures { 36 | compose true 37 | } 38 | composeOptions { 39 | kotlinCompilerExtensionVersion compose_version 40 | } 41 | packagingOptions { 42 | resources { 43 | excludes += '/META-INF/{AL2.0,LGPL2.1}' 44 | } 45 | } 46 | } 47 | 48 | dependencies { 49 | 50 | 51 | 52 | implementation 'androidx.core:core-ktx:1.9.0' 53 | implementation "androidx.compose.ui:ui:$compose_version" 54 | implementation "androidx.compose.material:material:$compose_version" 55 | implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" 56 | implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1' 57 | implementation 'androidx.activity:activity-compose:1.6.1' 58 | testImplementation 'junit:junit:4.13.2' 59 | androidTestImplementation 'androidx.test.ext:junit:1.1.5' 60 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' 61 | androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version" 62 | debugImplementation "androidx.compose.ui:ui-tooling:$compose_version" 63 | 64 | implementation project(':TabSyncCompose') 65 | } -------------------------------------------------------------------------------- /TabSyncCompose/src/main/java/com/ahmadhamwi/tabsync_compose/tabSyncMediator.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_compose 2 | 3 | import androidx.compose.foundation.lazy.LazyListState 4 | import androidx.compose.foundation.lazy.rememberLazyListState 5 | import androidx.compose.runtime.* 6 | 7 | @Composable 8 | fun lazyListTabSync( 9 | syncedIndices: List, 10 | lazyListState: LazyListState = rememberLazyListState(), 11 | tabsCount: Int? = null, 12 | smoothScroll: Boolean = true 13 | ): TabSyncState { 14 | require(syncedIndices.isNotEmpty()) { 15 | "You can't use the mediator without providing at least one index in the syncedIndices array" 16 | } 17 | 18 | if (tabsCount != null) { 19 | require(tabsCount <= syncedIndices.size) { 20 | "The tabs count is out of the bounds of the syncedIndices list provided. " + 21 | "Either add an index to syncedIndices that corresponds to an item to your lazy list, " + 22 | "or remove your excessive tab" 23 | } 24 | } 25 | 26 | var selectedTabIndex by remember { mutableStateOf(0) } 27 | 28 | LaunchedEffect(lazyListState) { 29 | snapshotFlow { lazyListState.layoutInfo }.collect { 30 | var itemPosition = lazyListState.findFirstFullyVisibleItemIndex() 31 | 32 | if (itemPosition == -1) { 33 | itemPosition = lazyListState.firstVisibleItemIndex 34 | } 35 | 36 | if (itemPosition == -1) { 37 | return@collect 38 | } 39 | 40 | if (lazyListState.findLastFullyVisibleItemIndex() == syncedIndices.last()) { 41 | itemPosition = syncedIndices.last() 42 | } 43 | 44 | if (syncedIndices.contains(itemPosition) && itemPosition != syncedIndices[selectedTabIndex]) { 45 | selectedTabIndex = syncedIndices.indexOf(itemPosition) 46 | } 47 | } 48 | } 49 | 50 | return TabSyncState( 51 | selectedTabIndex, 52 | lazyListState, 53 | rememberCoroutineScope(), 54 | syncedIndices, 55 | smoothScroll 56 | ) 57 | } -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /TabSync/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.library' 3 | id 'kotlin-android' 4 | id 'com.vanniktech.maven.publish' 5 | } 6 | 7 | android { 8 | compileSdkVersion 31 9 | 10 | defaultConfig { 11 | minSdkVersion 21 12 | targetSdkVersion 31 13 | 14 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 15 | consumerProguardFiles "consumer-rules.pro" 16 | } 17 | 18 | buildTypes { 19 | release { 20 | minifyEnabled false 21 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 22 | } 23 | } 24 | compileOptions { 25 | sourceCompatibility JavaVersion.VERSION_11 26 | targetCompatibility JavaVersion.VERSION_11 27 | } 28 | kotlinOptions { 29 | jvmTarget = '11' 30 | } 31 | } 32 | 33 | dependencies { 34 | implementation fileTree(dir: "libs", include: ["*.jar"]) 35 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" 36 | implementation 'androidx.core:core-ktx:1.6.0' 37 | implementation 'androidx.appcompat:appcompat:1.3.1' 38 | implementation 'com.google.android.material:material:1.4.0' 39 | } 40 | 41 | plugins.withId("com.vanniktech.maven.publish.base") { 42 | group = "io.github.ahmad-hamwi" 43 | version = "1.0.1" 44 | 45 | mavenPublishing { 46 | publishToMavenCentral("S01") 47 | 48 | // Will only apply to non snapshot builds. 49 | // Uses credentials as described above, supports both regular and in memory signing. 50 | signAllPublications() 51 | 52 | pom { 53 | name = "TabSync" 54 | description = "A lightweight synchronizer between Android's TabLayout and RecyclerView." 55 | inceptionYear = "2021" 56 | url = "https ://github.com/Ahmad-Hamwi/TabSync/" 57 | licenses { 58 | license { 59 | name = "The Apache License, Version 2.0" 60 | url = "http://www.apache.org/licenses/LICENSE-2.0.txt" 61 | distribution = "http://www.apache.org/licenses/LICENSE-2.0.txt" 62 | } 63 | } 64 | developers { 65 | developer { 66 | id = "Ahmad-Hamwi" 67 | name = "Ahmad Hamwi" 68 | url = "https://github.com/Ahmad-Hamwi/" 69 | } 70 | } 71 | scm { 72 | url = "https://github.com/Ahmad-Hamwi/TabSync/" 73 | connection = "scm:git:git://github.com/Ahmad-Hamwi/TabSync.git" 74 | developerConnection = "scm:git:ssh://git@github.com/Ahmad-Hamwi/TabSync.git" 75 | } 76 | } 77 | } 78 | } -------------------------------------------------------------------------------- /app/src/main/java/com/ahmadhamwi/tabsync_example/ui/MainActivity.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync_example.ui 2 | 3 | import androidx.appcompat.app.AppCompatActivity 4 | import android.os.Bundle 5 | import androidx.recyclerview.widget.RecyclerView 6 | import com.ahmadhamwi.tabsync_example.R 7 | import com.ahmadhamwi.tabsync_example.model.Category 8 | import com.ahmadhamwi.tabsync_example.model.Item 9 | import com.ahmadhamwi.tabsync.TabbedListMediator 10 | import com.google.android.material.tabs.TabLayout 11 | 12 | class MainActivity : AppCompatActivity() { 13 | 14 | private lateinit var tabLayout: TabLayout 15 | private lateinit var recyclerView: RecyclerView 16 | 17 | private val categories = mutableListOf( 18 | Category( 19 | "Category 1", 20 | Item("Item 1"), 21 | Item("Item 2"), 22 | Item("Item 3"), 23 | Item("Item 4"), 24 | Item("Item 5"), 25 | Item("Item 6") 26 | ), 27 | Category( 28 | "Category 2", 29 | Item("Item 1"), 30 | Item("Item 2"), 31 | Item("Item 3"), 32 | Item("Item 4"), 33 | ), 34 | Category( 35 | "Category 3", 36 | Item("Item 1"), 37 | Item("Item 2"), 38 | Item("Item 3"), 39 | Item("Item 4"), 40 | Item("Item 5"), 41 | Item("Item 6"), 42 | Item("Item 7"), 43 | Item("Item 8"), 44 | ), 45 | Category( 46 | "Category 4", 47 | Item("Item 1"), 48 | Item("Item 2"), 49 | Item("Item 3"), 50 | Item("Item 4"), 51 | Item("Item 5"), 52 | Item("Item 6") 53 | ), 54 | Category( 55 | "Category 5", 56 | Item("Item 1"), 57 | Item("Item 2"), 58 | Item("Item 4"), 59 | Item("Item 5"), 60 | ), 61 | ) 62 | 63 | override fun onCreate(savedInstanceState: Bundle?) { 64 | super.onCreate(savedInstanceState) 65 | setContentView(R.layout.activity_main) 66 | 67 | initViews() 68 | initTabLayout() 69 | initRecycler() 70 | initMediator() 71 | } 72 | 73 | private fun initViews() { 74 | tabLayout = findViewById(R.id.tabLayout) 75 | recyclerView = findViewById(R.id.recyclerView) 76 | } 77 | 78 | private fun initTabLayout() { 79 | for (category in categories) { 80 | tabLayout.addTab(tabLayout.newTab().setText(category.name)) 81 | } 82 | } 83 | 84 | private fun initRecycler() { 85 | recyclerView.adapter = CategoriesAdapter(this, categories) 86 | } 87 | 88 | private fun initMediator() { 89 | TabbedListMediator( 90 | recyclerView, 91 | tabLayout, 92 | categories.indices.toList() 93 | ).attach() 94 | } 95 | } -------------------------------------------------------------------------------- /TabSyncCompose/build.gradle: -------------------------------------------------------------------------------- 1 | plugins { 2 | id 'com.android.library' 3 | id 'org.jetbrains.kotlin.android' 4 | id 'com.vanniktech.maven.publish' 5 | } 6 | 7 | android { 8 | namespace 'com.ahmadhamwi.tabsync_compose' 9 | compileSdk 33 10 | 11 | defaultConfig { 12 | minSdk 24 13 | targetSdk 33 14 | 15 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" 16 | consumerProguardFiles "consumer-rules.pro" 17 | } 18 | 19 | buildTypes { 20 | release { 21 | minifyEnabled false 22 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 23 | } 24 | } 25 | compileOptions { 26 | sourceCompatibility JavaVersion.VERSION_1_8 27 | targetCompatibility JavaVersion.VERSION_1_8 28 | } 29 | kotlinOptions { 30 | jvmTarget = '1.8' 31 | } 32 | buildFeatures { 33 | compose true 34 | } 35 | composeOptions { 36 | kotlinCompilerExtensionVersion compose_version 37 | } 38 | } 39 | 40 | dependencies { 41 | 42 | implementation 'androidx.core:core-ktx:1.9.0' 43 | implementation 'androidx.appcompat:appcompat:1.6.0' 44 | implementation 'com.google.android.material:material:1.7.0' 45 | implementation "androidx.compose.ui:ui:$compose_version" 46 | implementation "androidx.compose.material:material:$compose_version" 47 | implementation "androidx.compose.ui:ui-tooling-preview:$compose_version" 48 | implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1' 49 | implementation 'androidx.activity:activity-compose:1.6.1' 50 | debugImplementation "androidx.compose.ui:ui-tooling:$compose_version" 51 | testImplementation 'junit:junit:4.13.2' 52 | androidTestImplementation 'androidx.test.ext:junit:1.1.5' 53 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1' 54 | androidTestImplementation "androidx.compose.ui:ui-test-junit4:$compose_version" 55 | } 56 | 57 | plugins.withId("com.vanniktech.maven.publish.base") { 58 | group = "io.github.ahmad-hamwi" 59 | version = "1.0.1" 60 | 61 | mavenPublishing { 62 | publishToMavenCentral("S01") 63 | 64 | // Will only apply to non snapshot builds. 65 | // Uses credentials as described above, supports both regular and in memory signing. 66 | signAllPublications() 67 | 68 | pom { 69 | name = "tabsync-compose" 70 | description = "A lightweight synchronizer between Android Jetpack Compose's LazyListState and ScrollableTabRow" 71 | inceptionYear = "2023" 72 | url = "https ://github.com/Ahmad-Hamwi/TabSync/" 73 | licenses { 74 | license { 75 | name = "The Apache License, Version 2.0" 76 | url = "http://www.apache.org/licenses/LICENSE-2.0.txt" 77 | distribution = "http://www.apache.org/licenses/LICENSE-2.0.txt" 78 | } 79 | } 80 | developers { 81 | developer { 82 | id = "Ahmad-Hamwi" 83 | name = "Ahmad Hamwi" 84 | url = "https://github.com/Ahmad-Hamwi/" 85 | } 86 | } 87 | scm { 88 | url = "https://github.com/Ahmad-Hamwi/TabSync/" 89 | connection = "scm:git:git://github.com/Ahmad-Hamwi/TabSync.git" 90 | developerConnection = "scm:git:ssh://git@github.com/Ahmad-Hamwi/TabSync.git" 91 | } 92 | } 93 | } 94 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ### Android ### 2 | # Gradle files 3 | .gradle/ 4 | build/ 5 | 6 | # Local configuration file (sdk path, etc) 7 | local.properties 8 | 9 | # Log/OS Files 10 | *.log 11 | 12 | # Android Studio generated files and folders 13 | captures/ 14 | .externalNativeBuild/ 15 | .cxx/ 16 | *.apk 17 | output.json 18 | 19 | # IntelliJ 20 | *.iml 21 | .idea/ 22 | misc.xml 23 | deploymentTargetDropDown.xml 24 | render.experimental.xml 25 | 26 | # Keystore files 27 | *.jks 28 | *.keystore 29 | 30 | # Google Services (e.g. APIs or Firebase) 31 | google-services.json 32 | 33 | # Android Profiling 34 | *.hprof 35 | 36 | ### Android Patch ### 37 | gen-external-apklibs 38 | 39 | # Replacement of .externalNativeBuild directories introduced 40 | # with Android Studio 3.5. 41 | 42 | ### Groovy ### 43 | # .gitignore created from Groovy contributors in https://github.com/apache/groovy/blob/master/.gitignore 44 | 45 | user.gradle 46 | target/ 47 | out/ 48 | 49 | *.DS_Store 50 | *.class 51 | *.swp 52 | *~ 53 | 54 | .idea 55 | *.ipr 56 | *.iws 57 | .shelf 58 | 59 | .settings/ 60 | .classpath 61 | .project 62 | bin/ 63 | 64 | 65 | ### Java ### 66 | # Compiled class file 67 | 68 | # Log file 69 | 70 | # BlueJ files 71 | *.ctxt 72 | 73 | # Mobile Tools for Java (J2ME) 74 | .mtj.tmp/ 75 | 76 | # Package Files # 77 | *.jar 78 | *.war 79 | *.nar 80 | *.ear 81 | *.zip 82 | *.tar.gz 83 | *.rar 84 | 85 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 86 | hs_err_pid* 87 | replay_pid* 88 | 89 | ### Kotlin ### 90 | # Compiled class file 91 | 92 | # Log file 93 | 94 | # BlueJ files 95 | 96 | # Mobile Tools for Java (J2ME) 97 | 98 | # Package Files # 99 | 100 | # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml 101 | 102 | ### Gradle ### 103 | .gradle 104 | **/build/ 105 | !src/**/build/ 106 | 107 | # Ignore Gradle GUI config 108 | gradle-app.setting 109 | 110 | # Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored) 111 | !gradle-wrapper.jar 112 | 113 | # Avoid ignore Gradle wrappper properties 114 | !gradle-wrapper.properties 115 | 116 | # Cache of project 117 | .gradletasknamecache 118 | 119 | # Eclipse Gradle plugin generated files 120 | # Eclipse Core 121 | # JDT-specific (Eclipse Java Development Tools) 122 | 123 | ### Gradle Patch ### 124 | # Java heap dump 125 | 126 | ### AndroidStudio ### 127 | # Covers files to be ignored for android development using Android Studio. 128 | 129 | # Built application files 130 | *.ap_ 131 | *.aab 132 | 133 | # Files for the ART/Dalvik VM 134 | *.dex 135 | 136 | # Java class files 137 | 138 | # Generated files 139 | gen/ 140 | 141 | # Gradle files 142 | 143 | # Signing files 144 | .signing/ 145 | 146 | # Local configuration file (sdk path, etc) 147 | 148 | # Proguard folder generated by Eclipse 149 | proguard/ 150 | 151 | # Log Files 152 | 153 | # Android Studio 154 | /*/build/ 155 | /*/local.properties 156 | /*/out 157 | /*/*/build 158 | /*/*/production 159 | .navigation/ 160 | 161 | # Keystore files 162 | 163 | # Google Services (e.g. APIs or Firebase) 164 | # google-services.json 165 | 166 | # Android Patch 167 | 168 | # External native build folder generated in Android Studio 2.2 and later 169 | .externalNativeBuild 170 | 171 | # NDK 172 | obj/ 173 | 174 | # IntelliJ IDEA 175 | /out/ 176 | 177 | # User-specific configurations 178 | .idea/caches/ 179 | .idea/libraries/ 180 | .idea/shelf/ 181 | .idea/workspace.xml 182 | .idea/tasks.xml 183 | .idea/.name 184 | .idea/compiler.xml 185 | .idea/copyright/profiles_settings.xml 186 | .idea/encodings.xml 187 | .idea/misc.xml 188 | .idea/modules.xml 189 | .idea/scopes/scope_settings.xml 190 | .idea/dictionaries 191 | .idea/vcs.xml 192 | .idea/jsLibraryMappings.xml 193 | .idea/datasources.xml 194 | .idea/dataSources.ids 195 | .idea/sqlDataSources.xml 196 | .idea/dynamic.xml 197 | .idea/uiDesigner.xml 198 | .idea/assetWizardSettings.xml 199 | .idea/gradle.xml 200 | .idea/jarRepositories.xml 201 | .idea/navEditor.xml 202 | 203 | # Legacy Eclipse project files 204 | .cproject 205 | 206 | # Mobile Tools for Java (J2ME) 207 | 208 | # Package Files # 209 | 210 | # virtual machine crash logs (Reference: http://www.java.com/en/download/help/error_hotspot.xml) 211 | 212 | ## Plugin-specific files: 213 | 214 | # mpeltonen/sbt-idea plugin 215 | .idea_modules/ 216 | 217 | # JIRA plugin 218 | atlassian-ide-plugin.xml 219 | 220 | # Mongo Explorer plugin 221 | .idea/mongoSettings.xml 222 | 223 | # Crashlytics plugin (for Android Studio and IntelliJ) 224 | com_crashlytics_export_strings.xml 225 | crashlytics.properties 226 | crashlytics-build.properties 227 | fabric.properties 228 | 229 | ### AndroidStudio Patch ### 230 | 231 | !/gradle/wrapper/gradle-wrapper.jar 232 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /compose-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 | -------------------------------------------------------------------------------- /TabSync/src/main/java/com/ahmadhamwi/tabsync/TabbedListMediator.kt: -------------------------------------------------------------------------------- 1 | package com.ahmadhamwi.tabsync 2 | 3 | import androidx.recyclerview.widget.LinearLayoutManager 4 | import androidx.recyclerview.widget.LinearSmoothScroller 5 | import androidx.recyclerview.widget.RecyclerView 6 | import androidx.recyclerview.widget.RecyclerView.SmoothScroller 7 | import com.google.android.material.tabs.TabLayout 8 | import com.google.android.material.tabs.TabLayout.OnTabSelectedListener 9 | 10 | /** 11 | * This class is made to provide the ability to sync between RecyclerView's specific items with 12 | * TabLayout tabs. 13 | * 14 | * @param mRecyclerView The RecyclerView that is going to be synced with the TabLayout 15 | * @param mTabLayout The TabLayout that is going to be synced with the RecyclerView specific 16 | * items. 17 | * @param mIndices The indices of the RecyclerView's items that is going to be playing a 18 | * role of "check points" for the syncing operation. 19 | * @param mIsSmoothScroll Defines the ability of smooth scroll when clicking the tabs of the 20 | * TabLayout. 21 | */ 22 | class TabbedListMediator( 23 | private val mRecyclerView: RecyclerView, 24 | private val mTabLayout: TabLayout, 25 | private var mIndices: List, 26 | private var mIsSmoothScroll: Boolean = false 27 | ) { 28 | 29 | private var mIsAttached = false 30 | 31 | private var mRecyclerState = RecyclerView.SCROLL_STATE_IDLE 32 | private var mTabClickFlag = false 33 | 34 | private val smoothScroller: SmoothScroller = 35 | object : LinearSmoothScroller(mRecyclerView.context) { 36 | override fun getVerticalSnapPreference(): Int { 37 | return SNAP_TO_START 38 | } 39 | } 40 | 41 | private var tabViewCompositeClickListener: TabViewCompositeClickListener = 42 | TabViewCompositeClickListener(mTabLayout) 43 | 44 | /** 45 | * Calling this method will ensure that the data that has been provided to the mediator is 46 | * valid for use, and start syncing between the the RecyclerView and the TabLayout. 47 | * 48 | * Call this method when you have: 49 | * 1- provided a RecyclerView Adapter, 50 | * 2- provided a TabLayout with the appropriate number of tabs, 51 | * 3- provided indices of the recyclerview items that you are syncing the tabs with. (You 52 | * need to be providing indices of at most the number of Tabs inflated in the TabLayout.) 53 | */ 54 | fun attach() { 55 | mRecyclerView.adapter 56 | ?: throw RuntimeException("Cannot attach with no Adapter provided to RecyclerView") 57 | 58 | if (mTabLayout.tabCount == 0) 59 | throw RuntimeException("Cannot attach with no tabs provided to TabLayout") 60 | 61 | if (mIndices.size > mTabLayout.tabCount) 62 | throw RuntimeException("Cannot attach using more indices than the available tabs") 63 | 64 | notifyIndicesChanged() 65 | mIsAttached = true 66 | } 67 | 68 | /** 69 | * Calling this method will ensure to stop the synchronization between the RecyclerView and 70 | * the TabLayout. 71 | */ 72 | 73 | fun detach() { 74 | clearListeners() 75 | mIsAttached = false 76 | } 77 | 78 | /** 79 | * This method will ensure that the synchronization is up-to-date with the data provided. 80 | */ 81 | private fun reAttach() { 82 | detach() 83 | attach() 84 | } 85 | 86 | /** 87 | * Calling this method will 88 | */ 89 | fun updateMediatorWithNewIndices(newIndices: List): TabbedListMediator { 90 | mIndices = newIndices 91 | 92 | if (mIsAttached) { 93 | reAttach() 94 | } 95 | 96 | return this 97 | } 98 | 99 | /** 100 | * This method will ensure that any listeners that have been added by the mediator will be 101 | * removed, including the one listener from 102 | * @see TabbedListMediator#addOnViewOfTabClickListener((TabLayout.Tab, int) -> Unit) 103 | */ 104 | 105 | private fun clearListeners() { 106 | mRecyclerView.clearOnScrollListeners() 107 | for (i in 0 until mTabLayout.tabCount) { 108 | mTabLayout.getTabAt(i)!!.view.setOnClickListener(null) 109 | } 110 | for (i in tabViewCompositeClickListener.getListeners().indices) { 111 | tabViewCompositeClickListener.getListeners().toMutableList().removeAt(i) 112 | } 113 | mTabLayout.removeOnTabSelectedListener(onTabSelectedListener) 114 | mRecyclerView.removeOnScrollListener(onScrollListener) 115 | } 116 | 117 | /** 118 | * This method will attach the listeners required to make the synchronization possible. 119 | */ 120 | 121 | private fun notifyIndicesChanged() { 122 | tabViewCompositeClickListener.addListener { _, _ -> mTabClickFlag = true } 123 | tabViewCompositeClickListener.build() 124 | mTabLayout.addOnTabSelectedListener(onTabSelectedListener) 125 | mRecyclerView.addOnScrollListener(onScrollListener) 126 | } 127 | 128 | private val onTabSelectedListener = object : OnTabSelectedListener { 129 | override fun onTabSelected(tab: TabLayout.Tab) { 130 | 131 | if (!mTabClickFlag) return 132 | 133 | val position = tab.position 134 | 135 | if (mIsSmoothScroll) { 136 | smoothScroller.targetPosition = mIndices[position] 137 | mRecyclerView.layoutManager?.startSmoothScroll(smoothScroller) 138 | } else { 139 | (mRecyclerView.layoutManager as LinearLayoutManager?)?.scrollToPositionWithOffset( 140 | mIndices[position], 141 | 0 142 | ) 143 | mTabClickFlag = false 144 | } 145 | } 146 | 147 | override fun onTabUnselected(tab: TabLayout.Tab) {} 148 | override fun onTabReselected(tab: TabLayout.Tab) {} 149 | } 150 | 151 | private val onScrollListener = object : RecyclerView.OnScrollListener() { 152 | override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) { 153 | mRecyclerState = newState 154 | if (mIsSmoothScroll && newState == RecyclerView.SCROLL_STATE_IDLE) { 155 | mTabClickFlag = false 156 | } 157 | } 158 | 159 | override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) { 160 | super.onScrolled(recyclerView, dx, dy) 161 | if (mTabClickFlag) { 162 | return 163 | } 164 | 165 | val linearLayoutManager: LinearLayoutManager = 166 | recyclerView.layoutManager as LinearLayoutManager? 167 | ?: throw RuntimeException("No LinearLayoutManager attached to the RecyclerView.") 168 | 169 | var itemPosition = 170 | linearLayoutManager.findFirstCompletelyVisibleItemPosition() 171 | 172 | if (itemPosition == -1) { 173 | itemPosition = 174 | linearLayoutManager.findFirstVisibleItemPosition() 175 | } 176 | 177 | if (mRecyclerState == RecyclerView.SCROLL_STATE_DRAGGING 178 | || mRecyclerState == RecyclerView.SCROLL_STATE_SETTLING 179 | ) { 180 | for (i in mIndices.indices) { 181 | if (itemPosition == mIndices[i]) { 182 | if (!mTabLayout.getTabAt(i)!!.isSelected) { 183 | mTabLayout.getTabAt(i)!!.select() 184 | } 185 | if (linearLayoutManager.findLastCompletelyVisibleItemPosition() == mIndices[mIndices.size - 1]) { 186 | if (!mTabLayout.getTabAt(mIndices.size - 1)!!.isSelected) { 187 | mTabLayout.getTabAt(mIndices.size - 1)!!.select() 188 | } 189 | return 190 | } 191 | } 192 | } 193 | } 194 | } 195 | } 196 | 197 | /** 198 | * @return the state of the mediator, either attached or not. 199 | */ 200 | 201 | fun isAttached(): Boolean { 202 | return mIsAttached 203 | } 204 | 205 | /** 206 | * @return the state of the mediator, is smooth scrolling or not. 207 | */ 208 | 209 | fun isSmoothScroll(): Boolean { 210 | return mIsSmoothScroll 211 | } 212 | 213 | /** 214 | * @param smooth sets up the mediator with smooth scrolling 215 | */ 216 | 217 | fun setSmoothScroll(smooth: Boolean) { 218 | mIsSmoothScroll = smooth 219 | } 220 | 221 | /** 222 | * @param listener the listener the will applied on "the view" of the tab. This method is useful 223 | * when attaching a click listener on the tabs of the TabLayout. 224 | * Note that this method is REQUIRED in case of the need of adding a click listener on the view 225 | * of a tab layout. Since the mediator uses a click flag @see TabbedListMediator#mTabClickFlag 226 | * it's taking the place of the normal on click listener, and thus the need of the composite click 227 | * listener pattern, so adding listeners should be done using this method. 228 | */ 229 | 230 | fun addOnViewOfTabClickListener( 231 | listener: (tab: TabLayout.Tab, position: Int) -> Unit 232 | ) { 233 | tabViewCompositeClickListener.addListener(listener) 234 | if (mIsAttached) { 235 | notifyIndicesChanged() 236 | } 237 | } 238 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 |

TabSync

2 | 3 |

4 | License 5 | API 6 | Medium 7 | Medium 8 | 9 |
10 | 11 |

12 | Buy Me A Coffee 13 |

14 | 15 |

16 | A lightweight synchronizer between Android's Tabs and Lists! Available for 17 | Views 18 | and Jetpack Compose! 19 |

20 | 21 | ## Behaviour expected ## 22 | 23 | As you scroll through items, the corresponding tabs will be selected automatically, and vice-versa; selecting tabs by hand will correspond to a scroll of the list to the corresponding item. 24 | 25 | 26 | ![Mediator attached](https://media.giphy.com/media/T1cDzfvY3KzQn7kp5d/giphy.gif) 27 | ![Mediator attached with smooth scroll](https://media.giphy.com/media/MTS4wKN5EenEqgCPw7/giphy.gif) 28 | 29 | 30 | ## Available on the View System & Jetpack Compose ## 31 | 32 | - On Android's [View](https://github.com/Ahmad-Hamwi/TabSync#setup-for-the-view-system), synchronization is made between TabLayout and RecyclerView 33 | - Android's [Jetpack Compose](https://github.com/Ahmad-Hamwi/TabSync#setup-for-jetpack-compose), the synchronization is made between any LazeListState-based Composable (e.g. LazyColumn) and an index-based Composable (e.g. ScrollableTabRow). 34 | 35 | ## Articles ## 36 | - RecyclerView and TabLayout: Here's a [Medium Article](https://ahmad-hamwi.medium.com/synchronize-recyclerview-with-tablayout-3c5da4f3b18b) demonstrating the example in this repo step-by-step. 37 | - Jetpack compose: Here's the Jetpack Compose version [Medium Article](https://proandroiddev.com/jetpack-compose-synchronize-lazyliststate-with-scrollabletabrow-22ff1f8577dc) demonstrating the example in this repo step-by-step, and explaining how it's working. 38 | 39 | # Setup for the View system # 40 | 41 | Get the latest version via Maven Central: 42 | 43 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.github.ahmad-hamwi/tabsync/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.github.ahmad-hamwi/tabsync) 44 | 45 | Add Maven Central repository to your root build.gradle at the end of repositories: 46 | 47 | ```groovy 48 | allprojects { 49 | repositories { 50 | ... 51 | mavenCentral() 52 | } 53 | } 54 | ``` 55 | 56 | Then add the dependency 57 | 58 | ```groovy 59 | dependencies { 60 | implementation 'io.github.ahmad-hamwi:tabsync:1.0.1' 61 | } 62 | ``` 63 | 64 | ## Usage ## 65 | 66 | Here is a non-comprehensive guide to TabSync: 67 | 68 | Create a TabbedListMediator, and pass a RecyclerView, a TabLayout, and a list of RecyclerView's items indices that you wish to sync the tabs with. 69 | 70 | ```kotlin 71 | val mediator = TabbedListMediator( 72 | recyclerView, 73 | tabLayout, 74 | categories.indices.toList() 75 | ) 76 | ``` 77 | 78 | Make sure that RecyclerView and the TabLayout are instantiated with their data (adapter with its 79 | data for RecyclerView, and tabs for the TabLayout) and call the attach method. Note that the tabs' 80 | count must not be less than the number of the passed indices. 81 | 82 | ```kotlin 83 | method.attach() 84 | ``` 85 | 86 | To make the RecycerView smooth scrolls when pressing on a tab, you can pass a smooth scroll 87 | flag to the mediator: 88 | 89 | ```kotlin 90 | val mediator = TabbedListMediator( 91 | recyclerView, 92 | tabLayout, 93 | categories.indices.toList(), 94 | true 95 | ) 96 | ``` 97 | 98 | To stop syncing between the RecyclerView and the TabLayout, call the detach method: 99 | 100 | ```kotlin 101 | mediator.detach() 102 | ``` 103 | 104 | In case of refreshing the mediator (like data set has been changed), call the reAttach method: 105 | 106 | ```kotlin 107 | mediator.reAttach() 108 | ``` 109 | 110 | To get the smooth scroll flag, call the getter isSmoothScroll() 111 | 112 | ```kotlin 113 | val isSmoothScrolling = mediator.isSmoothScroll() 114 | ``` 115 | 116 | > **_NOTE:_** You don't have to pass the full indices of the list provided as shown in the previous example. You may want to provide a couple of indices from your recyclerview's items to sync the tabs with: 117 | > ```kotlin 118 | > val mediator = TabbedListMediator( 119 | > recyclerView, 120 | > tabLayout, 121 | > // Syncing the first tab with the item of index 0, the second one with the item of 122 | > // index 2, and the third with the item of index 4 123 | > mutableListOf(0, 2, 4) 124 | > ) 125 | > ``` 126 | 127 | # Setup for Jetpack Compose # 128 | 129 | Get the latest version via Maven Central: 130 | 131 | [![Maven Central](https://maven-badges.herokuapp.com/maven-central/io.github.ahmad-hamwi/tabsync-compose/badge.svg)](https://maven-badges.herokuapp.com/maven-central/io.github.ahmad-hamwi/tabsync-compose) 132 | 133 | Add Maven Central repository to your root build.gradle at the end of repositories: 134 | 135 | ```groovy 136 | allprojects { 137 | repositories { 138 | ... 139 | mavenCentral() 140 | } 141 | } 142 | ``` 143 | 144 | Then add the dependency 145 | 146 | ```groovy 147 | dependencies { 148 | implementation 'io.github.ahmad-hamwi:tabsync-compose:1.0.1' 149 | } 150 | ``` 151 | 152 | ## Usage ## 153 | 154 | Here is a non-comprehensive guide to TabSync Compose: 155 | 156 | Call ```lazyListTabSync``` composable, and pass the indices of your list that you wish to sync the tabs with, and assign the result to a destructuring declaration like so: 157 | 158 | ```kotlin 159 | @Composable 160 | fun MyComposable(items: List) { 161 | 162 | val (selectedTabIndex, setSelectedTabIndex, syncedListState) = lazyListTabSync(items.indices.toList()) 163 | 164 | } 165 | ``` 166 | 167 | Make sure that whenever you call ```lazyListTabSync```, the indices and the list's data are ready (above we're passing the state as a parameter). 168 | 169 | Note that the tabs' count must not be less than the number of the passed indices. 170 | 171 | 172 | - The ```selectedTabIndex``` is the state of a ```ScrollableTabRow``` that we're going to sync with. 173 | - The ```setSelectedTabIndex``` is a function that we should call whenever the state of the ```ScrollableTabRow``` changes directly, for example by pressing the tab. 174 | 175 | 176 | ``` kotlin 177 | ScrollableTabRow(selectedTabIndex) { 178 | categories.forEachIndexed { index, category -> 179 | Tab( 180 | selected = index == selectedTabIndex, 181 | onClick = { setSelectedTabIndex(index) }, 182 | ) 183 | } 184 | } 185 | ``` 186 | 187 | - The ```listState``` is a ```LazyListState``` which should be attached to a scrollable that accepts it, for example a ```LazyColumn```. 188 | 189 | ```kotlin 190 | LazyColumn(state = syncedListState) { 191 | ... 192 | } 193 | ``` 194 | 195 | ### Combined: ### 196 | 197 | (Full example can be found under [compose-app](https://github.com/Ahmad-Hamwi/TabSync/tree/master/compose-app)) 198 | 199 | ```kotlin 200 | @Composable 201 | fun MyComposable(categories: List) { 202 | val (selectedTabIndex, setSelectedTabIndex, syncedListState) = lazyListTabSync(categories.indices.toList()) 203 | 204 | Column { 205 | ScrollableTabRow(selectedTabIndex) { 206 | categories.forEachIndexed { index, category -> 207 | Tab( 208 | selected = index == selectedTabIndex, 209 | onClick = { setSelectedTabIndex(index) }, 210 | ... 211 | ) 212 | } 213 | } 214 | 215 | LazyColumn(state = syncedListState) { 216 | ... 217 | } 218 | } 219 | } 220 | ``` 221 | 222 | > **_NOTE:_** You don't have to pass the full indices of the list provided as shown in the previous example. You may want to provide a couple of indices from your list's items to sync the tabs with: 223 | > ```kotlin 224 | > // Syncing the first tab with the item of index 0, the second one with the item of 225 | > // index 2, and the third with the item of index 4 226 | > val (selectedTabIndex, setSelectedTabIndex, syncedListState) = lazyListTabSync(mutableListOf(0, 2, 4)) 227 | > ``` 228 | 229 | ### Additional arguments ### 230 | 231 | - By default, the list smooth scrolls when the selected tab changes, if you would like to stop the smooth scrolling, you can pass an optional ```smoothScroll``` flag: 232 | - ```tabsCount``` can be used to check for the viability of the synchronization with the tabs, the optimal case is that the number of tabs that we're syncing with is the same as the number of indices provided. (You cannot have a number of tabs lower than the number of indices provided, use ```tabsCount``` to make sure you're not doing that). 233 | - ```lazyListState``` can be used if you would like to provide your own state, the one coming from the destructuring declaration is going to be the same. 234 | 235 | ```kotlin 236 | @Composable 237 | fun MyComposable(items: List) { 238 | 239 | val (selectedTabIndex, setSelectedTabIndex, listState) = tabSyncMediator( 240 | mutableListOf(0, 2, 4), //Mandatory. The indices of lazy list items to sync the tabs with 241 | tabsCount = 3, //Optional. To check for viability of the synchronization with the tabs. Optimal when equals the count of syncedIndices. 242 | lazyListState = rememberLazyListState(), //Optional. To provide your own LazyListState. Defaults to rememberLazyListState(). 243 | smoothScroll = true, // Optional. To make the auto scroll smooth or not when clicking tabs. Defaults to true 244 | ) 245 | 246 | } 247 | ``` 248 | 249 | # Contributing # 250 | This library is made to help other developers out in their app developments, feel free to contribute by suggesting ideas and creating issues and PRs that would make this repository more helpful. 251 | 252 | # License 253 | 254 | Copyright (C) 2023 Ahmad Hamwi 255 | 256 | Licensed under the Apache License, Version 2.0 257 | -------------------------------------------------------------------------------- /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 2021 Ahmad Hamwi 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. --------------------------------------------------------------------------------