Android RecyclerView Adapter with nested items & expand/contract functionality

Overview

AccordionRecycler

License Android Arsenal Bintray

Android RecyclerView Adapter with nested items & expand/contract functionality

With AccordionRecycler you can easily create awesome RecyclerViews, containing infinitely nested items and the ability to expand or collapse any part of the View at will.

Demo GIF

Installation

implementation 'com.izikode.izilib:accordionrecycler:0.5'

Usage

Have your Model Classes implement the AccordionRecyclerData interface.

  • The Adapter needs a way to get the ViewType, Primary data and the Enclosed (child) data, if existing. You use the above interface to provide those.
  • Fill the diamond operator with the type the model provides.
  • Optionally, you can use a base class as a reference for your different data sub-classes and also your ViewHolders. In the provided demo, and examples below, ColorData is the base class and it provided itself.
abstract class ColorData : AccordionRecyclerData<ColorData>

data class RedData(

        var arrayOfPink: Array<PinkData> = arrayOf()

) : ColorData(RedViewHolder.VIEW_TYPE) {

    override val mainData: ColorData?
            get() = this

    override val enclosedDataArray: Array<out AccordionRecyclerData<out ColorData?>>?
            get() = arrayOfPink

}

data class GrayData( ... ) : ColorData() { ... }
data class PinkData( ... ) : ColorData() { ... }
data class WhiteData( ... ) : ColorData() { ... }

Have your ViewHolders extend the AccordionRecyclerViewHolder abstract class.

  • Provide the ViewHolder constructor with the parent ViewGroup and the layout to be inflated.
  • Fill the diamond operator with the type of Model being handled.
  • Again, optionally, create a base ViewHolder as a reference.
abstract class ColorViewHolder<Data>( ... ) : AccordionRecyclerViewHolder<Data> where Data : ColorData

class RedViewHolder(parent: ViewGroup) : ColorViewHolder<RedData>(parent, R.layout.view_holder_red) {

    override var data: RedData? = null
    
    companion object {
        const val VIEW_TYPE = 2
    }

}

class GrayViewHolder( ... ) : ColorViewHolder<GrayData>
class PinkViewHolder( ... ) : ColorViewHolder<GrayData>
class WhiteViewHolder( ... ) : ColorViewHolder<RedData>

Create an Adapter that extends the AccordionRecyclerAdapter abstract class.

  • Override the buildViewHolder function to provide new ViewHolder instances.
  • Override the updateViewHolder function to update each item being recycled. The AccordionRecyclerItemDetails parameter contains information about the current item and all enclosing (parent) items, when existing. This allows for unlimited visual combinations for all view types.
  • Expand/Collapse is accomplished with:
    • removeItem to remove an item and all it's child items, if existing.
    • removeEnclosedItems to remove the child items only.
    • addItems to add all items and their child items, if existing.
    • addEnclosedItems to add all items as child items to another.
  • Optionally, you can modify how each item is handled when it is iterated and added into the recycler data. You do that by overriding the processForAdditionalItems function. You can use this feature to handle edge case. One such case is shown in the demo, in which whenever a Pink item does not have any child White items, a new item is added, showing the text 'Nothing to see here'.
class MainAccordionAdapter : AccordionRecyclerAdapter<ColorViewHolder<out ColorData>, ColorData>() {

    override fun buildViewHolder(parent: ViewGroup, viewType: Int): ColorViewHolder<out ColorData> =
            when(viewType) {

                RedViewHolder.VIEW_TYPE -> RedViewHolder(parent)
                PinkViewHolder.VIEW_TYPE -> PinkViewHolder(parent)
                WhiteViewHolder.VIEW_TYPE -> WhiteViewHolder(parent)

                else -> GrayViewHolder(parent)

            }

    override fun updateViewHolder(position: Int, viewHolder: ColorViewHolder<out ColorData>, data: ColorData?, 
                details: AccordionRecyclerItemDetails) {
                
            when (viewHolder) {

                is RedViewHolder -> viewHolder.apply { ... }
                is PinkViewHolder -> viewHolder.apply { ... }
                is WhiteViewHolder -> viewHolder.apply { ... }

                else -> (viewHolder as GrayViewHolder).apply { ... }

            }
        }
        
    override fun processForAdditionalItems(position: Int, item: AccordionRecyclerData<out ColorData?>?)
            : Array<out AccordionRecyclerData<out ColorData?>?> = item?.let {

                    if (it is PinkData && it.enclosedDataArray.isNullOrEmpty()) {
                        arrayOf(
                            it,
                            EmptyPinkViewHolder.EmptyPinkData()
                        )
                    } else {
                        super.processForAdditionalItems(position, item)
                    }

                } ?: super.processForAdditionalItems(position, item)

}

For a full example, see the sample app.

Licence

Copyright 2018 Fanis Veizis

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

Kotlin Dsl for Android RecyclerView

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

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

Recycler-coroutines - RecyclerView Auto Add Data Using Coroutines

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

Android MVVM framework write in kotlin, develop Android has never been so fun.

KBinding 中文版 Android MVVM framework write in kotlin, base on anko, simple but powerful. It depends on my another project AutoAdapter(A library for sim

Klimatic is an android app built using Kotlin. It try to showcase all the latest technologies used in android.
Klimatic is an android app built using Kotlin. It try to showcase all the latest technologies used in android.

Klimatic Klimatic is an android app built using Kotlin. It try to showcase all the latest technologies used in android. Built using Android Architectu

Oratio Library for Android Studio helps you simplify your Android TTS codes

Oratio Oratio is a library for Android Studio. This library is useful to a number of developers who are currently making apps using android TTS(Text-T

This is a demo android app representing implementation of SE principles in android app development

Articles Demo This repository contains a sample Android App that shows most popular articles data from NY Times API. This is a sample app that shows h

Android-Boilerplate - Base project for android development with new technology

Android-Boilerplate Base project for android development with new technology, in

Comments
  • Release 0.4

    Release 0.4

    • The Data interface properties were made immutable, to further separate the extension from the actual contents.
    • A weak map of item details was added, which reuses data unless a change occurs. This, along with recursive iteration over the parent items, greatly increases performance.
    opened by iFanie 0
  • Added grouped position info along with parent info

    Added grouped position info along with parent info

    The data provided to an item for recycling have been grouped in a data class along with the same info being recursively provided for the item's enclosing item.

    opened by iFanie 0
  • Added overridable pre addition processing

    Added overridable pre addition processing

    Before an item gets added to the actual data list, it gets passed through a processing phase, which has been made overridable. This was added to cover cases of null main data or empty enclosed data, for which the user may want to show a visual representation of the case, without having to temper with his original data.

    opened by iFanie 0
  • Release 0.1

    Release 0.1

    Contains the initial codebase and core functionality of the library including:

    • Interfaces and Abstract classes needed
    • Abstract RecyclerAdapter
    • Add/Remove/Append functionality for outer and nested items
    opened by iFanie 0
Releases(v0.5)
  • v0.5(Jan 5, 2019)

  • v0.4(Nov 19, 2018)

    • The Data interface properties were made immutable, to further separate the extension from the actual contents.
    • A weak map of item details was added, which reuses data unless a change occurs. This, along with recursive iteration over the parent items, greatly increases performance.
    Source code(tar.gz)
    Source code(zip)
  • v0.3(Nov 15, 2018)

    The data used for updating an item during recycling, has been grouped into a data class. In that, the same information is recursively added for any parent items.

    Source code(tar.gz)
    Source code(zip)
  • v0.2(Nov 14, 2018)

    Adds the ability to override the processing function of the items while their addition iteration is performed. This way the user can handle edge cases of empty main data or empty enclosed data, and possibly append to the recycler contents without tempering with the original data.

    The sample project has been updated using the above feature to show a placeholder Pink variant for the Pink rows that have no enlosed White items.

    Source code(tar.gz)
    Source code(zip)
  • v0.1(Nov 13, 2018)

    Initial implementation of the concept, with all needed interfaces and abstract classes exposed and all core (add, remove, append, expand, contract) functionalities.

    Included is a sample project which showcases the above features and contains Gray rows without any enclosed data, Red rows with Pink enclosed rows, which have White enclosed rows in their own turn. All items are visually adjusted based on their overall position and their relative position to their enclosing item.

    Also, the following Click Events are added:

    • Clicking on a non empty Red row removes all contents, clicking on an empty Red row generates random Pink rows.
    • Clicking on a non empty Pink row removes all contents, clicking on an empty Pink row removes itself.
    • Clicking on a White row removes itself.
    Source code(tar.gz)
    Source code(zip)
Owner
Fanis Veizis
Fanis Veizis
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
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
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
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
Kotlin Multiplatform lifecycle-aware business logic components (aka BLoCs) with routing functionality and pluggable UI (Jetpack Compose, SwiftUI, JS React, etc.), inspired by Badoos RIBs fork of the Uber RIBs framework

Decompose Please see the project website for documentation and APIs. Decompose is a Kotlin Multiplatform library for breaking down your code into life

Arkadii Ivanov 819 Dec 29, 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
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

Phillipp Glanz 5 Jan 7, 2022
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

Trade Me 849 Dec 27, 2022
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

Kacper Wojciechowski 6 Nov 23, 2021