GithubHelp home page GithubHelp logo

ifanie / accordionrecycler Goto Github PK

View Code? Open in Web Editor NEW
18.0 0.0 0.0 4.94 MB

Android RecyclerView Adapter with nested items & expand/contract functionality

License: Apache License 2.0

Kotlin 100.00%
android kotlin recyclerview recyclerview-adapter expandablerecyclerview recyclerview-multi-type collapsable expandable nested-recycler-view

accordionrecycler's Introduction

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.

accordionrecycler's People

Contributors

ifanie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

accordionrecycler's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.