I have a View (it's ViewHolder in RecyclerView but I think it wouldn't make any difference) which implements onClick(), onLongClick(), and onTouch(). The problem I faced is that longClick is never executed. I read about it in other questions but I can't figure out how to do it and when exactly I should return true \ false in onTouch(). Here is my code;
Function bind() in ViewHolder class which implements View.OnTouchListener:
binding.background.setOnClickListener {
Log.d("Click")
}
binding.background.setOnLongClickListener {
Log.d("Long Click")
}
binding.background.setOnTouchListener(this) // here I see warning "Custom view `ConstraintLayout` has setOnTouchListener called on it but does not override performClick"
Override onTouch()
override fun onTouch(v: View?, event: MotionEvent?): Boolean
{
return when (event!!.action)
{
MotionEvent.ACTION_DOWN ->
{
x1 = event.x
true
}
MotionEvent.ACTION_UP ->
{
x2 = event.x
return if (abs(x2 - x1) > MIN_DISTANCE)
{
Log.d("Was swiped")
true
}
else
{
Log.d("Wasn't swiped")
v?.performClick()
false
}
}
else -> false
}
}
This function just detects slide on the view. What should I change in onTouch() to make longClick working? Also, is there any simpler way to detect slide on the RecyclerView item?
If someone ever will have such a problem I solved it in this way:
(It is recycler view item which can be moved left / right or scrolled)
ViewHolder fields:
Touch function
Scroll function which is passed to ViewHolder
This code can execute Click, LongClick, and Touching view. When user make LongClick, move action wouldn't be executed. After holding a finger on the screen and moving it more than
CLICK_DISTANCEClick and LongClick wouldn't be executed