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
반응형
'Kotlin' 카테고리의 다른 글
adapter에서 엑티비티나, 프래그먼트로 데이터 보내기, 이벤트 (0) | 2019.02.19 |
---|---|
코틀린 배열 초기화 방법 (0) | 2019.02.11 |
코틀린 안드로이드 RecyclerView GridLayout 사용하기 (0) | 2019.02.09 |
코틀린 ArrayList<Object> 인텐트 보내기 (0) | 2019.01.29 |
코틀린 안드로이드 videoView 사용하기 (0) | 2019.01.28 |