androidkotlinbindingonlongclicklistener

Create setOnClickListener in Binding


Hopefully a simple query; in my Fragment I have set up some click listeners:

    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
    }

As above, the setOnClickListener has a type mismatch, (required Boolean, found Unit).

It suggests creating an extension function, but I can't seem to get this to work..


Solution

  • Just return a boolean in the lambda return.

    sitesFilterIconImageView.setOnLongClickListener{
       //Your other code
       false
    }