Skip to content

Instantly share code, notes, and snippets.

@amyu
Created August 28, 2018 09:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amyu/90393cd784153b7361d7f6d924772fba to your computer and use it in GitHub Desktop.
Save amyu/90393cd784153b7361d7f6d924772fba to your computer and use it in GitHub Desktop.
class StackCardLayoutManager : RecyclerView.LayoutManager() {
private val children: List<View>
get() = (0 until childCount).map { getChildAt(it) ?: throw NullPointerException() }
override fun generateDefaultLayoutParams(): RecyclerView.LayoutParams =
RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.MATCH_PARENT)
//初回のViewレイアウト
override fun onLayoutChildren(
recycler: RecyclerView.Recycler,
state: RecyclerView.State
) {
detachAndScrapAttachedViews(recycler)
for (i in 0 until state.itemCount) {
val view = recycler.getViewForPosition(i)
measureChild(view, 0, 0)
addView(view)
val left = 0
val top = (view.measuredHeight * i * 0.35).toInt()
val right = view.measuredWidth
val bottom = top + view.measuredHeight
layoutDecorated(view, left, top, right, bottom)
view.setTag(InitializedPosition.TOP.key, top)
}
}
override fun canScrollHorizontally(): Boolean = false
override fun canScrollVertically(): Boolean = true
override fun scrollVerticallyBy(
dy: Int,
recycler: RecyclerView.Recycler,
state: RecyclerView.State
): Int = dy.also { dy ->
if (childCount == 0) {
return@also
}
children.forEachIndexed { index, view ->
val initializedTop = view.getTag(InitializedPosition.TOP.key) as Int
val left = 0
val top = Math.min(Math.max((view.top - (dy)), (index * 30)), initializedTop)
val right = view.measuredWidth
val bottom = top + view.measuredHeight
layoutDecorated(view, left, top, right, bottom)
}
}
override fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler?, state: RecyclerView.State?): Int = 0
private enum class InitializedPosition(val key: Int) {
TOP(R.integer.top)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment