Solusi dengan ViewPager2 baru
Saat ini Anda harus mempertimbangkan untuk menggunakan ViewPager2 yang "menggantikan ViewPager, menangani sebagian besar masalah pendahulunya" :
- Berdasarkan RecyclerView
- Dukungan tata letak RTL (kanan-ke-kiri)
- Dukungan orientasi vertikal
- Dukungan Fragmen yang andal (termasuk menangani perubahan pada koleksi Fragmen yang mendasarinya)
- Animasi perubahan set data (termasuk
DiffUtil
dukungan)
Hasil
Kode
Di Aktivitas / Fragmen Anda, siapkan ViewPager2:
// MyRecyclerViewAdapter is an standard RecyclerView.Adapter :)
viewPager2.adapter = MyRecyclerViewAdapter()
// You need to retain one page on each side so that the next and previous items are visible
viewPager2.offscreenPageLimit = 1
// Add a PageTransformer that translates the next and previous items horizontally
// towards the center of the screen, which makes them visible
val nextItemVisiblePx = resources.getDimension(R.dimen.viewpager_next_item_visible)
val currentItemHorizontalMarginPx = resources.getDimension(R.dimen.viewpager_current_item_horizontal_margin)
val pageTranslationX = nextItemVisiblePx + currentItemHorizontalMarginPx
val pageTransformer = ViewPager2.PageTransformer { page: View, position: Float ->
page.translationX = -pageTranslationX * position
// Next line scales the item's height. You can remove it if you don't want this effect
page.scaleY = 1 - (0.25f * abs(position))
// If you want a fading effect uncomment the next line:
// page.alpha = 0.25f + (1 - abs(position))
}
viewPager2.setPageTransformer(pageTransformer)
// The ItemDecoration gives the current (centered) item horizontal margin so that
// it doesn't occupy the whole screen width. Without it the items overlap
val itemDecoration = HorizontalMarginItemDecoration(
context,
R.dimen.viewpager_current_item_horizontal_margin
)
viewPager2.addItemDecoration(itemDecoration)
Tambahkan HorizontalMarginItemDecoration
, yang sepele ItemDecoration
:
/**
* Adds margin to the left and right sides of the RecyclerView item.
* Adapted from https://stackoverflow.com/a/27664023/4034572
* @param horizontalMarginInDp the margin resource, in dp.
*/
class HorizontalMarginItemDecoration(context: Context, @DimenRes horizontalMarginInDp: Int) :
RecyclerView.ItemDecoration() {
private val horizontalMarginInPx: Int =
context.resources.getDimension(horizontalMarginInDp).toInt()
override fun getItemOffsets(
outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State
) {
outRect.right = horizontalMarginInPx
outRect.left = horizontalMarginInPx
}
}
Tambahkan dimensi yang mengontrol seberapa banyak item sebelumnya / berikutnya terlihat, dan margin horizontal item saat ini:
<dimen name="viewpager_next_item_visible">26dp</dimen>
<dimen name="viewpager_current_item_horizontal_margin">42dp</dimen>
Terakhir tambahkan ViewPager2
ke tata letak Anda:
<androidx.viewpager2.widget.ViewPager2
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Satu hal penting: ViewPager2
item harus memiliki layout_height="match_parent"
(jika tidak maka akan melempar IllegalStateException), jadi Anda harus melakukan sesuatu seperti:
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent" <-- this!
app:cardCornerRadius="8dp"
app:cardUseCompatPadding="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="280dp">
<!-- ... -->
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Contoh PageTransformer
Google telah menambahkan panduan di ViewPager2 yang memiliki 2 PageTransformer
implementasi yang dapat Anda gunakan sebagai inspirasi: https://developer.android.com/training/animation/screen-slide-2
Tentang ViewPager2 baru
setPageMargin(-100)
berfungsi dengan benar?