ArticleAdapter
com.bsvillarraga.spaceflightnews.presentation.ui.articles.adapter.ArticleAdapter
RecyclerView ListAdapter for displaying articles with pagination loading indicator.
Inheritance: Extends ListAdapter<Article, RecyclerView.ViewHolder>
Diff Callback: Uses ArticleDiffCallback() for efficient list updates
Constructor Parameters
onItemClicked- Lambda invoked when article item is clicked, receives clicked Article
Properties
viewTypeItem: Int = 0- View type constant for article itemsviewTypeLoading: Int = 1- View type constant for loading indicatorisLoading: Boolean = false- Current loading state for pagination
Methods
getItemViewType()
- Returns
viewTypeItemfor article positions - Returns
viewTypeLoadingfor loading indicator position
adapter/ArticleAdapter.kt:18
getItemCount()
adapter/ArticleAdapter.kt:22
onCreateViewHolder()
viewTypeItem→ CreatesArticleViewHolderwithItemArticleBindingviewTypeLoading→ CreatesLoadingViewHolderwithItemLoadingBinding
parent- Parent ViewGroupviewType- View type constant
adapter/ArticleAdapter.kt:26
onBindViewHolder()
- Only processes
ArticleViewHolderinstances - Gets article at position from ListAdapter
- Calls
bind()on holder - Sets click listener to invoke
onItemClickedlambda
holder- ViewHolder to bindposition- Item position in list
adapter/ArticleAdapter.kt:43
setLoading()
isLoading- true to show loading indicator, false to hide
- Returns early if state unchanged (prevents unnecessary updates)
- When true: Notifies insertion of loading item at end
- When false: Notifies removal of loading item
adapter/ArticleAdapter.kt:54
ArticleViewHolder
com.bsvillarraga.spaceflightnews.presentation.ui.articles.adapter.ArticleViewHolder
ViewHolder for individual article items in RecyclerView.
Inheritance: Extends RecyclerView.ViewHolder
Constructor Parameters
binding- View binding for article item layout
Methods
bind()
article- Article data to display
- Shows progress bar initially
- Loads article image using Glide with:
- Center crop transformation
- RequestListener for load state
- Hides progress bar on image load success/failure
- Shows image view only on successful load
- Sets formatted published date with string resource
- Sets article title
- Uses Glide for efficient image loading and caching
- Implements
RequestListener<Drawable>for load state callbacks:onLoadFailed()- Returns false to allow Glide default error handlingonResourceReady()- Hides progress bar, shows image, returns false
- Uses
toFormattedDate()extension function - Formats with
R.string.published_atstring resource
adapter/ArticleViewHolder.kt:19
View Bindings
Thebind() method interacts with these views from ItemArticleBinding:
progressBar- ProgressBar shown during image loadimgArticle- ImageView for article imagetvDate- TextView for formatted published datetitleText- TextView for article title
Dependencies
Glide Configuration:centerCrop()- Image transformation- Custom RequestListener for load state management
Article.toFormattedDate()- Formats timestamp to readable date string
adapter/ArticleViewHolder.kt:1