Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Android RecyclerView Adapter with nested items & expand/contract functionality

License

NotificationsYou must be signed in to change notification settings

iFanie/AccordionRecycler

Repository files navigation

LicenseAndroid ArsenalBintray

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 theAccordionRecyclerData 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.
abstractclassColorData :AccordionRecyclerData<ColorData>data classRedData(vararrayOfPink:Array<PinkData> = arrayOf()) : ColorData(RedViewHolder.VIEW_TYPE) {overrideval mainData:ColorData?            get()=thisoverrideval enclosedDataArray:Array<outAccordionRecyclerData<outColorData?>>?            get()= arrayOfPink}data classGrayData( ... ) : ColorData() {... }data classPinkData( ... ) : ColorData() {... }data classWhiteData( ... ) : ColorData() {... }

Have your ViewHolders extend theAccordionRecyclerViewHolder 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.
abstractclassColorViewHolder<Data>( ... ) : AccordionRecyclerViewHolder<Data> where Data : ColorDataclassRedViewHolder(parent:ViewGroup) : ColorViewHolder<RedData>(parent,R.layout.view_holder_red) {overridevar data:RedData?=nullcompanionobject {constvalVIEW_TYPE=2    }}classGrayViewHolder( ... ) : ColorViewHolder<GrayData>classPinkViewHolder( ... ) : ColorViewHolder<GrayData>classWhiteViewHolder( ... ) : ColorViewHolder<RedData>

Create an Adapter that extends theAccordionRecyclerAdapter abstract class.

  • Override thebuildViewHolder function to provide new ViewHolder instances.
  • Override theupdateViewHolder function to update each item being recycled. TheAccordionRecyclerItemDetails 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 theprocessForAdditionalItems 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'.
classMainAccordionAdapter :AccordionRecyclerAdapter<ColorViewHolder<outColorData>,ColorData>() {overridefunbuildViewHolder(parent:ViewGroup,viewType:Int):ColorViewHolder<outColorData>=when(viewType) {RedViewHolder.VIEW_TYPE->RedViewHolder(parent)PinkViewHolder.VIEW_TYPE->PinkViewHolder(parent)WhiteViewHolder.VIEW_TYPE->WhiteViewHolder(parent)else->GrayViewHolder(parent)            }overridefunupdateViewHolder(position:Int,viewHolder:ColorViewHolder<outColorData>,data:ColorData?,details:AccordionRecyclerItemDetails) {when (viewHolder) {isRedViewHolder-> viewHolder.apply {... }isPinkViewHolder-> viewHolder.apply {... }isWhiteViewHolder-> viewHolder.apply {... }else-> (viewHolderasGrayViewHolder).apply {... }            }        }overridefunprocessForAdditionalItems(position:Int,item:AccordionRecyclerData<outColorData?>?):Array<outAccordionRecyclerData<outColorData?>?>= item?.let {if (itisPinkData&& 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 VeizisLicensed 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.0Unless required by applicable law or agreed to in writing, softwaredistributed 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 andlimitations under the License.

[8]ページ先頭

©2009-2025 Movatter.jp