本文翻译自:https://johncodeos.com/how-to-use-view-binding-in-android-using-kotlin/
Android Stuido4.1中,移除了kotlin-android-extensions插件
所以不能在.kt文件中使用Kotlinx synthetic语法了
替代方案有:
- ButterKnife
- findViewById()
- View Binding
下面看下View Binding在下面的场景怎么使用
- Activities
- Fragments:
- Inflate
- Bind
- RecyclerView Adapter
一、Enabling View Binding
android {
// module级别的build.gradle文件中添加下名的配置
// 会自动为xml文件生成binding class
// 命名规则是Pascal case + Binding
// 可在XML中添加属性来屏蔽生成binding class
// tools:viewBindingIgnore="true"
buildFeatures {
viewBinding true
}
}
二、Using View Binding in Activities
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
val view = binding.root
setContentView(view)
// ...
}
}
binding.myButton.setOnClickListener { // Button Tapped }
binding.myTextView.text = "View Binding is the best!"
三、Using View Binding in Fragments
inflate methos
class DemoInflateFragment : Fragment() {
private var fragmentDemoBinding: FragmentDemoBinding? = null
// 注意是在onCreateView中inflate内容,所以AsyncLayoutInflater不适应Fragment
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding = FragmentDemoBinding.inflate(inflater, container, false)
fragmentDemoBinding = binding
binding.myButton.setOnClickListener {
// Button Tapped
}
binding.myTextView.text = "View Binding is the best!"
return binding.root
}
override fun onDestroyView() {
// View不见时Fragment可以存在
fragmentDemoBinding = null
super.onDestroyView()
}
}
Bind method
class DemoBindFragment : Fragment(R.layout.fragment_demo) {
private var fragmentDemoBinding: FragmentDemoBinding? = null
// 构造方法中传入了,resId,默认的onCreateView中会inflate内容,此时只需要bind即可
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentDemoBinding.bind(view)
fragmentDemoBinding = binding
binding.myButton.setOnClickListener { // Button Tapped }
binding.myTextView.text = "View Binding is the best!"
}
override fun onDestroyView() {
// View不见时Fragment可以存在
fragmentDemoBinding = null
super.onDestroyView()
}
}
四、Using View Binding in RecycleView Adapter
class Items_RVAdapter(private var itemsCells: ArrayList<String?>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
// 第一步,使用binding class作为root layout
class ItemViewHolder(var viewBinding: ItemRowBinding) : RecyclerView.ViewHolder(viewBinding.root)
// 第二步,inflate内容并返回ItemViewHolder
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val binding = ItemRowBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ItemViewHolder(binding)
}
// 第三步,直接使用了
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
val itemViewHolder = holder as ItemViewHolder
itemViewHolder.viewBinding.itemtextview.text = itemsCells[position]
}
}
网友评论