如何解决Android数据绑定按钮onClick不起作用
我被困在这里,帮助。
我有以下代码:
ProfileFragment:
@AndroidEntryPoint
class ProfileFragment : Fragment() {
private val profileViewModel: ProfileViewModel by viewModels()
override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
val binding = FragmentProfileBinding.inflate(inflater,container,false)
binding.viewModel = profileViewModel
binding.lifecycleOwner = viewLifecycleOwner
return binding.root
}
}
ProfileViewModel:
class ProfileViewModel @ViewModelInject constructor(
@ApplicationContext private val context: Context,private val profileRepository: ProfileRepository
) : ViewModel() {
fun getUser() {
....
}
}
fragment_profile.xml:
<data>
<variable
name="viewModel"
type="my.app.viewmodel.ProfileViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="match_parent"
android:layout_height="55dp"
android:onClick="@{()->viewModel.getUser()}" />
</LinearLayout>
问题在于,无论我做什么以及如何尝试,onClick
都不会触发。
但是,只要在ProfileFragment
中这样操作,它就可以正常工作:
binding.myButton.setOnClickListener {
profileViewModel.getUser()
}
有什么想法吗?因为我被困在这里
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。