Skip to content

kibotu/RecyclerViewPresenter

master
Switch branches/tags
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
app
 
 
lib
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Donation About Jan Rabe

RecyclerView Presenter Hits-of-Code Android Arsenal appetize.io Javadoc Build Status API Gradle Version Kotlin GitHub license androidx

Convenience library to handle different view types with different presenters in a single RecyclerView.

Screenshot

How to install

repositories {
    maven {
	url "https://jitpack.io"
    }
}

dependencies {
    implementation 'com.github.kibotu:RecyclerViewPresenter:-SNAPSHOT'
}

How to use

  1. Create a presenter, e.g. PhotoPresenter or LabelPresenter
class LabelPresenter : Presenter<String, LabelPresenterItemBinding>(
    layout = R.layout.label_presenter_item,
    viewBindingAccessor = LabelPresenterItemBinding::bind
) {

    override fun bindViewHolder(
        viewBinding: LabelPresenterItemBinding,
        viewHolder: RecyclerView.ViewHolder,
        item: PresenterViewModel<String>,
        payloads: MutableList<Any>?
    ) = with(viewBinding) {
        Logger.v( "bindViewHolder ${viewHolder.adapterPosition} $item payload=$payloads" )
        label.text = "${item.model}"
    }
}
  1. Add the PresenterAdapter to your RecyclerView
val adapter = PresenterAdapter()
list.adapter = adapter
  1. Register Presenter
adapter.registerPresenter(PhotoPresenter())
adapter.registerPresenter(LabelPresenter())
adapter.registerPresenter(NumberPresenter())
  1. Submit list of models with presenter matching layout to the adapter, e.g.:
val items = mutableListOf<PresenterViewModel<*>>()

for (i in 0..99) {
    items.add(PresenterModel(createRandomImageUrl(), R.layout.photo_presenter_item))
    items.add(PresenterModel(createRandomImageUrl(), R.layout.label_presenter_item))
    items.add(PresenterModel(createRandomImageUrl(), R.layout.number_presenter_item))
}

adapter.submitList(items)

5.1 Add click listener to adapter

adapter.onItemClick { item, view, position ->
    snack("$position. ${item.model}")
}

5.2 or pass to your RecyclerViewModel

val item = PresenterViewModel(createRandomImageUrl(), R.layout.photo_presenter_item) { item, view, position -> 
	snack("$position. $item")
})

Updating item

adapter.submitList(newItems)

Circular

adapter.isCircular = true

// Note: use adapter scroll methods for correct scroll position when using circular
adapter.smoothScrollToPosition(0)
adapter.scrollToPosition(0)

Notes

Follow me on Twitter: Twitter Follow

Let me know what you think: jan.rabe@kibotu.net

Contributions welcome!

License

Copyright 2021 Jan Rabe

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

About

RecyclerView Adapter Library with different models and different layouts as convenient as possible.

Topics

Resources

License

Stars

Watchers

Forks

Sponsor this project

Packages

No packages published