Sunday, 14 October 2018

HB Blog 160: Listview Using DataBinding In Kotlin Language.

Hello guys, we have seen Listview using Kotlin Language in my old posts. You can recall using below link,
HB Blog 157: Listview In Android Using Kotlin.

Also, we saw Databinding library that is part of Jetpack Architecture in my previous blog post, 
HB Blog 159: Jetpack Architecture: - DataBinding.

In this tutorial, let us build listview using databinding in Kotlin language.
Refer the below link for complete sample code:-

Download Sample Code

Have a look on few code snippets,

//build.gradle
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'

android {
    compileSdkVersion 26
    buildToolsVersion "27.0.0"
    defaultConfig {
        applicationId "com.harshalbenake.kotlindatabindinglist"
        minSdkVersion 16
        targetSdkVersion 26
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    dataBinding {
        enabled true
    }
}

repositories {
    mavenCentral()
}
dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:support-v4:26.1.0'
    implementation 'com.android.support:appcompat-v7:26.1.0'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:+"
    implementation 'com.android.support:recyclerview-v7:26.1.0'
    kapt "com.android.databinding:compiler:3.0.0"
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:+"
}

//rowitempersonprofile.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <variable
            name="personprofile"
            type="com.harshalbenake.kotlindatabindinglist.Model.PersonProfile" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:padding="16dp"
        android:layout_height="wrap_content"
        android:background="@android:drawable/alert_light_frame"
        android:orientation="vertical">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:textColor="@android:color/black"
            android:text="@{personprofile.name,default=name}" />

        <TextView
            android:id="@+id/tv_email"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{personprofile.email,default=email}" />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@{Integer.toString(personprofile.age),default=0}" />
    </LinearLayout>
</layout>

//PersonProfile.kt
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
package com.harshalbenake.kotlindatabindinglist.Model

/**
 * Used as a layout variable to provide static properties name, emails and age
 */
data class PersonProfile(
        val name: String,
        val email: String,
        val age: Int
)

//PersonProfileAdapter.kt
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.harshalbenake.kotlindatabindinglist.Adapters

import android.databinding.DataBindingUtil
import android.databinding.ViewDataBinding
import android.graphics.Color
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import com.harshalbenake.kotlindatabindinglist.BR
import com.harshalbenake.kotlindatabindinglist.Model.PersonProfile
import com.harshalbenake.kotlindatabindinglist.R
import kotlinx.android.synthetic.main.rowitempersonprofile.view.*

class PersonProfileAdapter(val locallist: List<PersonProfile>) : RecyclerView.Adapter<ViewHolder>() {
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(locallist[position])
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
        val binding: ViewDataBinding = DataBindingUtil.inflate(layoutInflater, R.layout.rowitempersonprofile, parent, false)
        binding.root.tv_email.setTextColor(Color.GRAY)
        return ViewHolder(binding)
    }

    override fun getItemCount(): Int = locallist.size
}

class ViewHolder(val binding: ViewDataBinding) : RecyclerView.ViewHolder(binding.root) {
    fun bind(personprofile: PersonProfile) {
        binding.setVariable(BR.personprofile, personprofile)
        binding.executePendingBindings()
    }
}

//MainActivity.kt
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.harshalbenake.kotlindatabindinglist

import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import com.harshalbenake.kotlindatabindinglist.Adapters.PersonProfileAdapter
import com.harshalbenake.kotlindatabindinglist.Model.PersonProfile

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main)

        //array variable initialized
        var listItems = arrayListOf<PersonProfile>()
        //arraylist item added
        for (i in 0..10) {
            listItems.add(PersonProfile("Harshal Benake " + i, "harshalbenake" + i + "@gmail.com", 28 + i))
        }

        val recyclerView = findViewById<RecyclerView>(R.id.rv_personprofile)
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = PersonProfileAdapter(listItems)
    }
}

No comments:

Post a Comment