This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
dependencies { | |
... | |
implementation "androidx.viewpager2:viewpager2:1.0.0" | |
... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
tools:context=".MainActivity"> | |
<androidx.viewpager2.widget.ViewPager2 | |
android:id="@+id/viewPager2" | |
android:layout_width="match_parent" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Item(val color: Int) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout | |
xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:tools="http://schemas.android.com/tools" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<ImageView | |
android:id="@+id/item" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PageAdapter(private val items: List<Item>) : RecyclerView.Adapter<PageAdapter.ViewHolder>() { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | |
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)) | |
} | |
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | |
holder.item.setBackgroundColor(items[position].color) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun getItemCount(): Int { | |
if (items.size <= 1) { | |
return items.size | |
} | |
val period = Int.MAX_VALUE / 2 / items.size | |
return period * items.size * 2 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PageAdapter(private val items: List<Item>) : RecyclerView.Adapter<PageAdapter.ViewHolder>() { | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | |
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.item, parent, false)) | |
} | |
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | |
holder.item.setBackgroundColor(items[getRealPosition(position)].color) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
initViewPager() | |
} | |
private fun initViewPager() { | |
val pageAdapter = PageAdapter(listOf(Item(Color.RED), Item(Color.BLUE), Item(Color.GREEN))) | |
viewPager2.offscreenPageLimit = 2 | |
viewPager2.orientation = ViewPager2.ORIENTATION_VERTICAL |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PageTransformer : ViewPager2.PageTransformer { | |
companion object { | |
private const val SCALE = 0.9f | |
} | |
override fun transformPage(view: View, position: Float) { | |
val translationOffset = calculateTranslationOffset(view, position) | |
with(view) { | |
when { | |
position < -2.5 -> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private fun initViewPager() { | |
val pageAdapter = PageAdapter(listOf(Item(Color.RED), Item(Color.BLUE), Item(Color.GREEN))) | |
viewPager2.setPageTransformer(PageTransformer()) | |
viewPager2.offscreenPageLimit = 2 | |
viewPager2.orientation = ViewPager2.ORIENTATION_VERTICAL | |
viewPager2.adapter = pageAdapter | |
viewPager2.currentItem = pageAdapter.getMiddlePosition() | |
} |
OlderNewer