我正在尝试实现购物车视图,用户可以在其中查看他选择的产品并可以编辑其数量。对于存储,我正在使用Room并从数据库中观察实时数据。我在EditText上使用焦点更改侦听器,并在edittext失去焦点时更新数量。我面临的问题是,每当我单击任何edittext时,第一个edittext都会成为焦点。
dashboardViewModel.cartProducts.observe(viewLifecycleOwner, object: Observer<List<Product?>> {
override fun onChanged(t: List<Product?>?) {
t?.let {
Log.d(TAG,"${it.size}")
fragmentDashboardBinding.cartRecycler.apply {
val recyclerViewState = fragmentDashboardBinding.cartRecycler.layoutManager?.onSaveInstanceState()
adapter = CartAdapter(it)
fragmentDashboardBinding.cartRecycler.layoutManager?.onRestoreInstanceState(recyclerViewState)
}
}
}
}
binding.apply {
viewModel?.cartItem = cartItem
binding.cartProductQuantity.setOnFocusChangeListener(object: View.OnFocusChangeListener{
override fun onFocusChange(v: View?, hasFocus: Boolean) {
if(!hasFocus){
cartItem?.quantity = if(binding.cartProductQuantity.text.toString()==""){0}else{binding.cartProductQuantity.text.toString().toInt()}
dashboardViewModel.updateProduct(cartItem!!)
}
}
})
executePendingBindings()
}