0
0
Android Kotlinmobile~20 mins

Why dynamic lists display data efficiently in Android Kotlin - Build It to Prove It

Choose your learning style9 modes available
Build: Dynamic List Efficiency
This screen shows a list of items using a dynamic list component that efficiently displays data by reusing views.
Target UI
-------------------------
| Dynamic List Efficiency |
-------------------------
| Item 1                 |
| Item 2                 |
| Item 3                 |
| ...                    |
| Item N                 |
-------------------------
Use RecyclerView to display a list of 100 items labeled 'Item 1' to 'Item 100'.
Implement ViewHolder pattern to reuse item views efficiently.
Display a simple TextView for each item showing its label.
Ensure smooth scrolling performance.
Starter Code
Android Kotlin
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

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

        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this)

        // TODO: Set adapter with list of items
    }
}

// TODO: Create Adapter class here
Task 1
Task 2
Task 3
Task 4
Solution
Android Kotlin
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

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

        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
        recyclerView.layoutManager = LinearLayoutManager(this)

        val items = List(100) { index -> "Item ${index + 1}" }
        recyclerView.adapter = ItemAdapter(items)
    }
}

class ItemAdapter(private val items: List<String>) : RecyclerView.Adapter<ItemAdapter.ItemViewHolder>() {

    class ItemViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(android.R.id.text1)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(android.R.layout.simple_list_item_1, parent, false)
        return ItemViewHolder(view)
    }

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        holder.textView.text = items[position]
    }

    override fun getItemCount(): Int = items.size
}

This app uses RecyclerView to display a long list of 100 items efficiently. RecyclerView only creates enough item views to fill the screen plus a few extra. When you scroll, it recycles these views instead of creating new ones, which saves memory and improves performance.

The ItemAdapter class uses the ViewHolder pattern to hold references to the item views. This avoids repeatedly finding views by ID, making scrolling smooth.

Each item is a simple TextView showing "Item 1", "Item 2", and so on. This example shows how dynamic lists reuse views to display large data sets efficiently.

Final Result
Completed Screen
-------------------------
| Dynamic List Efficiency |
-------------------------
| Item 1                 |
| Item 2                 |
| Item 3                 |
| ...                    |
| Item 100               |
-------------------------
User can scroll up and down smoothly through the 100 items.
RecyclerView reuses item views as user scrolls to keep performance high.
Stretch Goal
Add a click listener to each item that shows a Toast message with the item label.
💡 Hint
In your adapter's onBindViewHolder, set an OnClickListener on holder.itemView and use Toast.makeText(context, "Clicked: " + items[position], Toast.LENGTH_SHORT).show()