Skip to content

Instantly share code, notes, and snippets.

@theonlyanil
Last active September 18, 2019 12:45
Show Gist options
  • Save theonlyanil/14c2224665ebe10bf9423b339f18b87f to your computer and use it in GitHub Desktop.
Save theonlyanil/14c2224665ebe10bf9423b339f18b87f to your computer and use it in GitHub Desktop.
How to implement Item Click on RecyclerView (Easiest Approach)
/* Changes start from here... */
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
// each data item is just a string in this case
public TextView mTextView;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
mTextView = itemView.findViewById(R.id.textView);
}
@Override
public void onClick(View view)
{
// get the position on recyclerview.
int pos = getLayoutPosition();
SingleItemClick(pos); // click on recyclerview item.
}
}
/* Changes End Here */
@theonlyanil
Copy link
Author

theonlyanil commented Sep 18, 2019

@nvinai It's a custom function which takes integer pos and passes to other variables. Useful if you have many lines of code in onClick, else you can write your code just here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment