In this lab, you will learn how to manipulate lists in android.
A ListView is a android View that stores a scrollable collection of views.
Some common examples of ListViews in apps include:
Consider an instagram feed. What would happen if the app loaded 1000 items into the feed?
Recycler View is a powerful and flexible Android View used for displaying large sets of data efficiently by recycling views that are no longer visible.
It's a more modern, flexible, and performant approach to displaying lists as compared to a regular ListView.
From the android documentation:
RecyclerView makes it easy to efficiently display large sets of data. You supply the data and define how each item looks, and the RecyclerView library dynamically creates the elements when they're needed.
As the name implies, RecyclerView recycles those individual elements. When an item scrolls off the screen, RecyclerView doesn't destroy its view. Instead, RecyclerView reuses the view for new items that have scrolled onscreen. RecyclerView improves performance and your app's responsiveness, and it reduces power consumption.
In general, there are a few components that are necessary for implementing a listview
This project would give you practical experience in creating an app with a list. It will use the recycleradapter
Movie to store a title and description of a movie.MainActivity.ktKotlin Class/Fileclass Movie(val title:String, val description:String)
activity_main.xml and remove the "Helo World" TextView.RecyclerView as a child element inside the parent element@+id/rvMovies. This will be used as a lookup value in the code to access this element .match_parentmatch_parent <androidx.recyclerview.widget.RecyclerView
android:id="@+id/rvMovies"
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"
/>
item_movie.xml that would describe what a single item looks likewrap_content@+id/titleTextView@+id/descriptionTextView<!-- Constraint layout properties for the two TextViews
below are left out for brevity -->
<TextView
android:id="@+id/titleTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
/>
<TextView
android:id="@+id/descriptionTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
/>
MovieAdapter.ktMainActivity.ktclass MovieAdapter(private val movies: List<Movie>) : RecyclerView.Adapter<MovieAdapter.ViewHolder>() {
// Code redacted
}
From the android documentation
Each individual element in the list is defined by a view holder object. When the view holder is created, it doesn't have any data associated with it. After the view holder is created, the RecyclerView binds it to its data. You define the view holder by extending RecyclerView.ViewHolder.
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val titleTextView: TextView = itemView.findViewById(R.id.titleTextView)
val descriptionTextView: TextView = itemView.findViewById(R.id.descriptionTextView)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_movie, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val movie = movies[position]
holder.titleTextView.text = movie.title
holder.descriptionTextView.text = movie.description
}
override fun getItemCount(): Int {
return movies.size
}
At this point we have the following items:
Movies.ktitem_movie.xmlRecyclerView inside activity_main.xmlMovieAdapter.ktThe only thing left to do is put it all together.
The following instructions should be performed in the file MainActivity.kt
MainActivity.kt called getMovieData() private fun getMovieData():List<Movie>{
return listOf(
Movie("Lorem Ipsum", "This is a long description of a movie called lorem ipsum and this is awesome."),
Movie("Movie Two", "This is a long description of a movie called Movie Two and this is awesome."),
Movie("Fast and Furious", "This is a long description of a movie called Fast and Furious and this is awesome."),
Movie("Android reigns Supreme", "No description needed. Android is just better"),
Movie("iPhone", "It took me so many years to be able to pause video while recording. Call me the internet explorer of the mobile world"),
Movie("Water Bottle", "Thirsty people drink water. Stay hydrated."),
Movie("The struggle", "When is A1 going to drop? Stay tuned for the next episode"),
Movie("The week 7", "It's the unspoken rule that the 7th week is just... Tears."),
Movie("The week 8", "I survived week 7. I think. Coursework marks may say otherwise"),
Movie("The week 11", "Coursework really did say otherwise. Watch the grind of the students going full tryhard for finals"),
Movie("Week 12", "The grind continues..."),
Movie("Week 13", "Teaching is done, will any students fall into the trap of getting relaxed before exams?"),
Movie("Christmas", "What does Christmas have in store? Would I get my marks? Did I pass? Stay tuned"),
)
}
MainActivity.kt called renderList. Ensure that this function is called last in the onCreate private fun renderList(){
val mainLayout: RecyclerView = findViewById(R.id.rvMovies)
val movies:List<Movie> = getMovieData()
val movieAdapterInst = MovieAdapter(movies)
mainLayout.adapter = movieAdapterInst
mainLayout.layoutManager = LinearLayoutManager(this)
}
The LayoutManager is used to define the general layout specs of the list. By default, the items will be vertically scrollable, but, the properties of the LinearLayoutManager can be used to change this to a horizontal scrolling list
With the successful completion of the lab, you should have an appreciation on how list data is rendered in an android app. With this lab, you are well equipped to replicate lists you're familiar with , it's now just a matter of
An insta DM class could be defined with the properties
How would you create a function getChatData that's similar to the getMovieData function to return a list of recent chats?
How would you create a file similar to the item_movie.xml to define the layout of a single chat item?
The whatsapp recent chats list could be defined as a Chat class with the properties
These items are completely optional.
item_movie.xml file wrap_content modify the layout_height property of the root element in the item_movie.xml file and experiment with different heights e.g. 50dp, 200px, 5em, etc