recyclerview with onclicklistener in android code example
Example 1: recyclerview onclicklistener kotlin
val data = list[position]holder.itemView.setOnClickListener { cellClickListener.onCellClickListener(data)}
Example 2: recyclerview onclicklistener kotlin
interface CellClickListener { fun onCellClickListener(data: Model)}
Example 3: 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 4: recyclerview button onclicklistener
TextView textView;
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
textView = (TextView)view.findViewById(android.R.id.text1);
}
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "position = " + getLayoutPosition(), Toast.LENGTH_SHORT).show();
if(getLayoutPosition()==0){
}else if(getLayoutPosition()==1){
}else if(getLayoutPosition()==2){
}else if(getLayoutPosition()==3){
}else if(getLayoutPosition()==4){
}else if(getLayoutPosition()==5){
}
for(int i = 0; i<exampleList.size(); i++){
}
}
}