/ExempleAdapter.kt Secret
Last active
November 20, 2023 17:56
Star
You must be signed in to star a gist
Un RecyclerView en 3 fichiers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.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=".RecyclerActivity"> | |
<androidx.recyclerview.widget.RecyclerView | |
android:id="@+id/rvDevices" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" | |
tools:listitem="@layout/item_list" /> | |
<Button | |
android:id="@+id/button" | |
android:text="AJOUT !!" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintEnd_toEndOf="parent" | |
app:layout_constraintStart_toStartOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExempleAdapter(private val deviceList: MutableList<String>, private val onClick: ((selectedDevice: String) -> Unit)? = null) : RecyclerView.Adapter<ExempleAdapter.ViewHolder>() { | |
// Comment s'affiche ma vue | |
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { | |
fun showItem(device: String, onClick: ((selectedDevice: String) -> Unit)? = null) { | |
itemView.findViewById<TextView>(R.id.title).text = device | |
if(onClick != null) { | |
itemView.setOnClickListener { | |
onClick(device) | |
} | |
} | |
} | |
} | |
// Retourne une « vue » / « layout » pour chaque élément de la liste | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { | |
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_list, parent, false) | |
return ViewHolder(view) | |
} | |
// Connect la vue ET la données | |
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | |
holder.showItem(deviceList[position], onClick) | |
} | |
override fun getItemCount(): Int { | |
return deviceList.size | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:app="http://schemas.android.com/apk/res-auto" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content"> | |
<TextView | |
android:id="@+id/title" | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:padding="20dp" | |
android:text="@string/app_name" | |
android:textSize="16sp" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Le reste de votre code | |
val items = mutableListOf("Item 1", "Item 2") | |
val rvDevices = this.findViewById<RecyclerView>(R.id.rvDevices) | |
rvDevices.layoutManager = LinearLayoutManager(this) | |
rvDevices.adapter = ExempleAdapter(items) { item -> | |
Toast.makeText(this@MainActivity, "Connexion à $item", Toast.LENGTH_SHORT).show() | |
} | |
findViewById<Button>(R.id.button).setOnClickListener { | |
items.add("Item ${items.size + 1}") | |
rvDevices.adapter?.notifyItemInserted(items.size - 1) | |
} | |
// Le reste de votre code |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment