├── app
├── .gitignore
├── src
│ └── main
│ │ ├── res
│ │ ├── values
│ │ │ ├── strings.xml
│ │ │ ├── dimen.xml
│ │ │ ├── colors.xml
│ │ │ └── styles.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
│ │ ├── layout
│ │ │ ├── sample.xml
│ │ │ ├── activity_main.xml
│ │ │ ├── activity_multiple_categories.xml
│ │ │ ├── item_category_page.xml
│ │ │ └── category_item.xml
│ │ ├── mipmap-anydpi-v26
│ │ │ ├── ic_launcher.xml
│ │ │ └── ic_launcher_round.xml
│ │ ├── drawable-v24
│ │ │ └── ic_launcher_foreground.xml
│ │ └── drawable
│ │ │ └── ic_launcher_background.xml
│ │ ├── java
│ │ └── com
│ │ │ └── serapbercin
│ │ │ └── viewpager2example
│ │ │ ├── ViewPager2PageChangeCallback.kt
│ │ │ ├── adapter
│ │ │ ├── CategoryViewPagerAdapter.kt
│ │ │ ├── viewholder
│ │ │ │ ├── CategoryViewHolder.kt
│ │ │ │ └── DiffUtilCategoryItemViewHolder.kt
│ │ │ ├── CategoryAdapter.kt
│ │ │ └── CategoryDiffUtilAdapter.kt
│ │ │ ├── util
│ │ │ ├── Category.kt
│ │ │ └── DummyCategoryData.kt
│ │ │ ├── views
│ │ │ ├── DiffCategoriesActivity.kt
│ │ │ ├── PageTransformerCategoryActivity.kt
│ │ │ ├── CategoryActivity.kt
│ │ │ ├── CategoryFragmentActivity.kt
│ │ │ ├── InfinitiveCategoryPageActivity.kt
│ │ │ └── MultipleCategoriesPageActivity.kt
│ │ │ ├── transformer
│ │ │ └── ViewPager2PageTransformation.kt
│ │ │ └── MainActivity.kt
│ │ └── AndroidManifest.xml
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── README.md
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .idea
├── markdown-navigator
│ └── profiles_settings.xml
├── encodings.xml
├── vcs.xml
├── misc.xml
├── runConfigurations.xml
├── gradle.xml
├── codeStyles
│ └── Project.xml
└── markdown-navigator.xml
├── .gitignore
├── gradle.properties
├── gradlew.bat
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # androidx-ViewPager2
2 |
3 | https://proandroiddev.com/look-deep-into-viewpager2-13eb8e06e419
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | ViewPager2Example
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/serapbercin/androidx-ViewPager2/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.idea/markdown-navigator/profiles_settings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/serapbercin/androidx-ViewPager2/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/serapbercin/androidx-ViewPager2/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/serapbercin/androidx-ViewPager2/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/serapbercin/androidx-ViewPager2/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/serapbercin/androidx-ViewPager2/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/serapbercin/androidx-ViewPager2/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/serapbercin/androidx-ViewPager2/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/serapbercin/androidx-ViewPager2/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/serapbercin/androidx-ViewPager2/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/serapbercin/androidx-ViewPager2/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/dimen.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 20dp
4 | 30dp
5 | 50dp
6 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/sample.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sun Jul 21 14:41:35 CEST 2019
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-5.5-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_multiple_categories.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #008577
4 | #00574B
5 | #D81B60
6 | #93B3CC
7 | #F8F3F3
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/java/com/serapbercin/viewpager2example/ViewPager2PageChangeCallback.kt:
--------------------------------------------------------------------------------
1 | package com.serapbercin.viewpager2example
2 |
3 | import androidx.viewpager2.widget.ViewPager2
4 |
5 | class ViewPager2PageChangeCallback(private val listener: (Int) -> Unit) :
6 | ViewPager2.OnPageChangeCallback() {
7 |
8 | override fun onPageSelected(position: Int) {
9 | super.onPageSelected(position)
10 | when (position) {
11 | 0 -> listener.invoke(5)
12 | 6 -> listener.invoke(1)
13 | }
14 | }
15 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/serapbercin/viewpager2example/adapter/CategoryViewPagerAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.serapbercin.viewpager2example.adapter
2 |
3 | import androidx.fragment.app.FragmentActivity
4 | import androidx.viewpager2.adapter.FragmentStateAdapter
5 | import com.serapbercin.viewpager2example.views.PageAdapter
6 |
7 | class CategoryViewPagerAdapter(fm: FragmentActivity) : FragmentStateAdapter(fm) {
8 | override fun createFragment(position: Int) = PageAdapter.instance()
9 | override fun getItemCount(): Int = 2
10 | }
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/java/com/serapbercin/viewpager2example/util/Category.kt:
--------------------------------------------------------------------------------
1 | package com.serapbercin.viewpager2example.util
2 |
3 | class Category(var id: Int, var name: String) {
4 |
5 | override fun equals(obj: Any?): Boolean {
6 | if (this === obj) return true
7 | if (obj == null || javaClass != obj.javaClass) return false
8 | val category = obj as Category
9 | if (id != category.id) return false
10 | return name == category.name
11 |
12 | }
13 |
14 | override fun hashCode(): Int {
15 | var result = id
16 | result += name.hashCode()
17 | return result
18 | }
19 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/serapbercin/viewpager2example/util/DummyCategoryData.kt:
--------------------------------------------------------------------------------
1 | package com.serapbercin.viewpager2example.util
2 |
3 | object DummyCategoryData {
4 | val categories: List
5 | get() {
6 | val categories = mutableListOf()
7 | return categories.apply {
8 | add(Category(1, "Your Recording"))
9 | add(Category(2, "Film"))
10 | add(Category(3, "Series"))
11 | add(Category(4, "Kids"))
12 | add(Category(5, "Sport"))
13 | add(Category(6, "Information"))
14 | add(Category(7, "Planned"))
15 | add(Category(8, "All"))
16 | }
17 | }
18 | }
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/item_category_page.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/java/com/serapbercin/viewpager2example/adapter/viewholder/CategoryViewHolder.kt:
--------------------------------------------------------------------------------
1 | package com.serapbercin.viewpager2example.adapter.viewholder
2 |
3 | import android.view.LayoutInflater
4 | import android.view.View
5 | import android.view.ViewGroup
6 | import androidx.recyclerview.widget.RecyclerView
7 | import com.serapbercin.viewpager2example.R
8 | import com.serapbercin.viewpager2example.util.Category
9 | import kotlinx.android.synthetic.main.category_item.view.*
10 |
11 | class CategoryViewHolder constructor(itemView: View) :
12 | RecyclerView.ViewHolder(itemView) {
13 | constructor(parent: ViewGroup) :
14 | this(LayoutInflater.from(parent.context).inflate(R.layout.category_item, parent, false))
15 |
16 | fun bind(category: Category) {
17 | itemView.categoryName.text = category.name
18 | }
19 | }
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/main/java/com/serapbercin/viewpager2example/views/DiffCategoriesActivity.kt:
--------------------------------------------------------------------------------
1 | package com.serapbercin.viewpager2example.views
2 |
3 | import android.os.Bundle
4 | import androidx.appcompat.app.AppCompatActivity
5 | import com.serapbercin.viewpager2example.R
6 | import com.serapbercin.viewpager2example.adapter.CategoryDiffUtilAdapter
7 | import com.serapbercin.viewpager2example.util.DummyCategoryData.categories
8 | import kotlinx.android.synthetic.main.activity_main.*
9 |
10 | class DiffCategoriesActivity : AppCompatActivity() {
11 |
12 | override fun onCreate(savedInstanceState: Bundle?) {
13 | super.onCreate(savedInstanceState)
14 | setContentView(R.layout.activity_main)
15 |
16 | val adapter = CategoryDiffUtilAdapter()
17 | viewPager2.adapter = adapter
18 |
19 | adapter.submitList(categories)
20 | }
21 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/serapbercin/viewpager2example/adapter/viewholder/DiffUtilCategoryItemViewHolder.kt:
--------------------------------------------------------------------------------
1 | package com.serapbercin.viewpager2example.adapter.viewholder
2 |
3 | import android.view.LayoutInflater
4 | import android.view.View
5 | import android.view.ViewGroup
6 | import androidx.recyclerview.widget.RecyclerView
7 | import com.serapbercin.viewpager2example.R
8 | import com.serapbercin.viewpager2example.util.Category
9 | import kotlinx.android.synthetic.main.category_item.view.*
10 |
11 | class DiffUtilCategoryItemViewHolder constructor(itemView: View) :
12 | RecyclerView.ViewHolder(itemView) {
13 | constructor(parent: ViewGroup) :
14 | this(LayoutInflater.from(parent.context).inflate(R.layout.category_item, parent, false))
15 |
16 | fun bind(category: Category) {
17 | itemView.categoryName.text = category.name
18 | }
19 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/serapbercin/viewpager2example/transformer/ViewPager2PageTransformation.kt:
--------------------------------------------------------------------------------
1 | package com.serapbercin.viewpager2example.transformer
2 |
3 | import android.view.View
4 | import androidx.viewpager2.widget.ViewPager2
5 | import kotlin.math.abs
6 | import kotlin.math.max
7 |
8 | class ViewPager2PageTransformation : ViewPager2.PageTransformer {
9 |
10 | override fun transformPage(page: View, position: Float) {
11 | val absPos = abs(position)
12 | page.apply {
13 | translationY = absPos * 500f
14 | translationX = absPos * 500f
15 | scaleX = 1f
16 | scaleY = 1f
17 | }
18 |
19 | when {
20 | position < -1 ->
21 | page.alpha = 0.1f
22 | position <= 1 -> {
23 | page.alpha = max(0.2f, 1 - abs(position))
24 | }
25 | else -> page.alpha = 0.1f
26 | }
27 | }
28 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/serapbercin/viewpager2example/adapter/CategoryAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.serapbercin.viewpager2example.adapter
2 |
3 | import android.view.ViewGroup
4 | import androidx.recyclerview.widget.RecyclerView
5 | import com.serapbercin.viewpager2example.adapter.viewholder.CategoryViewHolder
6 | import com.serapbercin.viewpager2example.util.Category
7 |
8 | class CategoryAdapter : RecyclerView.Adapter() {
9 | var list: List = listOf()
10 |
11 | override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder {
12 | return CategoryViewHolder(parent)
13 | }
14 |
15 | override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) {
16 | holder.bind(list[position])
17 | }
18 |
19 | fun setItem(list: List) {
20 | this.list = list
21 | notifyDataSetChanged()
22 | }
23 |
24 | override fun getItemCount(): Int = list.size
25 | }
26 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 | # 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 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/serapbercin/viewpager2example/views/PageTransformerCategoryActivity.kt:
--------------------------------------------------------------------------------
1 | package com.serapbercin.viewpager2example.views
2 |
3 | import android.os.Bundle
4 | import androidx.appcompat.app.AppCompatActivity
5 | import com.serapbercin.viewpager2example.R
6 | import com.serapbercin.viewpager2example.adapter.CategoryAdapter
7 | import com.serapbercin.viewpager2example.transformer.ViewPager2PageTransformation
8 | import com.serapbercin.viewpager2example.util.DummyCategoryData.categories
9 | import kotlinx.android.synthetic.main.activity_main.*
10 | import kotlinx.android.synthetic.main.activity_main.view.*
11 |
12 | class PageTransformerCategoryActivity : AppCompatActivity() {
13 |
14 | override fun onCreate(savedInstanceState: Bundle?) {
15 | super.onCreate(savedInstanceState)
16 | setContentView(R.layout.activity_main)
17 |
18 | val adapter = CategoryAdapter()
19 | viewPager2.adapter = adapter
20 | adapter.setItem(categories)
21 | with(viewPager2) {
22 | clipToPadding = false
23 | clipChildren = false
24 | offscreenPageLimit = 3
25 | }
26 |
27 | viewPager2.setPageTransformer(ViewPager2PageTransformation())
28 | }
29 | }
--------------------------------------------------------------------------------
/app/src/main/res/layout/category_item.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
19 |
20 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/java/com/serapbercin/viewpager2example/views/CategoryActivity.kt:
--------------------------------------------------------------------------------
1 | package com.serapbercin.viewpager2example.views
2 |
3 | import android.os.Bundle
4 | import androidx.appcompat.app.AppCompatActivity
5 | import androidx.viewpager2.widget.ViewPager2
6 | import com.serapbercin.viewpager2example.ORIENTATION_TYPE
7 | import com.serapbercin.viewpager2example.R
8 | import com.serapbercin.viewpager2example.adapter.CategoryAdapter
9 | import com.serapbercin.viewpager2example.util.DummyCategoryData.categories
10 | import kotlinx.android.synthetic.main.activity_main.*
11 |
12 | class CategoryActivity : AppCompatActivity() {
13 |
14 | override fun onCreate(savedInstanceState: Bundle?) {
15 | super.onCreate(savedInstanceState)
16 | setContentView(R.layout.activity_main)
17 |
18 |
19 | val orientationType = intent.getStringExtra(ORIENTATION_TYPE)
20 |
21 | val adapter = CategoryAdapter()
22 | viewPager2.adapter = adapter
23 |
24 | if (orientationType == ORIENTATION_VERTICAL) {
25 | viewPager2.orientation = ViewPager2.ORIENTATION_VERTICAL
26 | }
27 | adapter.setItem(categories)
28 | }
29 |
30 | companion object {
31 | const val ORIENTATION_HORIZONTAL = "ORIENTATION_HORIZONTAL"
32 | const val ORIENTATION_VERTICAL = "ORIENTATION_VERTICAL"
33 | }
34 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/serapbercin/viewpager2example/adapter/CategoryDiffUtilAdapter.kt:
--------------------------------------------------------------------------------
1 | package com.serapbercin.viewpager2example.adapter
2 |
3 | import android.view.ViewGroup
4 | import androidx.recyclerview.widget.DiffUtil
5 | import androidx.recyclerview.widget.ListAdapter
6 | import com.serapbercin.viewpager2example.adapter.viewholder.DiffUtilCategoryItemViewHolder
7 | import com.serapbercin.viewpager2example.util.Category
8 |
9 | class CategoryDiffUtilAdapter : ListAdapter(
10 | DIFF_UTIL_ITEM_CALLBACK
11 | ) {
12 |
13 | override fun onCreateViewHolder(
14 | parent: ViewGroup,
15 | viewType: Int
16 | ): DiffUtilCategoryItemViewHolder = DiffUtilCategoryItemViewHolder(parent)
17 |
18 |
19 | override fun onBindViewHolder(holder: DiffUtilCategoryItemViewHolder, position: Int) {
20 | val item = getItem(position)
21 | holder.bind(item)
22 | }
23 |
24 | companion object {
25 | private val DIFF_UTIL_ITEM_CALLBACK =
26 | object : DiffUtil.ItemCallback() {
27 | override fun areItemsTheSame(oldItem: Category, newItem: Category) =
28 | oldItem.id == newItem.id
29 |
30 | override fun areContentsTheSame(oldItem: Category, newItem: Category) =
31 | oldItem == newItem
32 | }
33 | }
34 | }
35 |
--------------------------------------------------------------------------------
/app/src/main/java/com/serapbercin/viewpager2example/views/CategoryFragmentActivity.kt:
--------------------------------------------------------------------------------
1 | package com.serapbercin.viewpager2example.views
2 |
3 | import android.os.Bundle
4 | import android.view.LayoutInflater
5 | import android.view.View
6 | import android.view.ViewGroup
7 | import androidx.fragment.app.Fragment
8 | import androidx.fragment.app.FragmentActivity
9 | import androidx.recyclerview.widget.RecyclerView
10 | import com.serapbercin.viewpager2example.R
11 | import com.serapbercin.viewpager2example.adapter.CategoryViewPagerAdapter
12 | import kotlinx.android.synthetic.main.activity_main.*
13 |
14 | class CategoryFragmentActivity : FragmentActivity() {
15 |
16 | override fun onCreate(savedInstanceState: Bundle?) {
17 | super.onCreate(savedInstanceState)
18 | setContentView(R.layout.activity_main)
19 | viewPager2.adapter = createFragmentStateAdapter()
20 | }
21 |
22 | private fun createFragmentStateAdapter(): RecyclerView.Adapter<*> =
23 | CategoryViewPagerAdapter(this)
24 | }
25 |
26 | class PageAdapter : Fragment() {
27 |
28 | override fun onCreateView(
29 | inflater: LayoutInflater,
30 | container: ViewGroup?,
31 | savedInstanceState: Bundle?
32 | ): View? {
33 | return inflater.inflate(R.layout.item_category_page, container, false)
34 | }
35 |
36 | companion object {
37 | fun instance() = PageAdapter()
38 | }
39 | }
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 | apply plugin: 'kotlin-android-extensions'
3 | apply plugin: 'kotlin-android'
4 |
5 | android {
6 | compileSdkVersion 28
7 | defaultConfig {
8 | applicationId "com.serapbercin.viewpager2example"
9 | minSdkVersion 15
10 | targetSdkVersion 28
11 | versionCode 1
12 | versionName "1.0"
13 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
14 | }
15 | buildTypes {
16 | release {
17 | minifyEnabled false
18 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
19 | }
20 | }
21 | }
22 |
23 | dependencies {
24 | implementation fileTree(dir: 'libs', include: ['*.jar'])
25 | implementation 'androidx.cardview:cardview:1.0.0'
26 | implementation 'androidx.viewpager2:viewpager2:1.0.0-beta03'
27 | implementation 'androidx.appcompat:appcompat:1.0.2'
28 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
29 | implementation 'com.google.android.material:material:1.0.0'
30 | implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
31 |
32 | testImplementation 'junit:junit:4.12'
33 | androidTestImplementation 'androidx.test:runner:1.2.0'
34 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
35 | compile "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
36 | }
37 | repositories {
38 | mavenCentral()
39 | }
40 |
--------------------------------------------------------------------------------
/app/src/main/java/com/serapbercin/viewpager2example/views/InfinitiveCategoryPageActivity.kt:
--------------------------------------------------------------------------------
1 | package com.serapbercin.viewpager2example.views
2 |
3 | import android.os.Bundle
4 | import androidx.appcompat.app.AppCompatActivity
5 | import com.serapbercin.viewpager2example.R
6 | import com.serapbercin.viewpager2example.ViewPager2PageChangeCallback
7 | import com.serapbercin.viewpager2example.adapter.CategoryAdapter
8 | import com.serapbercin.viewpager2example.util.Category
9 | import kotlinx.android.synthetic.main.activity_main.*
10 |
11 | class InfinitiveCategoryPageActivity : AppCompatActivity() {
12 | private lateinit var viewPager2PageChangeCallback: ViewPager2PageChangeCallback
13 |
14 | override fun onCreate(savedInstanceState: Bundle?) {
15 | super.onCreate(savedInstanceState)
16 | setContentView(R.layout.activity_main)
17 |
18 |
19 | val list: MutableList = mutableListOf()
20 |
21 | list.add(Category(1, "Your Recording"))
22 | list.add(Category(1, "Your Recording"))
23 | list.add(Category(2, "Film"))
24 | list.add(Category(3, "Series"))
25 | list.add(Category(4, "Kids"))
26 | list.add(Category(5, "Sport"))
27 | list.add(Category(5, "Sport"))
28 |
29 | val adapter = CategoryAdapter()
30 | viewPager2.adapter = adapter
31 | adapter.setItem(list)
32 |
33 | viewPager2.currentItem = 1
34 |
35 | viewPager2PageChangeCallback = ViewPager2PageChangeCallback {
36 | viewPager2.post {
37 | viewPager2.setCurrentItem(it, false)
38 | }
39 | }
40 | viewPager2.registerOnPageChangeCallback(viewPager2PageChangeCallback)
41 | }
42 |
43 | override fun onDestroy() {
44 | super.onDestroy()
45 | viewPager2.unregisterOnPageChangeCallback(viewPager2PageChangeCallback)
46 | }
47 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/serapbercin/viewpager2example/views/MultipleCategoriesPageActivity.kt:
--------------------------------------------------------------------------------
1 | package com.serapbercin.viewpager2example.views
2 |
3 | import android.os.Bundle
4 | import androidx.appcompat.app.AppCompatActivity
5 | import androidx.core.view.ViewCompat
6 | import androidx.viewpager2.widget.ViewPager2
7 | import androidx.viewpager2.widget.ViewPager2.ORIENTATION_HORIZONTAL
8 | import com.serapbercin.viewpager2example.R
9 | import com.serapbercin.viewpager2example.adapter.CategoryAdapter
10 | import com.serapbercin.viewpager2example.util.DummyCategoryData.categories
11 | import kotlinx.android.synthetic.main.activity_multiple_categories.*
12 |
13 | class MultipleCategoriesPageActivity : AppCompatActivity() {
14 |
15 | override fun onCreate(savedInstanceState: Bundle?) {
16 | super.onCreate(savedInstanceState)
17 | setContentView(R.layout.activity_multiple_categories)
18 |
19 | val adapter = CategoryAdapter()
20 | viewPager2.adapter = adapter
21 | adapter.setItem(categories)
22 |
23 | with(viewPager2) {
24 | clipToPadding = false
25 | clipChildren = false
26 | offscreenPageLimit = 3
27 | }
28 |
29 | val pageMarginPx = resources.getDimensionPixelOffset(R.dimen.pageMargin)
30 | val offsetPx = resources.getDimensionPixelOffset(R.dimen.offset)
31 | viewPager2.setPageTransformer { page, position ->
32 | val viewPager = page.parent.parent as ViewPager2
33 | val offset = position * -(2 * offsetPx + pageMarginPx)
34 | if (viewPager.orientation == ORIENTATION_HORIZONTAL) {
35 | if (ViewCompat.getLayoutDirection(viewPager) == ViewCompat.LAYOUT_DIRECTION_RTL) {
36 | page.translationX = -offset
37 | } else {
38 | page.translationX = offset
39 | }
40 | } else {
41 | page.translationY = offset
42 | }
43 | }
44 | }
45 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 | xmlns:android
14 |
15 | ^$
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 | xmlns:.*
25 |
26 | ^$
27 |
28 |
29 | BY_NAME
30 |
31 |
32 |
33 |
34 |
35 |
36 | .*:id
37 |
38 | http://schemas.android.com/apk/res/android
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 | .*:name
48 |
49 | http://schemas.android.com/apk/res/android
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 | name
59 |
60 | ^$
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 | style
70 |
71 | ^$
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 | .*
81 |
82 | ^$
83 |
84 |
85 | BY_NAME
86 |
87 |
88 |
89 |
90 |
91 |
92 | .*
93 |
94 | http://schemas.android.com/apk/res/android
95 |
96 |
97 | ANDROID_ATTRIBUTE_ORDER
98 |
99 |
100 |
101 |
102 |
103 |
104 | .*
105 |
106 | .*
107 |
108 |
109 | BY_NAME
110 |
111 |
112 |
113 |
114 |
115 |
116 |
--------------------------------------------------------------------------------
/app/src/main/java/com/serapbercin/viewpager2example/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.serapbercin.viewpager2example
2 |
3 | import android.R
4 | import android.app.ListActivity
5 | import android.os.Bundle
6 | import android.content.Intent
7 | import android.view.View
8 | import android.widget.ListView
9 | import android.widget.SimpleAdapter
10 | import com.serapbercin.viewpager2example.views.*
11 | import com.serapbercin.viewpager2example.views.CategoryActivity.Companion.ORIENTATION_HORIZONTAL
12 | import com.serapbercin.viewpager2example.views.CategoryActivity.Companion.ORIENTATION_VERTICAL
13 |
14 | const val ORIENTATION_TYPE = "ORIENTATION_TYPE"
15 |
16 | class MainActivity : ListActivity() {
17 |
18 | override fun onCreate(savedInstanceState: Bundle?) {
19 | super.onCreate(savedInstanceState)
20 |
21 | listAdapter = SimpleAdapter(
22 | this, getData(),
23 | R.layout.simple_list_item_1, arrayOf("title"),
24 | intArrayOf(R.id.text1)
25 | )
26 | }
27 |
28 | private fun getData(): List