视图绑定 是Jetpack Compose的一部分,它通过以null安全的方式替换findViewById方法来提供与类绑定的视图。在本文中,我们将在RecyclerView的Adapter类中看到其实现。请注意,我们将使用Kotlin语言实施此项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Kotlin作为编程语言
步骤2:添加视图绑定依赖项
转到android标签内的build.gradle(app)和以下依赖项,然后单击立即同步。
buildFeatures {
viewBinding true
}
步骤3:使用activity_main.xml
转到activity_main.xml文件,并参考以下代码。以下是activity_main.xml文件的代码。它只有一个Recycler视图,我们将用它来显示我们的数据。
XML
XML
Kotlin
// this is the Language model class
class Language(
val name : String ="",
val exp : String =""
)
Kotlin
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.geeksforgeeks.rvadapterviewbinding.databinding.SingleItemBinding
class RvAdapter(
var languageList: List,
) : RecyclerView.Adapter() {
// create an inner class with name ViewHolder
// It takes a view argument, in which pass the generated class of single_item.xml
// ie SingleItemBinding and in the RecyclerView.ViewHolder(binding.root) pass it like this
inner class ViewHolder(val binding: SingleItemBinding) : RecyclerView.ViewHolder(binding.root)
// inside the onCreateViewHolder inflate the view of SingleItemBinding
// and return new ViewHolder object containing this layout
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = SingleItemBinding
.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding)
}
// bind the items with each item of the list languageList which than will be
// shown in recycler view
// to keep it simple we are not setting any image data to view
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
with(holder){
with(languageList[position]){
binding.tvLangName.text = this.name
binding.tvExp.text = this.exp
}
}
}
// return the size of languageList
override fun getItemCount(): Int {
return languageList.size
}
}
Kotlin
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.geeksforgeeks.rvadapterviewbinding.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
// view binding for the activity
private var _binding : ActivityMainBinding? = null
private val binding get() = _binding!!
// create reference to the adapter and the list
// in the list pass the model of Language
private lateinit var rvAdapter: RvAdapter
private lateinit var languageList : List
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
// load data to language list
loadLanguage()
// initialize the adapter, and pass the required argument
rvAdapter = RvAdapter(languageList)
// attach adapter to the recycler view
binding.rvList.adapter = rvAdapter
}
// add items to the list manually in our case
private fun loadLanguage() {
languageList = listOf(
Language("Java" , "Exp : 3 years"),
Language("Kotlin" , "Exp : 2 years"),
Language("Python" , "Exp : 4 years"),
Language("JavaScript" , "Exp : 6 years"),
Language("PHP" , "Exp : 1 years"),
Language("CPP" , "Exp : 8 years"),
)
}
// on destroy of view make the binding reference to null
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
步骤4:创建一个新的布局文件并将其命名为single_item.xml文件
转到single_item.xml文件,并参考以下代码。以下是single_item.xml文件的代码。这是我们将在RecyclerView中使用的单个项目布局。
XML格式
步骤5:创建一个新的模型类
创建一个新的Language.kt类,我们将使用自定义通用“ Language ”的数据来传递将在回收者视图中显示的列表。
科特林
// this is the Language model class
class Language(
val name : String ="",
val exp : String =""
)
步骤6:使用适配器类
创建一个新的类RvAdapter.kt,它将充当回收者视图的Adapter类。使用视图绑定,我们使用布局single_item.xml的生成类(即SingleItemBinding)在我们的案例中在MainActivity.kt的回收者视图中添加数据和视图。为了更好的理解,在代码之前添加了注释。
科特林
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.geeksforgeeks.rvadapterviewbinding.databinding.SingleItemBinding
class RvAdapter(
var languageList: List,
) : RecyclerView.Adapter() {
// create an inner class with name ViewHolder
// It takes a view argument, in which pass the generated class of single_item.xml
// ie SingleItemBinding and in the RecyclerView.ViewHolder(binding.root) pass it like this
inner class ViewHolder(val binding: SingleItemBinding) : RecyclerView.ViewHolder(binding.root)
// inside the onCreateViewHolder inflate the view of SingleItemBinding
// and return new ViewHolder object containing this layout
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val binding = SingleItemBinding
.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(binding)
}
// bind the items with each item of the list languageList which than will be
// shown in recycler view
// to keep it simple we are not setting any image data to view
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
with(holder){
with(languageList[position]){
binding.tvLangName.text = this.name
binding.tvExp.text = this.exp
}
}
}
// return the size of languageList
override fun getItemCount(): Int {
return languageList.size
}
}
步骤7:使用MainActivity.kt
转到MainActivity.kt文件,并参考以下代码。下面是MainActivity.kt文件的代码。在代码内部添加了注释,以更详细地了解代码。
科特林
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.geeksforgeeks.rvadapterviewbinding.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
// view binding for the activity
private var _binding : ActivityMainBinding? = null
private val binding get() = _binding!!
// create reference to the adapter and the list
// in the list pass the model of Language
private lateinit var rvAdapter: RvAdapter
private lateinit var languageList : List
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
_binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
// load data to language list
loadLanguage()
// initialize the adapter, and pass the required argument
rvAdapter = RvAdapter(languageList)
// attach adapter to the recycler view
binding.rvList.adapter = rvAdapter
}
// add items to the list manually in our case
private fun loadLanguage() {
languageList = listOf(
Language("Java" , "Exp : 3 years"),
Language("Kotlin" , "Exp : 2 years"),
Language("Python" , "Exp : 4 years"),
Language("JavaScript" , "Exp : 6 years"),
Language("PHP" , "Exp : 1 years"),
Language("CPP" , "Exp : 8 years"),
)
}
// on destroy of view make the binding reference to null
override fun onDestroy() {
super.onDestroy()
_binding = null
}
}
输出: