List,
ListView
RecyclerView
Adapters: Servants of the ListView

- It fetches the items to be displayed from the data source
- It decides how they should be displayed
- It passes this information on to the ListView
val context = this
listView.setOnItemClickListener { _, _, position, _ ->
// 1
val selectedRecipe = recipeList[position]
// 2
val detailIntent = RecipeDetailActivity.newIntent(context, selectedRecipe)
// 3
startActivity(detailIntent)
}
Note: Before you dive into the explanation, make sure you understand the four arguments that are provided by onItemClick; they work as follows:
- parent: The view where the selection happens — in your case, it’s the ListView
- view: The selected view (row) within the ListView
- position: The position of the row in the adapter
- id: The row id of the selected item
本文深入探讨了Android开发中常用的ListView和RecyclerView组件。这两种组件用于展示列表数据,它们从数据源获取数据并决定如何显示。适配器在其中起着关键作用,负责传递数据和布局信息给ListView或RecyclerView。当用户点击ListView的项时,可以通过适配器获取选中项,并启动新的Activity展示详细信息。了解这些组件的工作原理对于优化Android应用的性能至关重要。
3万+

被折叠的 条评论
为什么被折叠?



