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

Overview

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>(R.layout.label_presenter_item) {

    private val View.label: TextView
        get() = findViewById(R.id.label)

    override fun bindViewHolder(viewHolder: RecyclerView.ViewHolder, item: PresenterViewModel<String>, payloads: MutableList<Any>?) = with(viewHolder.itemView) {
        logv { "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: [email protected]

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.
You might also like...
Simplify mutating "immutable" state models

Mutekt (Pronunciation: /mjuːˈteɪt/, 'k' is silent) "Simplify mutating "immutable" state models" Generates mutable models from immutable model definiti

Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.
Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.

Carousel Recyclerview Create carousel effect in recyclerview with the CarouselRecyclerview in a simple way. Including in your project Gradle Add below

Adapter library for SharedPreferences

EasyPrefs Adapter library for SharedPreferences which reduces boilerplate needed to store simple data, but open enough to not interfere with your own

The Android maps adapter
The Android maps adapter

MapMe MapMe is an Android library for working with Maps. MapMe brings the adapter pattern to Maps, simplifying the management of markers and annotatio

A highly customizable calendar library for Android, powered by RecyclerView.
A highly customizable calendar library for Android, powered by RecyclerView.

CalendarView A highly customizable calendar library for Android, powered by RecyclerView. With this library, your calendar will look however you want

Library For Toast with different colors

Toaster-Library Library For Toast with different colors To get a Git project int

A sample app showing different kind of memory leaks and their solutions

MemoryLeakApp A sample app showing different kind of memory leaks and their solutions. Make sure to check the source code to understand the concept of

Kotlin-based modern RecyclerView rendering weapon
Kotlin-based modern RecyclerView rendering weapon

Read this in other languages: 中文, English, Changelog Yasha Item introduction: Kotlin-based modern RecyclerView rendering weapon Item Features: No Adap

Kotlin Dsl for Android RecyclerView

KRecyclerDsl Kotlin Dsl for Android RecyclerView Exemple Sample project recyclerView.adapter = dataClassAdapterMyView, MyDataClass(R.layout.my_view,

Comments
  • Issue #2. ViewBinding support

    Issue #2. ViewBinding support

    What are your thoughts on these changes? It works fine, but refactoring of all presentation classes should be required after upgrade.

    Please add #hacktoberfest-accepted, if you want to merge :)

    opened by dionep 1
Owner
Jan Rabe
Native Android / Unity Developer & Technocrat #kotlin #csharp #unity #opensource
Jan Rabe
Auto-generate the fastest possible Parcelable implementations for Java and Kotlin

This project is deprecated It will still be maintained, but no new features will be added. Please use Parcelize, as it is the official way of generati

Bradley Campbell 492 Nov 17, 2022
Android RecyclerView Adapter with nested items & expand/contract functionality

AccordionRecycler Android RecyclerView Adapter with nested items & expand/contract functionality With AccordionRecycler you can easily create awesome

Fanis Veizis 17 Aug 18, 2022
💡🚀⭐️ A generalized adapter for RecyclerView on Android which makes it easy to add heterogeneous items to a list

Mystique is a Kotlin library for Android’s RecyclerView which allows you to create homogeneous and heterogeneous lists effortlessly using an universal

Rahul Chowdhury 48 Oct 3, 2022
Don't write a RecyclerView adapter again. Not even a ViewHolder!

LastAdapter Don't write a RecyclerView adapter again. Not even a ViewHolder! Based on Android Data Binding Written in Kotlin No need to write the adap

Miguel Ángel Moreno 781 Dec 19, 2022
Condenses KDoc where possible.

KDocCondenser Template ToDo list Create a new IntelliJ Platform Plugin Template project. Get known with the template documentation. Verify the pluginG

Jacob Wysko 0 Nov 3, 2021
A somewhat copy past of Jetbrain's code from the kotlin plugin repo to make it humanly possible to test Intellij IDEA kotlin plugins that work on kotlin

A somewhat copy past of Jetbrain's code from the kotlin plugin repo to make it humanly possible to test Intellij IDEA kotlin plugins that work on kotlin

common sense OSS 0 Jan 20, 2022
Clean MVVM with eliminating the usage of context from view models by introducing hilt for DI and sealed classes for displaying Errors in views using shared flows (one time event), and Stateflow for data

Clean ViewModel with Sealed Classes Following are the purposes of this repo Showing how you can remove the need of context in ViewModels. I. By using

Kashif Mehmood 22 Oct 26, 2022
An Easy-to-use Kotlin based Customizable Modules Collection with Material Layouts by BlackBeared.

Fusion By BlackBeared An Easy-to-use Kotlin based Customizable Library with Material Layouts by @blackbeared. Features Custom Floating Action Buttons

Sandip Savaliya 38 Oct 5, 2022
Android fundamental codelabs (layouts)

AboutMe Finished code for Android Kotlin Fundamentals codelab(Lesson 2: Layouts) Introduction In the AboutMe app, you can showcase interesting facts a

Animesh Roy 0 Nov 26, 2021
Mogen - Converts Kotlin models to other languages

Mogen is a small library that converts Kotlin's models to other programming lang

elevup 3 Jun 8, 2022