Popularity
2.4
Growing
Activity
7.8
Declining
75
6
28

Description

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

Programming language: Kotlin
Latest version: v4.6.0

RecyclerView Presenter alternatives and similar packages

Based on the "Kotlin" category

Do you think we are missing an alternative of RecyclerView Presenter or a related project?

Add another 'Kotlin' Package

README

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](app/src/main/java/net/kibotu/android/recyclerviewpresenter/app/kotlin/PhotoPresenter.kt#L14-L24) or [LabelPresenter](app/src/main/java/net/kibotu/android/recyclerviewpresenter/app/kotlin/LabelPresenter.kt#L12-L19)
class LabelPresenter : Presenter<String>() {

    override val layout = R.layout.label_presenter_item

    override fun bindViewHolder(viewHolder: RecyclerView.ViewHolder, item: PresenterModel<String>, position: Int, payloads: MutableList<Any>?, adapter: Adapter) = with(viewHolder.itemView) {
    label.text = item.model
    }
}
  1. [Add the PresenterAdapter to your RecyclerView](app/src/main/java/net/kibotu/android/recyclerviewpresenter/app/kotlin/PresenterActivity.kt#L23-L29)
val adapter = PresenterAdapter()
list.adapter = adapter
  1. [Register Presenter](app/src/main/java/net/kibotu/android/recyclerviewpresenter/app/kotlin/PresenterActivity.kt#L24-L26)
adapter.registerPresenter(PhotoPresenter())
adapter.registerPresenter(LabelPresenter())
adapter.registerPresenter(NumberPresenter())
  1. [Submit list of models with presenter matching layout](app/src/main/java/net/kibotu/android/recyclerviewpresenter/app/kotlin/PresenterActivity.kt#L39-L51) to the adapter, e.g.:
val items = ArrayList<PresenterModel<String>>()

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)
  1. Add click listener [to adapter](app/src/main/java/net/kibotu/android/recyclerviewpresenter/app/kotlin/PresenterActivity.kt#L31-L33)
adapter.onItemClick { item, view, position ->
    snack("$position. ${item.model}")
}

4.1 or pass [to your RecyclerViewModel](app/src/main/java/net/kibotu/android/recyclerviewpresenter/app/kotlin/PresenterActivity.kt#L43-L45)

val item = PresenterModel(createRandomImageUrl()) {
    snack("$position. ${item.model}")
}

[Updating item](app/src/main/java/net/kibotu/android/recyclerviewpresenter/app/kotlin/PresenterActivity.kt#L55)

adapter.submitList(newItems)

Notes

Follow me on Twitter: Twitter Follow

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

Contributions welcome!

License

Copyright 2019 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.


*Note that all licence references and agreements mentioned in the RecyclerView Presenter README section above are relevant to that project's source code only.