Last active
June 12, 2025 11:47
-
-
Save Seniru/92ea889e1ee45967bd79990965098ec6 to your computer and use it in GitHub Desktop.
Android RecyclerView example. This gist only showcases the recycler view handling. You may you have to implement the layouts yourself if you wish to run this.
This file contains hidden or 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
| package com.seniru.recylerviewexample | |
| import android.os.Bundle | |
| import android.widget.Button | |
| import android.widget.TextView | |
| import androidx.appcompat.app.AppCompatActivity | |
| import androidx.recyclerview.widget.LinearLayoutManager | |
| import androidx.recyclerview.widget.RecyclerView | |
| class MainActivity : AppCompatActivity() { | |
| private val todoItems = mutableListOf( | |
| TodoItem("Wake up", true), | |
| TodoItem("Prepare breakfast", true), | |
| TodoItem("Revise mobile application development", true), | |
| TodoItem("Create mobile app", false), | |
| TodoItem("Prepare lunch", true), | |
| TodoItem("Prepare dinner", false), | |
| TodoItem("Exercise", false) | |
| ) | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main) | |
| val newTodoText: TextView = findViewById(R.id.todoText) | |
| val submitButton: Button = findViewById(R.id.submitButton) | |
| // set up recycler view | |
| val todoList: RecyclerView = findViewById(R.id.listView) | |
| todoList.layoutManager = LinearLayoutManager(this) | |
| todoList.adapter = TodoItemAdapter(todoItems) | |
| // add a to-do item to the list when submit button is clicked | |
| submitButton.setOnClickListener { | |
| todoItems.add(TodoItem(newTodoText.text.toString(), false)) | |
| // notify the adapter of the newly added item so it can refresh the view | |
| (todoList.adapter as TodoItemAdapter).notifyItemChanged(todoItems.size - 1) | |
| } | |
| } | |
| } |
This file contains hidden or 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
| package com.seniru.recylerviewexample | |
| data class TodoItem( | |
| val description: String, | |
| val completed: Boolean = false | |
| ) |
This file contains hidden or 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
| package com.seniru.recylerviewexample | |
| import android.view.LayoutInflater | |
| import android.view.View | |
| import android.view.ViewGroup | |
| import androidx.recyclerview.widget.RecyclerView | |
| class TodoItemAdapter(private val data: List<TodoItem>) : RecyclerView.Adapter<TodoItemVH>() { | |
| override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): TodoItemVH { | |
| // inflate the layout | |
| val layoutInflater: LayoutInflater = LayoutInflater.from(parent.context) | |
| val view: View = layoutInflater.inflate(R.layout.todo_item_layout, parent, false) | |
| return TodoItemVH(view) | |
| } | |
| override fun getItemCount(): Int { | |
| return data.size | |
| } | |
| override fun onBindViewHolder(holder: TodoItemVH, position: Int) { | |
| // get a single item from the list | |
| val todoItem = data[position] | |
| // update the view with to-do data | |
| holder.descriptionTextView.text = todoItem.description | |
| holder.completedCheckbox.isChecked = todoItem.completed | |
| } | |
| } |
This file contains hidden or 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
| package com.seniru.recylerviewexample | |
| import android.view.View | |
| import android.widget.CheckBox | |
| import android.widget.TextView | |
| import androidx.recyclerview.widget.RecyclerView.ViewHolder | |
| // view holder will hold a unit to-do item component and the actions related to it | |
| class TodoItemVH(itemView: View) : ViewHolder(itemView) { | |
| val descriptionTextView: TextView = itemView.findViewById(R.id.todoDescription) | |
| val completedCheckbox: CheckBox = itemView.findViewById(R.id.completed) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment