Last active
July 27, 2022 11:17
-
-
Save d4vidi/bcaf9f46c772ae8074144f4f63c13542 to your computer and use it in GitHub Desktop.
A RecyclerView decorator that lets your easily turn a PagerSnapHelper from a provider of a simple full-screen "ViewPager" onto the common fancy cards carousel (such as duo lingo's: http://i.imgur.com/UXpVigQ.gif).
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
package com.d4vidi | |
import android.content.Context | |
import android.graphics.Rect | |
import android.support.annotation.Px | |
import android.support.v7.widget.RecyclerView | |
import android.view.View | |
/** | |
* A [RecyclerView decorator][RecyclerView.ItemDecoration] which, if applied over a recycler view | |
* with a [PagerSnapHelper][android.support.v7.widget.PagerSnapHelper] set up (i.e. that behaves like | |
* a full-screen [ViewPager][android.support.v4.view.ViewPager]), allows for easily setting up gaps | |
* between the RV's items as typically done in a cards carousel, all-the-while keeping the items | |
* properly snapped to the screen's center. | |
* | |
* **Typical usage:** | |
* | |
* ``` | |
* // Get each item to take exactly 80% of the screen's width. | |
* // In addition, enable a sneak-preview of neighbouring (unsnapped) items of 1% of their width. | |
* val cardWidthPixels = activity.resources.displayMetrics.widthPixels * 0.80f | |
* recyclerView.addItemDecoration(RVPagerSnapFancyDecorator(activity, cardWidthPixels, 0.01f)) | |
* ``` | |
*/ | |
open class RVPagerSnapFancyDecorator : RecyclerView.ItemDecoration { | |
private val mInterItemsGap: Int | |
private val mNetOneSidedGap: Int | |
constructor(context: Context, @Px itemWidth: Int, itemPeekingPercent: Float = .035f) | |
: this(context.resources.displayMetrics.widthPixels, itemWidth, itemPeekingPercent) | |
constructor(@Px totalWidth: Int, @Px itemWidth: Int, itemPeekingPercent: Float = .035f) { | |
val cardPeekingWidth = (itemWidth * itemPeekingPercent + .5f).toInt() | |
mInterItemsGap = (totalWidth - itemWidth) / 2 | |
mNetOneSidedGap = mInterItemsGap / 2 - cardPeekingWidth | |
} | |
override fun getItemOffsets(outRect: Rect?, view: View?, recyclerView: RecyclerView?, state: RecyclerView.State?) { | |
val index = recyclerView!!.getChildAdapterPosition(view) | |
val isFirstItem = isFirstItem(index) | |
val isLastItem = isLastItem(index, recyclerView) | |
val leftInset = if (isFirstItem) mInterItemsGap else mNetOneSidedGap | |
val rightInset = if (isLastItem) mInterItemsGap else mNetOneSidedGap | |
outRect!!.set(leftInset, 0, rightInset, 0) | |
} | |
private fun isFirstItem(index: Int) = index == 0 | |
private fun isLastItem(index: Int, recyclerView: RecyclerView) = index == recyclerView.adapter.itemCount - 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @vipin5605,
You can actually use the Kotlin file in Android studio as is.
You then reference it from your Java code as you would a normal Java file.