【发布时间】:2020-01-06 02:57:52
【问题描述】:
我目前有一个在 MainActivity 中实现的接口,它允许与 NumberFragment 进行通信。 onCountClicked 是 NumberFragment 中的方法,用于接收项目点击的 id。
问题
如何在每次单击项目时将 onCountClicked 位置发送到 NumberFragmentViewModel?
注意:使用数据绑定和recycleview
主要活动
val newFragment = NumberFragment()
val args = Bundle()
args.putInt(NumberFragment().onCountClicked(data).toString(),data.toInt())
newFragment.arguments = args
数字片段
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val sleepTrackerViewModel =
ViewModelProviders.of(
this, viewModelFactory).get(NumberViewModel::class.java)
setHasOptionsMenu(true)
binding.setLifecycleOwner(this)
binding.sleepTrackerViewModel = sleepTrackerViewModel
//Observes for any changes on database and updates the UI
sleepTrackerViewModel.dbCount.observe(this, Observer {dbCount ->
dbView.text = dbCount.number.toString()
})
return binding.root
}
fun onCountClicked(position: Long) {
Log.i("NumberF" , "------------->" + position)
}
【问题讨论】:
标签: android android-fragments kotlin android-viewmodel