💡🚀⭐️ A generalized adapter for RecyclerView on Android which makes it easy to add heterogeneous items to a list

Overview

Download Kotlin API License Android Arsenal Awesome

Mystique is a Kotlin library for Android’s RecyclerView which allows you to create homogeneous and heterogeneous lists effortlessly using an universal adapter. It’s RecyclerView.Adapter on steroids, written purely in Kotlin (oh yeah, with extension functions too).

Download

You can download this library through jCenter by one of the following ways,

Gradle

Add this line to your build.gradle file under the dependencies section along with any other library that you might use.

dependencies {
	compile 'co.upcurve.mystique:mystique:1.0'
}

Maven

If you prefer to use Maven, then add the following lines,

<dependency>
  <groupId>co.upcurve.mystique</groupId>
  <artifactId>mystique</artifactId>
  <version>1.0</version>
  <type>pom</type>
</dependency>

Usage

Using this library is fairly simple. You need to create a data class which will be the model for your row data, and a presenter or item which extends MystiqueItemPresenter

Creating the model class

The model class is a regular data class which holds all the data that needs to be displayed in a single row inside the list, with however one special addition.

You need to annotate the presenter that will be used with this model class for Mystique to correctly recognize the presenter for this model class and bind both of them without any effort from your side.

NewsModel.kt

//Specify your presenter here in this format
@Presenter(NewsItem::class)
data class NewsModel(var heading: String = "", 
		     var desc: String = "",
		     var content: String = "")

Please note, the model class needs to be data class for functions like removeItem() to work.

That’s it, nothing more is required to add to your data class.

Creating your presenter or item class

The presenter or the item class is the place where you specify which layout should be inflated for the model and how to load data into the inflated view from your model according to your business logic. You can also add your custom click listeners here to handle clicks on the row or individual items in the row.

Consider the following example class,

NewsItem.kt

class NewsItem : MystiqueItemPresenter() {

    //Declare your NewsModel object
    lateinit private var newsModel: NewsModel

    //Binding all the views in the row using ButterKnife
    @BindView(R.id.heading_news)
    lateinit var newsHeading: TextView

    /**
     * This is where you need to populate your data from the model
     * to each element in the row according to your business logic
     */
    override fun displayView(itemView: View) {
        ButterKnife.bind(this, itemView)

        newsHeading.text = newsModel.heading
    }

    /**
     * Return your model here for Mystic to do proper mapping
     * while adding or deleting items from your list
     */
    override fun getModel() = newsModel

    /**
     * Return the layout associated with the model for Mystic
     * to correctly inflate the proper views for each supplied
     * model
     */
    override fun getLayout() = R.layout.view_item_news

    /**
     * This is where you initialize your model as supplied
     * by Mystique through this method call
     */
    override fun loadModel(model: Any) {
        newsModel = model as NewsModel
    }

    /**
     * If you need to handle clicks on your items or
     * items within your item, this is where you do it
     * by adding your own custom listener (see sample)
     */
    override fun handleClickEvents(itemView: View) {

    }

    /**
     * Initialize your custom listener in this function
     * call, which will be called my Mystique while loading
     * your data
     */
    override fun setListener(listener: Any?) {

    }
}

And, you’re all set!

Loading the models to the adapter

The next step is to load your models to the universal adapter, which is a breeze thanks to Mystique’s auto-binding of models to their respective presenters.

Once you have your data initialized in their appropriate model classes, declare a reference to your adapter and load the data in this fashion,

/**
  * Initialize your adapter
  */
val adapter = MystiqueAdapter<MystiqueItemPresenter>()

/**
  * Set the models to the adapter for loading
  */
adapter.setItems(newsList.toMystifiedList())

Here, the extension function (since it’s Kotlin) converts a regular List<T> to a MutableList<MystiqueItemPresenter> by attaching all the appropriate models to their presenters behind the scenes for you.

Voila! That’s all. Did you expect more?

Other functions

Mystique also defines other functions such as,

Add an item

fun <T : MystiqueItemPresenter> MystiqueAdapter<T>.addItem(item: T?, index: Int = mystiqueItems.size)

Remove an item

Using an index,

fun <T : MystiqueItemPresenter> MystiqueAdapter<T>.removeItem(index: Int = mystiqueItems.size - 1)

Using a model,

fun <T : MystiqueItemPresenter> MystiqueAdapter<T>.removeItem(model: Any)

Add a list of items

fun <T : MystiqueItemPresenter> MystiqueAdapter<T>.addItems(items: List<T>, startPosition: Int = mystiqueItems.size)

Remove a list of items

fun <T : MystiqueItemPresenter> MystiqueAdapter<T>.removeItems(items: List<Any>)

Convert any object to a Mystified object

object.toMystifiedItem()

Sample app

Refer to this section in this repo for a sample app using the library,

MystiqueSample

Contribution

I have made this library as easy as possible to use, but I might have missed out some cool stuff. If you have something that you would like to add, fork the library, make your changes and please send a pull request.

I would be happy to add your contribution to the library, if it improves the library.

Who made Mystique?

Well the name of the library might be Mystique, but certainly the author is not a mystery.

Rahul Chowdhury

Blog | Twitter | LinkedIn | Facebook | Medium

License

Copyright 2017 Rahul Chowdhury

   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

You might also like...
A set of extension properties on Int, Long, Double, and Duration, that makes it easier to work with Kotlin Duration

Kotlin Duration Extensions Gradle Groovy repositories { mavenCentral() } implementation 'com.eygraber:kotlin-duration-extensions:1.0.1' Kotlin rep

This server uses json files to model items, entities and more.
This server uses json files to model items, entities and more.

Design Server | Simple server to design items, entities or more About this project/server: This server uses json files to model items, entities and mo

A Clean Architecture App that uses clean Architecture (app, presentation, domain, data) packages to use coffee items API using MVVM Architecture, Coroutines, Retrofit2, Pagination, Live Data, Data Binding, Hilt , SOLID Principles , Navigation Component , RoomDb. 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

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

Add Wheel Date - Time Picker in Android Jetpack Compose.
Add Wheel Date - Time Picker in Android Jetpack Compose.

WheelPickerCompose Add Wheel Date - Time Picker in Android Jetpack Compose. Usage Picker Usage WheelDateTimePicker { snappedDateTime - } WheelDatePic

A FDPClient fork , It aims to add more modules.

LightClient A FDPClient fork , It aims to add more modules. You can download development version at Github-Actions , Release at Release Only running o

A kotlin library of extension functions that add smalltalk style methods to objects.

KtTalk A kotlin library of extension functions that add smalltalk style methods to objects. Motivation Smalltalk is a pure OO language in which everyt

A simple e-market application that allows users to view the store details and products, add products to the basket, and place an order.
A simple e-market application that allows users to view the store details and products, add products to the basket, and place an order.

E-Market Application Features : Store details & products screen Fetch the store detail from an endpoint and display this upper part of the screen. Fet

Releases(v1.0)
Owner
Rahul Chowdhury
Android Developer. Loves Kotlin. Oh yeah and shares memes.
Rahul Chowdhury
A simple and easy adapter for RecyclerView. You don't have to make adapters and view holders anymore. Slush will help you.

한국어 No more boilerplate adapters and view holders. Slush will make using RecyclerView easy and fast. The goal of this project is to make RecyclerView,

SeungHyun 26 Sep 13, 2022
:speedboat: Floating navigation view for displaying a list of items dynamically on Android.

Submarine Fully customizable floating navigation view for listing items dynamically on Android. Including in your project Gradle Add below codes to yo

Jaewoong Eum 460 Dec 21, 2022
RecyclerView Adapter Library with different models and different layouts as convenient as possible.

RecyclerView Presenter Convenience library to handle different view types with different presenters in a single RecyclerView. How to install repositor

Jan Rabe 86 Dec 26, 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
Stateful is a Kotlin library which makes Android application development faster and easier.

Stateful Stateful is a Kotlin library which makes Android application development faster and easier. It helps you delete all the boilerplate code for

PicsArt 67 Oct 3, 2022
Recycler-coroutines - RecyclerView Auto Add Data Using Coroutines

Sample RecyclerView Auto Add With Coroutine Colaborator Very open to anyone, I'l

Faisal Amir 8 Dec 1, 2022
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

Jack and phantom 514 Jan 8, 2023
Arc Layout is a view group with which you can add a arc-shaped container in your layout.

ArcLayout Arc Layout is a view group with which you can add a arc-shaped container in your layout. Two main variables are the direction and the curvat

Ali Rezaiyan 32 Aug 17, 2022
Muhammad Bilal 0 Jan 6, 2022
API-Request - Android app that makes API Request

API-Request Android project using Retrofit and Ktor for Http Requests, built wit

Thales 2 Mar 26, 2022