Pro Android with Kotlin


CHAPTER 9: User Interface


Download 5.42 Mb.
Pdf ko'rish
bet169/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   165   166   167   168   169   170   171   172   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

200
CHAPTER 9: User Interface
override
fun onBindViewHolder(holder:ViewHolder,
position:Int) {
// replace the contents of the view with
// the element at this position
holder.v.findViewById(
R.id.firstLine).text =
myDataset[position]
}
override
fun getItemCount() : Int = myDataset.size
}
Here are a couple of notes on that listing:

The class inside the “companion object” is Kotlin’s way of declaring a 
static inner class. This one designates a reference to each data item as 
a UI element. More precisely, the recycler view will internally hold only so 
many view holders as are necessary to represent the visible items.

Only when really needed the function onCreateViewHolder() to create
a view holder gets called. More precisely, it’s called more or less only as 
often as is necessary to render the items visible to the user.

The function onBindViewHolder() connects one of the visible view 
holders with a certain data item. Here we must replace the contents of a 
view holder’s view.
Inside the activity all that is needed to define the recycler view is the following:
with(recycler_view) {
// use this setting to improve performance if you know
// that changes in content do not change the layout
// size of the RecyclerView
setHasFixedSize(true)
// use for example a linear layout manager
layoutManager = LinearLayoutManager(this@MainActivity)
// specify the adapter, use some sample data
val dataset = (1..21).map { "Itm" + it }.toTypedArray()
adapter = MyAdapter(dataset)
}
This will look like Figure 
9-1
. The following are useful extensions to the program:

Add on-click listeners to all items

Make items selectable

Make items or item parts editable

Automatically react to changes in the underlying data

Tailor graphical transition effects



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   165   166   167   168   169   170   171   172   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling