Loading repository data…
Loading repository data…
manneohlund / repository
Small, smart and generic adapter for recycler view with easy and advanced data to ViewHolder binding.
A transparent discovery signal based on current public GitHub metadata.
This score does not audit code, security, maintainers, documentation quality, or suitability. Verify the repository and its current documentation before adoption.
Never code any boilerplate RecyclerAdapter again! This library will make it easy and painless to map your data item with a target ViewHolder.
Add jcenter() or maven { url "https://dl.bintray.com/manneohlund/maven" } to your build.gradle under repositories
Core
dependencies {
// Core SmartRecyclerAdapter
implementation 'io.github.manneohlund:smart-recycler-adapter:5.0.0-rc01'
}
Extensions
dependencies {
// ViewEvent click listeners, multi select, swipe dismiss and drag & drop
implementation 'io.github.manneohlund:smart-recycler-adapter-viewevent:1.0.0-beta03'
// DiffUtil extension library
implementation 'io.github.manneohlund:smart-recycler-adapter-diffutil:1.0.0-beta01'
// Nested adapter extension library
implementation 'io.github.manneohlund:smart-recycler-adapter-nestedadapter:1.0.0-beta01'
// Sticky header extension library
implementation 'io.github.manneohlund:smart-recycler-adapter-stickyheader:1.0.0-alpha02'
// Filter extension library
implementation 'io.github.manneohlund:smart-recycler-adapter-filter:1.0.0-alpha01'
}
SmartRecyclerAdapter
.items(items)
.map(MoviePosterModel::class, PosterViewHolder::class)
.map(MovieBannerModel::class, BannerViewHolder::class)
.map(MovieModel::class, MovieViewHolder::class)
.map(TopNewsModel::class, TopNewsViewHolder::class)
.add(OnClickEventListener { event: ViewEvent.OnClick ->
// Handle event
})
.into<SmartRecyclerAdapter>(recyclerView)
Just extend your ViewHolder class with SmartViewHolder and pass in the target type ex SmartViewHolder<Mail>.
Note that the constructor can both take View or ViewGroup as parameter, in this case PosterViewHolder(parentView: ViewGroup) to avoid casting to ViewGroup while inflating.
The parentView is the recyclerView.
The method unbind has an default implementation and is optional.
class PosterViewHolder(parentView: ViewGroup) :
SmartViewHolder<MovieModel>(parentView, R.layout.poster_item) {
override fun bind(movie: MovieModel) {
Glide.with(imageView)
.load(model.posterUrl)
.into(imageView)
}
override fun unbind() {
Glide.with(imageView).clear(imageView)
}
}
Works with Android DataBinding! Just add the DataBinding LayoutInflater in super call. 🚀
class PosterViewHolder(parentView: ViewGroup) :
SmartViewHolder<MovieModel>(
LayoutInflater.from(parentView.context)
.inflate(R.layout.poster_item, parentView, false)
)
If you want to bind one data type with different view holders depending on some attribute you can set a ViewTypeResolver. Note .map() call not needed in this case but you can combine if you want to.
SmartRecyclerAdapter
.items(items)
.setViewTypeResolver{ item, position -> {
when {
item is MovieTrailerModel -> MovieTrailerViewHolder::class
item is MovieModel && item.isRatedR() -> RMovieViewHolder::class
else -> MovieViewHolder::class // Add default view if needed, else SmartRecyclerAdapter will look at the base `.map` mapping
}
}}
.into(recyclerView)
A popular feature in apps is to have endless scrolling with pagination, in other words load more items when user has scrolled to bottom. With SmartEndlessScrollRecyclerAdapter you can achieve this.
setAutoLoadMoreEnabled defines if false load more button should be visible before loading.setLoadMoreLayoutResource can also set your custom loading/loadmore view.OnLoadMoreListener is called when scrolled to the last item and loading view is visible.val endlessScrollAdapter: SmartEndlessScrollRecyclerAdapter = SmartEndlessScrollRecyclerAdapter
.items(items)
.setAutoLoadMoreEnabled(true)
.setLoadMoreLayoutResource(R.layout.custom_loadmore_view)
.setOnLoadMoreListener { adapter, loadMoreViewHolder ->
// Handle load more items
}
.map(MovieModel::class, MovieViewHolder::class)
.into(recyclerView)
Enable/Disable endless scrolling and thus removing the loading view.
endlessScrollAdapter.isEndlessScrollEnabled = false
As of smart-recycler-adapter:v5.0.0