High order function in Android data binding.

Arunachalam kalimuthu
2 min readOct 22, 2020

In this article, we will learn about the high order function in Data Binding Library and what is all about High Order function

What is DataBinding in Android?

The Data Binding Library is an Android Jetpack library that allows you to bind UI components in your XML layouts to data sources in your app using a declarative format rather than programmatically, reducing boilerplate code.

What is the High Order function?

A higher-order function is a function that takes a function as an argument or returns a function.

Example

   // lambda expression 

var
lambda = {println("Hello world") }
// higher-order function

fun
higherfunc( lmbd: () -> Unit ) { // accepting lambda as parameter
lmbd() //invokes lambda expression
}
//main function
fun
main(args: Array<String>) {
//invoke higher-order function
higherfunc(lambda) // passing lambda as parameter
}

Using Kotlin High Order function

So If we want to trigger two methods at the same time whereas to add items to the ViewModel and trigger notifyItemChanged to recycler view adapter using data binding.

To achieve the above case We can pass the second function as an argument.
After adding items to the ViewModel we can trigger the notify notifyItemChanged method

Example Recycler view ItemView

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>

<import type="android.view.View" />

<variable
name="cartViewModel"
type="com.demo.ui.cart.CartViewModel" />

<variable
name="product"
type="com.demo.model.productlist.Product" />

<variable
name="productInteractionListener"
type="com.demo.adapters.recyclerview.cart.ProductListAdapter.ProductInteractionListener" />
</data>

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
style="@style/text_medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="8dp"
android:onClick="@{(view)->productInteractionListener.onItemStateChanged(product,cartViewModel.addProduct(product))}"
android:padding="8dp"
android:text="Add to basket"
android:textColor="@color/blue"
android:textSize="14sp"
app:drawableStartCompat="@drawable/ic_add_circle"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
</layout>

ProductInteractionListener to communicate with the adapter and activity/fragment

interface ProductInteractionListener {
fun onItemStateChanged(product: Product?, action: () -> Product?)
}

In Activity/Fragment notifyItemChanged at the position and calling that function from that argument.

override fun onItemStateChanged(product: Product?, action: () -> Product?) {
productListAdapter.notifyItemChanged(productList.indexOf(product))
action()
}

Function triggered by ProductInteractionListener to do the business logic after adding the items to the cart.

fun addProduct(product: Product): () -> Product? = {
//Implement your buisness logic here
product
}

Conclusion

This article taught you one of the use cases using high order function in android development. I hope this article is helpful. If you think something is missing, have questions, or would like to offer any thoughts or suggestions, go ahead and leave a comment below. I’d appreciate the feedback.

--

--