Skip to content

coenvk/zycle

Repository files navigation

Zycle ♻️

A Kotlin DSL for Android RecyclerViews.

Bintray GitHub GitHub stars

Table of Contents

Getting Started

  1. Add the jcenter() repository to the project build.gradle file.
buildscript {  
 repositories { jcenter() }}  
  1. Add the dependency to your module's build.gradle file.

Latest version: Bintray

dependencies {  
 implementation 'com.coenvk.android.zycle:zycle:1.0.0'}  
  1. Migrate to AndroidX and enable Jetifier in the gradle.properties file.
android.useAndroidX=true  
android.enableJetifier=true  
  1. Add the following dependency for extra kotlin extensions.

Latest version: Bintray

dependencies {  
 implementation 'com.coenvk.android.zycle:zycle-ktx:1.0.0'
}  

Usage

Basic

An adapter can be created by supplying a list of items and the layout resource for the item view. Bind data to the item view in the onBind and add extra event listeners such as the onClick. This can be done using the Builder as illustrated below or a custom Binder can be provided by extending the Binder class. To register event listeners the Binder needs to be Hookable.

Kotlin (click to expand)

recyclerView.zycle {
	adapterOf(list, layoutRes, [viewType]) { // Binder
		onCreate { /* */ }
		onBind { item -> /* */ }
		onRecycle { /* */ }
		stableId { item, position -> /* */ }
		onClick { item, position -> /* */ }
		onLongClick { item, position -> /* */ }
		onTouch { item, position, motionEvent -> /* */ }
		onDrag { item, position, dragEvent -> /* */ }
	}
}

Mapper

If you want to use different layouts, multiple Binder objects can be created and combined into a Mapper.

Kotlin (click to expand)

recyclerView.zycle {
	adapterOf(list) { // Mapper
		map(layoutRes) { // Binder
			// ...
		}
	}
}

Observable List

The ObservableList class can be used with the adapter to automatically update the item views when a change is applied to the list. The list extends the MutableList class, so it can be used with the same functionality. The items in the list can be of any type. For better performance, the item class can implement Diffable and implement the areItemsTheSame and areContentsTheSame methods to compare two items.

Static Views

It is also possible to add static views which don't require binding data.

Kotlin (click to expand)

recyclerView.zycle {
	viewsOf(layoutRes...)
}

Composite Adapter

A composite adapter can be created consisting of multiple adapters. This way it's easy to add headers and footers to your item list.

Kotlin (click to expand)

recyclerView.zycle {
	adapterOf {
		viewsOf(headerRes)
		adapterOf(list) { // Mapper
			map(binder)
		}
		viewsOf(footerRes)
	}
}

Adapter Position Lookup

Since adapters can be nested in composite adapters, the adapter position from the RecyclerView.ViewHolder will not (necessarily) match the index in the inner adapter. Therefore, the AdapterPositionLookup is provided that looks up this position.

Conditional Adapter

An adapter can be shown or hidden based on a condition. In addition, a substitute view can be shown when the condition is not met.

Kotlin (click to expand)

val condition = Condition()
val isEnabled by condition
recyclerView.zycle {
	adapterOf {
		adapterOf(list) {
			map(binder)
		}
		postBuild {
			showIf(condition)
			showIfElse(condition, layoutRes)
		}
	}
}
isEnabled = false // hide the list

Drag & Swipe

To add simple drag functionality a drag callback can be created.

Kotlin (click to expand)

val dragCallback = DragCallback(  
    object : OnDragListener {  
        override fun onDragged(fromPosition: Int, toPosition: Int) {  
            val fromIndex = adapterPositionLookup.innerPosition(fromPosition)  
            val toIndex = adapterPositionLookup.innerPosition(toPosition)  
            list.move(fromIndex, toIndex)  
        }  
    }  
)
recyclerView += ItemTouchHelper(dragCallback)

For this to work, the Binder should implement the Draggable interface.

Secondly, a swipe callback can be used to support swipe actions like swipe-to-delete.

Kotlin (click to expand)

val swipeCallback = SwipeCallback.Builder()  
	.left {  // When swiped left
		background(context, android.R.color.holo_red_dark)  
        text(context, "Delete", spSize = 20f)  
        onSwiped {  
			val index = adapterPositionLookup.innerPosition(it)  
            list.removeAt(index)  
        }  
	}.build()
recyclerView += ItemTouchHelper(swipeCallback)

The Binder needs to be Swipeable.

Sample

For more comprehensive example usages, checkout the sample application here.

Build

The project can be build locally using the following commands:

$ git clone https://github.com/coenvk/zycle.git
$ cd zycle
$ ./gradlew clean build

License

Zycle is licensed under the Apache 2.0 License.

About

♻️ A Kotlin DSL for Android RecyclerViews.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors