希望能简单的查询;在我的Fragment中,我设置了一些点击监听器。
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
Log.d(TAG, "onCreateView()")
activity?.let {
businessViewModel = ViewModelProvider(it).get(BusinessViewModel::class.java)
Log.d(TAG, "businessViewModel = ${businessViewModel.toString()}")
}
// Inflate the layout for this fragment
val binding: SitesFragmentBinding = DataBindingUtil.inflate(
inflater, R.layout.sites_fragment, container, false
)
binding.apply {
sitesOverflowOptionsMenuButton.setOnClickListener {
showPopUp(it)
Log.d(TAG, "sitesOverflowOptionsMenuButton clicked, showPopUp(it)")
}
sitesDrawerMenuButton.setOnClickListener {
(activity as BusinessActivity).openCloseNavigationDrawer()
Log.d(TAG, "sitesDrawerMenuButton clicked, openCloseNavigationDrawer()")
}
sitesFloatingActionButtonAdd.setOnClickListener {
businessViewModel.getCurrentSite("")
navController.navigate(R.id.action_sitesFragment_to_siteAddEditFragment)
Log.d(TAG,"sitesFloatingActionButtonAdd clicked, navigating to siteAddEditFragment")
}
sitesClearIconImageView.setOnClickListener {
sitesSearchFilterEditTextView.text.clear()
}
sitesFilterIconImageView.setOnClickListener {
filterDialog()
}
sitesFilterIconImageView.setOnLongClickListener{
// This doesn't work - type mismatch, required Boolean, found Unit
}
}
return binding.root
}
如上所述,setOnClickListener有一个类型不匹配,(需要的是Boolean,发现的是Unit)。
它建议创建一个扩展函数,但我似乎不能让这个工作......。
在lambda return中返回一个布尔值即可。
sitesFilterIconImageView.setOnLongClickListener{
//Your other code
false
}