Poniżej przykład prostejimplementacji RecyclerView w języku Kotlin.
Na początku klasa do trzymania danych dla pojedynczego wiersza na liście:
data class Item(
var name: String?,
var description: String?)
Layout dla pojedynczego wiersza:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:orientation="horizontal">
<TextView
android:id="@+id/nameTV"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Name: " />
<TextView
android:id="@+id/name"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/descriptionTV"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Description" />
<TextView
android:id="@+id/description"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
Adapter dla RecyclerView:
class MainAdapter( private val data: List<Item>) : RecyclerView.Adapter<MainAdapter.MainViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder {
val inflater = LayoutInflater.from(parent.context)
val view = inflater.inflate(R.layout.list_item, parent, false)
return MainViewHolder(view)
}
override fun onBindViewHolder(holder: MainViewHolder, position: Int) {
holder.name.text = data[position].name
holder.description.text = data[position].description
}
override fun getItemCount(): Int {
return data.size
}
inner class MainViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
internal var name: TextView
internal var description: TextView
init {
name = itemView.findViewById<View>(R.id.name) as TextView
description = itemView.findViewById<View>(R.id.description) as TextView
}
}
}
Activity w którym RecyclerView jest użyty:
class MainActivity : AppCompatActivity() {
private var itemList: MutableList<Item> = ArrayList()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val recycler = findViewById<View>(R.id.recycler) as RecyclerView
recycler.layoutManager = LinearLayoutManager(this)
val adapter = MainAdapter(itemList)
recycler.adapter = adapter
val button = findViewById<View>(R.id.button) as Button
button.setOnClickListener {
val description = findViewById<EditText>(R.id.inputDescription)
val name = findViewById<EditText>(R.id.InputName)
val nowyWpis = Item(description.text.toString(), name.text.toString())
itemList.add(nowyWpis)
adapter.notifyDataSetChanged()
}
}
}
Layout dla Activity:
<?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"
tools:context=".MainActivity">
<TextView
android:id="@+id/InputNameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Name"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/InputName"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/InputName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/InputNameTV"
android:layout_marginStart="8dp"
android:ems="10"
app:layout_constraintBottom_toTopOf="@+id/inputDescription"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/InputNameTV" />
<TextView
android:id="@+id/InputDescriptionTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/InputName"
android:layout_alignParentStart="true"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="Description: "
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/inputDescription"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/inputDescription" />
<EditText
android:id="@+id/inputDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/InputDescriptionTV"
android:layout_alignParentEnd="true"
android:layout_marginStart="8dp"
android:layout_marginBottom="32dp"
android:ems="10"
android:text=""
app:layout_constraintBottom_toTopOf="@+id/button"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/inputDescription"
android:layout_alignParentStart="true"
android:layout_marginStart="8dp"
android:layout_marginBottom="32dp"
android:text="Add"
app:layout_constraintBottom_toTopOf="@+id/recycler"
app:layout_constraintStart_toStartOf="parent" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="match_parent"
android:layout_height="250dp"
android:layout_below="@+id/button"
android:layout_alignParentStart="true"
android:layout_marginStart="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>