item click listener recyclerview android kotlin code example
Example 1: how to set onClickListener to RecyclerView kotlin
private fun CreateRecyclerView(){
val recycler_list = findViewBiId<RecyclerView>(R.id.myRecyclerViewId)
recycler_list.setOnTouchListener { v, _ ->
if (layoutAttachment.visibility == View.VISIBLE || menuButtons.visibility == View.VISIBLE) {
closeOpenDialogs()
v.performClick()
} else {
false
}
}
recycler_list.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
recycler_list.setHasFixedSize(true)
recycler_list.adapter = myAdapter
recycler_list.itemAnimator = DefaultItemAnimator()
}
private fun closeOpenDialogs(){
if (layoutAttachment.visibility == View.VISIBLE || menuButtons.visibility == View.VISIBLE) {
layoutAttachment.visibility == View.GONE
menuButtons.visibility == View.GONE
}
}
Example 2: recyclerview onclicklistener kotlin
interface CellClickListener { fun onCellClickListener(data: Model)}
Example 3: recyclerview onclicklistener kotlin
class MainActivity : AppCompatActivity(), CellClickListener { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val recyclerView: RecyclerView = findViewById(R.id.recycler_view) recyclerView.layoutManager = LinearLayoutManager(this) recyclerView.adapter = Adapter(this, fetchList(), this) } private fun fetchList(): ArrayList<Model> { val list = arrayListOf<Model>() for (i in 0..9) { val model = Model(R.drawable.ic_collections_black_24dp, "Title : $i", "Subtitle : $i") list.add(model) } return list } override fun onCellClickListener() { Toast.makeText(this,"Cell clicked", Toast.LENGTH_SHORT).show() }}