Kotlin

코틀린 안드로이드 RecyclerView ItemClickListener 만들기

Machine_웅 2019. 2. 9. 16:15
728x90
반응형

class content_RecyclerVier_ClickEvent : RecyclerView.OnItemTouchListener {


interface OnItemClickListener {
fun onItemClick(view: View, position: Int)
fun onItemLongClick(view: View, position: Int)
}

private var mListener: OnItemClickListener? = null
private var mGestureDetector: GestureDetector? = null

constructor(context: Context, rView: RecyclerView, clickListener: OnItemClickListener){

mListener = clickListener
mGestureDetector = GestureDetector(context, object : GestureDetector.SimpleOnGestureListener() {
override fun onSingleTapUp(e: MotionEvent): Boolean {
return true
}

override fun onLongPress(e: MotionEvent) {
val childView = rView.findChildViewUnder(e.x, e.y)

if (childView != null && mListener != null) {
mListener!!.onItemLongClick(childView, rView.getChildPosition(childView))
}
}
})
}

override fun onInterceptTouchEvent(rv: RecyclerView?, e: MotionEvent?): Boolean {
val childView = rv!!.findChildViewUnder(e!!.getX(), e.getY())

if (childView != null && mListener != null && mGestureDetector!!.onTouchEvent(e)) {
mListener!!.onItemClick(childView, rv.getChildPosition(childView))
}
return false
}

override fun onTouchEvent(rv: RecyclerView?, e: MotionEvent?) {

}


override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
}
}

클래스 생성

 

 

 

 

// 리사이클러뷰 클릭 이벤트
fun recyclerViewClickEvent(context: Context) {
contentList_RecyclerView.addOnItemTouchListener(content_RecyclerVier_ClickEvent(this, contentList_RecyclerView,
object : content_RecyclerVier_ClickEvent.OnItemClickListener {
override fun onItemClick(view: View, position: Int) {
Toast.makeText(context, ""+position, Toast.LENGTH_SHORT).show()

}

override fun onItemLongClick(view: View, position: Int) {

}
}))
}

이벤트

728x90
반응형