recyclerview onclicklistener code example
Example 1: recyclerview onclicklistener
@Override
public void onClick(final View view) {
int itemPosition = mRecyclerView.getChildLayoutPosition(view);
String item = mList.get(itemPosition);
Toast.makeText(mContext, item, Toast.LENGTH_LONG).show();
}
Example 2: recyclerview onclicklistener kotlin
val data = list[position]holder.itemView.setOnClickListener { cellClickListener.onCellClickListener(data)}
Example 3: recyclerview onclicklistener kotlin
interface CellClickListener { fun onCellClickListener(data: Model)}
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++){
}
}
}
Example 5: 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() }}