Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support auto skeleton item size calculation. #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.RecyclerView
import com.faltenreich.skeletonlayout.recyclerview.SkeletonRecyclerView

private const val LIST_ITEM_COUNT_DEFAULT = 3
private const val LIST_ITEM_COUNT_AUTO = 0

/**
* Creates a new Skeleton that wraps the given View in a SkeletonLayout
Expand Down Expand Up @@ -61,7 +61,7 @@ fun View.createSkeleton(
@JvmOverloads
fun RecyclerView.applySkeleton(
@LayoutRes listItemLayoutResId: Int,
itemCount: Int = LIST_ITEM_COUNT_DEFAULT,
itemCount: Int = LIST_ITEM_COUNT_AUTO,
@ColorInt maskColor: Int = ContextCompat.getColor(context, SkeletonLayout.DEFAULT_MASK_COLOR),
cornerRadius: Float = SkeletonLayout.DEFAULT_CORNER_RADIUS,
showShimmer: Boolean = SkeletonLayout.DEFAULT_SHIMMER_SHOW,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.faltenreich.skeletonlayout.recyclerview

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.ColorInt
import androidx.annotation.LayoutRes
Expand All @@ -9,24 +10,61 @@ import com.faltenreich.skeletonlayout.SkeletonLayout
import com.faltenreich.skeletonlayout.createSkeleton

internal class SkeletonRecyclerViewAdapter(
@LayoutRes private val layoutResId: Int,
private val itemCount: Int,
@ColorInt private val maskColor: Int,
private val cornerRadius: Float,
private val showShimmer: Boolean,
@ColorInt private val shimmerColor: Int,
private val shimmerDurationInMillis: Long
@LayoutRes private val layoutResId: Int,
private var itemCount: Int,
@ColorInt private val maskColor: Int,
private val cornerRadius: Float,
private val showShimmer: Boolean,
@ColorInt private val shimmerColor: Int,
private val shimmerDurationInMillis: Long
) : RecyclerView.Adapter<SkeletonRecyclerViewHolder>() {

private var measured = itemCount != 0

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SkeletonRecyclerViewHolder {
val originView = LayoutInflater.from(parent.context).inflate(layoutResId, parent, false)
val skeleton = originView.createSkeleton(maskColor, cornerRadius, showShimmer, shimmerColor, shimmerDurationInMillis) as SkeletonLayout
skeleton.layoutParams = originView.layoutParams
skeleton.showSkeleton()
// Automatic calc the skeleton item count
if (!measured) {
var itemHeight = skeleton.height
var parentHeight = parent.height
val calcItemCount = {
if (itemHeight != 0 && parentHeight != 0) {
measured = true
itemCount = parentHeight / itemHeight
}
}
val listener = object : View.OnLayoutChangeListener {
override fun onLayoutChange(v: View?, left: Int, top: Int, right: Int, bottom: Int, oldLeft: Int, oldTop: Int, oldRight: Int, oldBottom: Int) {
if (top != bottom) {
skeleton.removeOnLayoutChangeListener(this)
when (v) {
skeleton -> itemHeight = bottom - top
parent -> parentHeight = bottom - top
}
calcItemCount()
}
}
}
if (itemHeight == 0) {
skeleton.addOnLayoutChangeListener(listener)
}
if (parentHeight == 0) {
parent.addOnLayoutChangeListener(listener)
}
calcItemCount()
}
return SkeletonRecyclerViewHolder(skeleton)
}

override fun onBindViewHolder(holder: SkeletonRecyclerViewHolder, position: Int) = Unit

override fun getItemCount() = itemCount
override fun getItemCount(): Int {
return when (measured) {
true -> itemCount
else -> 1
}
}
}