引用生成的适配器 类 (android-sunflower)
Adapter referencing generated classes (android-sunflower)
我正在查看 Google 开发的名为 Sunflower 的 Kotlin
示例应用程序,我正在使用它来了解 Kotlin
/ Android Jetpack
的工作原理。
在PlantAdapter
中是如下代码:
/**
* Adapter for the [RecyclerView] in [PlantListFragment].
*/
class PlantAdapter : ListAdapter<Plant, PlantAdapter.ViewHolder>(PlantDiffCallback()) {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val plant = getItem(position)
holder.apply {
bind(createOnClickListener(plant.plantId), plant)
itemView.tag = plant
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(ListItemPlantBinding.inflate(
LayoutInflater.from(parent.context), parent, false))
}
private fun createOnClickListener(plantId: String): View.OnClickListener {
return View.OnClickListener {
val direction = PlantListFragmentDirections.ActionPlantListFragmentToPlantDetailFragment(plantId)
it.findNavController().navigate(direction)
}
}
class ViewHolder(
private val binding: ListItemPlantBinding
) : RecyclerView.ViewHolder(binding.root) {
fun bind(listener: View.OnClickListener, item: Plant) {
binding.apply {
clickListener = listener
plant = item
executePendingBindings()
}
}
}
}
我很困惑的是它从哪里得到PlantListFragmentDirections
和ListItemPlantBinding
?当我跳转到这些 类 的定义时,它们位于自动生成的 build
文件夹中。当我查看导入时
import
com.google.samples.apps.sunflower.PlantListFragmentDirections
import
com.google.samples.apps.sunflower.databinding.ListItemPlantBinding
它们不在项目结构中。这是如何工作的?
这是两种不同的生成 classes。它们在构建过程中自动生成(使用 Android Studio 时会即时生成)。命名遵循 .xml 资源文件中定义的名称,并带有与其组件相对应的后缀。
1. ListItemPlantBinding
ListItemPlantBinding
是为数据绑定生成的 class,参见 generated data-binding documentation
The above layout filename is activity_main.xml so the corresponding generated class is ActivityMainBinding
这意味着 ListItemPlantBinding
是为 list_item_plant.xml
生成的
启用了数据绑定
dataBinding {
enabled = true
}
2. PlantListFragmentDirections
Navigation Architecture Component docs指向第二个答案。
A class for the destination where the action originates, appended with the word "Directions".
因此PlantListFragmentDirections
源自nav_garden.xml:
<fragment
android:id="@+id/plant_list_fragment"
android:name="com.google.samples.apps.sunflower.PlantListFragment"
android:label="@string/plant_list_title"
tools:layout="@layout/fragment_plant_list">
<action
android:id="@+id/action_plant_list_fragment_to_plant_detail_fragment"
app:destination="@id/plant_detail_fragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />
</fragment>
请注意包含 <action>
的 <fragment>
元素
我正在查看 Google 开发的名为 Sunflower 的 Kotlin
示例应用程序,我正在使用它来了解 Kotlin
/ Android Jetpack
的工作原理。
在PlantAdapter
中是如下代码:
/**
* Adapter for the [RecyclerView] in [PlantListFragment].
*/
class PlantAdapter : ListAdapter<Plant, PlantAdapter.ViewHolder>(PlantDiffCallback()) {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val plant = getItem(position)
holder.apply {
bind(createOnClickListener(plant.plantId), plant)
itemView.tag = plant
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
return ViewHolder(ListItemPlantBinding.inflate(
LayoutInflater.from(parent.context), parent, false))
}
private fun createOnClickListener(plantId: String): View.OnClickListener {
return View.OnClickListener {
val direction = PlantListFragmentDirections.ActionPlantListFragmentToPlantDetailFragment(plantId)
it.findNavController().navigate(direction)
}
}
class ViewHolder(
private val binding: ListItemPlantBinding
) : RecyclerView.ViewHolder(binding.root) {
fun bind(listener: View.OnClickListener, item: Plant) {
binding.apply {
clickListener = listener
plant = item
executePendingBindings()
}
}
}
}
我很困惑的是它从哪里得到PlantListFragmentDirections
和ListItemPlantBinding
?当我跳转到这些 类 的定义时,它们位于自动生成的 build
文件夹中。当我查看导入时
import com.google.samples.apps.sunflower.PlantListFragmentDirections
import com.google.samples.apps.sunflower.databinding.ListItemPlantBinding
它们不在项目结构中。这是如何工作的?
这是两种不同的生成 classes。它们在构建过程中自动生成(使用 Android Studio 时会即时生成)。命名遵循 .xml 资源文件中定义的名称,并带有与其组件相对应的后缀。
1. ListItemPlantBinding
ListItemPlantBinding
是为数据绑定生成的 class,参见 generated data-binding documentation
The above layout filename is activity_main.xml so the corresponding generated class is ActivityMainBinding
这意味着 ListItemPlantBinding
是为 list_item_plant.xml
dataBinding {
enabled = true
}
2. PlantListFragmentDirections
Navigation Architecture Component docs指向第二个答案。
A class for the destination where the action originates, appended with the word "Directions".
因此PlantListFragmentDirections
源自nav_garden.xml:
<fragment
android:id="@+id/plant_list_fragment"
android:name="com.google.samples.apps.sunflower.PlantListFragment"
android:label="@string/plant_list_title"
tools:layout="@layout/fragment_plant_list">
<action
android:id="@+id/action_plant_list_fragment_to_plant_detail_fragment"
app:destination="@id/plant_detail_fragment"
app:enterAnim="@anim/slide_in_right"
app:exitAnim="@anim/slide_out_left"
app:popEnterAnim="@anim/slide_in_left"
app:popExitAnim="@anim/slide_out_right" />
</fragment>
请注意包含 <action>
<fragment>
元素