facebookarchive / shimmer-android

An easy, flexible way to add a shimmering effect to any view in an Android app.
http://facebook.github.io/shimmer-android/
Other
5.31k stars 698 forks source link

Shimmer animation still continue when different item is showing in recyclerview #114

Closed yusufonderd closed 3 years ago

yusufonderd commented 3 years ago

There are two different item type in my recyclerview adapter. First one is ProductViewHolder and the other one is ShimmerViewHolder.

I have to use shimmer layout in recyclerview items. I'm using startShimmer() function in ShimmerViewHolder. When recyclerview itemViewType changed as ShimmerViewHolder instead of ProductViewHolder, shimmer alpha animation still continue and make blinking on the ProductViewHolder items. How can i fix this problem ?

Here is the code snippets:

class ShimmerViewHolder(
    view: View) : RecyclerView.ViewHolder(view) {

  private val shimmerLayout: ShimmerFrameLayout = view.findViewById(R.id.shimmerLayout)

  fun bind(shimmer: Shimmer?) {
    shimmerLayout.setShimmer(shimmer)
    shimmerLayout.startShimmer()
  }
}
class ProductViewHolder(
    view: View) : RecyclerView.ViewHolder(view) {
  private val tvProduct : TextView = view.findViewById(R.id.tvProduct)

  fun bind(product: Product) {
    tvProduct.text = product.name
  }
}
class ProductsAdapter(
    val products: List<Product>
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

  private val shimmer = Shimmer
      .AlphaHighlightBuilder()
      .build()

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {

return when (viewType) {
 VIEW_TYPE_PRODUCT -> {
        ProductViewHolder(
            LayoutInflater.from(context).inflate(R.layout.item_product, parent, false),
        )
      }
VIEW_TYPE_SHIMMER -> {
        ShimmerViewHolder(
            LayoutInflater.from(context).inflate(R.layout.item_shimmer, parent, false)
        )
      }
}
}

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    when (holder) {
      is ShimmerViewHolder -> {
        holder.bind(shimmer)
      }
      is ProductViewHolder -> {
      holder.bind(product)
      }
    }
  }

}
yusufonderd commented 3 years ago

I noticed that problem is caused by something else. Then i closed it.