Skip to content

Commit

Permalink
[Added]: RecyclerView Adapter.
Browse files Browse the repository at this point in the history
  • Loading branch information
avinashbest committed May 30, 2022
1 parent 9db7ab0 commit 35a7a3b
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.android.newsapp.shared

import androidx.recyclerview.widget.DiffUtil
import com.android.newsapp.data.NewsArticle

class NewsArticleComparator : DiffUtil.ItemCallback<NewsArticle>() {
override fun areItemsTheSame(oldItem: NewsArticle, newItem: NewsArticle) =
oldItem.url == newItem.url

override fun areContentsTheSame(oldItem: NewsArticle, newItem: NewsArticle) =
oldItem == newItem
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.android.newsapp.shared

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.ListAdapter
import com.android.newsapp.data.NewsArticle
import com.android.newsapp.databinding.ItemNewsArticleBinding

class NewsArticleListAdapter :
ListAdapter<NewsArticle, NewsArticleViewHolder>(NewsArticleComparator()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NewsArticleViewHolder {
val binding =
ItemNewsArticleBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return NewsArticleViewHolder(binding)
}

override fun onBindViewHolder(holder: NewsArticleViewHolder, position: Int) {
val currentItem = getItem(position)
if (currentItem != null) {
holder.bind(currentItem)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.android.newsapp.shared

import androidx.recyclerview.widget.RecyclerView
import com.android.newsapp.R
import com.android.newsapp.data.NewsArticle
import com.android.newsapp.databinding.ItemNewsArticleBinding
import com.bumptech.glide.Glide

class NewsArticleViewHolder(
private val binding: ItemNewsArticleBinding
) : RecyclerView.ViewHolder(binding.root) {

fun bind(article: NewsArticle) {
binding.apply {
Glide.with(itemView)
.load(article.thumbnailUrl)
.error(R.drawable.image_placeholder)
.into(imageView)

textViewTitle.text = article.title ?: ""

imageViewBookmark.setImageResource(
when {
article.isBookmarked -> R.drawable.ic_bookmark_selected
else -> R.drawable.ic_bookmark_unselected
}
)
}
}


}

0 comments on commit 35a7a3b

Please sign in to comment.