RecyclerView with Dummy Data

nisfu07
Binar Academy
Published in
2 min readNov 24, 2018

TUTORIAL: This time, I will show you how to make recyclerview using data dummy. So, let’s start!

  1. Open the build.gradle file for your app module
  2. Add the support library to the dependencies section.
dependencies {
implementation 'com.android.support:recyclerview-v7:28.0.0'
}

Then, make recyclerview layout.

my_rv.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView
android:id="@+id/my_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

Ok, we have ok this time. We already have a recyclerview but we don’t have the item we want to enter into the recyclerview. So we now make the item layout.

item_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical"
android:padding="8dp">

<ImageView
android:id="@+id/ivThumbnail"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"
android:src="@android:color/holo_orange_light" />

<TextView
android:id="@+id/tvName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Burjo Bosque" android:textAppearance="@style/Base.TextAppearance.AppCompat.Headline" />

<TextView
android:id="@+id/tvDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Burjo Belakang Kampus" android:textAppearance="@style/TextAppearance.AppCompat.Subhead" />
</LinearLayout>

We already have the item that will become the list, then we will connect the item with recyclerview using the adapter.

MainAdapter.kt

class MainAdapter(
private val travels: List<Travel>,
private val listener: (Travel) -> Unit
) : RecyclerView.Adapter<MainAdapter.MainViewHolder>() {

override fun onCreateViewHolder(p0: ViewGroup, p1: Int): MainViewHolder =
MainViewHolder(LayoutInflater.from(p0.context).inflate(R.layout.item_list, p0, false))

override fun onBindViewHolder(p0: MainViewHolder, p1: Int) {
p0.bind(travels.get(0), listener)
}

override fun getItemCount() = 20

inner class MainViewHolder(val view: View) : RecyclerView.ViewHolder(view) {

fun bind(travel: Travel, listener: (Travel) -> Unit) {
view.tvName.text = travel.name
view.tvDescription.text = travel.description
view.setOnClickListener { listener(travel) }
}
}
}

Ok, after that we will create a model for dummy data.

Travel.kt

data class Travel(
val id: Long? = null,
val name: String? = null,
val description: String? = null
)

And finally, we will set the MainActivity.kt so that it can appear on the recyclerview list

class MainActivity : AppCompatActivity() {

private lateinit var adapter: MainAdapter
private val travels = mutableListOf(Travel())

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

adapter = MainAdapter(travels) {
startActivity(Intent(this, TravelActivity::class.java)
.putExtra("id", it.id))
}
rvTravel.apply {
adapter = this@MainActivity.adapter
layoutManager = LinearLayoutManager(this@MainActivity)
addItemDecoration(DividerItemDecoration(this@MainActivity, DividerItemDecoration.VERTICAL))
}
}
}

It will be like this when it's finished

perjalananku.apk

--

--