Skip to content

Instantly share code, notes, and snippets.

@Hardik8184
Created December 27, 2019 07:33
Show Gist options
  • Save Hardik8184/30cb1f1ebb5a12a883ad3ff10e11477a to your computer and use it in GitHub Desktop.
Save Hardik8184/30cb1f1ebb5a12a883ad3ff10e11477a to your computer and use it in GitHub Desktop.
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.bumptech.glide.Glide
import com.hardik.shimmereffect.R.layout
import com.hardik.shimmereffect.RecipeListAdapter.RecipeViewHolder
import kotlinx.android.synthetic.main.recipe_list_item.view.chef
import kotlinx.android.synthetic.main.recipe_list_item.view.description
import kotlinx.android.synthetic.main.recipe_list_item.view.name
import kotlinx.android.synthetic.main.recipe_list_item.view.price
import kotlinx.android.synthetic.main.recipe_list_item.view.thumbnail
import kotlinx.android.synthetic.main.recipe_list_item.view.timestamp
/**
* Created by Hardik on 18/10/18.
*/
class RecipeListAdapter(
private val context: Context,
private val recipeList: List<RecipeResponse>
) : Adapter<RecipeViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): RecipeViewHolder {
val itemView = LayoutInflater.from(parent.context)
.inflate(layout.recipe_list_item, parent, false)
return RecipeViewHolder(itemView)
}
override fun onBindViewHolder(
holder: RecipeViewHolder,
position: Int
) {
holder.bind(recipeList[position])
}
inner class RecipeViewHolder(itemView: View) : ViewHolder(itemView) {
fun bind(recipeResponse: RecipeResponse) {
itemView.name.text = recipeResponse.name
itemView.chef.text = String.format("By %s", recipeResponse.chef)
itemView.description.text = recipeResponse.description
itemView.price.text = String.format("Price: ₹ %s", recipeResponse.price)
itemView.timestamp.text = recipeResponse.timestamp
Glide.with(context)
.load(recipeResponse.thumbnail)
.into(itemView.thumbnail)
}
}
// recipe
override fun getItemCount(): Int {
return recipeList.size
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment