How to populating RecyclerView in Kotlin - Joklinz-Tech

Latest

Home Of Tech news,jobs and android tutorials

Thursday 15 February 2018

How to populating RecyclerView in Kotlin






Welcome Coders ,

Here in this tutorial ,we are going to learn about populating RecyclerView in Kotlin


  • Language – Kotlin
  •  
  • Android studio – V 3.0.1

What we are going to Learn ? 
  • RecyclerView Adapter in Kotlin
  •  
  • Model Class in Kotlin
  •  
  • for loop etc
  •  
  • View Binding

Lets get started ,
Create a new application and choose basic activity as the template ,
Add recyclerview to your layout
– Drag and drop the recyclerview from the palette into the layout or create manually

Sample XML : content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="in.yapp.recyclerviewexample.MainActivity"
tools:showIn="@layout/activity_main">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
view rawcontent_main.xml hosted with ❤ by GitHub

Let’s Create a sample adapter for recyclerview with two textview
adapter.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="2dp"
android:layout_margin="5dp">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_margin="5dp"/>
</RelativeLayout>
view rawadapter.xml hosted with ❤ by GitHub

Model Class


Create a new Kotlin file from File -> New -> – Kotlin File/ Class


Name it as Model write the following code

Model.kt 
package `in`.yapp.recyclerviewexample
/**
* Created by elstin on 12/30/2017.
*/
data class User(
var name:String,
var location:String
)
view rawModel.Kt hosted with ❤ by GitHub
RecyclerView Adapter

Now we need a class to inflate the adapter layout we just created above ,

– Create a new Kotlin class and name it as Adapter.kt

Adapter.kt 


package `in`.yapp.recyclerviewexample
import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.adapter.view.*
/**
* Created by elstin on 12/30/2017.
*/
class Adapter constructor(private val list: List<User>, private val context: Context) : RecyclerView.Adapter<Adapter.setUpData>() {
override fun onBindViewHolder(holder: setUpData, position: Int) {
holder.dataBind(list[position], context)
}
override fun getItemCount(): Int {
return list.size
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): setUpData {
val view = LayoutInflater.from(parent.context).inflate(R.layout.adapter, parent, false)
return setUpData(view)
}
class setUpData(itemView: View?) : RecyclerView.ViewHolder(itemView) {
fun dataBind(data: User, context: Context) {
// No more find ViewByID
itemView.name.text = data.name
itemView.location.text = data.location
}
}
}
view rawAdapter.kt hosted with ❤ by GitHub
Now comes the main part ;)

Lets Finalize the codes 

– Open the MainActivity.kt and make the following changes

MainActivity.kt 


package `in`.yapp.recyclerviewexample
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*
class MainActivity : AppCompatActivity() {
lateinit var userList : ArrayList<User>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
userList = ArrayList()
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = Adapter(userList,this)
addDummyData()
}
private fun addDummyData() {
val user0 = User("Leo Elstin", "Kaniyakumari,TN")
userList.add(user0)
val user1 = User("Bat Man", "Gotham")
userList.add(user1)
val user2 = User("Iorn Man", "New York")
userList.add(user2)
val user3 = User("Thor", "Parallel universe")
userList.add(user3)
val user4 = User("Loki", "Alien Planet")
userList.add(user4)
recyclerView.adapter.notifyDataSetChanged()
}
}
view rawMainActivity.kt hosted with ❤ by GitHub
Hit the run button and tadaa.. :p 

– Hope you learned something new :)

No comments:

Post a Comment