In Listview, we just do something like this :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
listViewUser.setOnItemClickListener(new AdapterView.OnItemClickListener() { | |
@Override | |
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { | |
Toast.makeText(MainActivity.this, "Position : " + position, Toast.LENGTH_SHORT).show(); | |
} | |
}); |
How about on Recyclerview?
Actually there are many ways to implement onClickListener in RecyclerView. If you ask at Stackoverflow, they will give many answers to do this.
From this article i will give you one of many way how to implement onClickListener in RecyclerView.
See also : how to put different view/layout on recyclerview's item.
I do this on Adapter class. For simply, give an ID on RelativeLayout or LinearLayour in your Adapter's layout.
After that, define the LinearLayour or RelativeLayout in your Holder. Next, do the OnClick in onBindViewHolder method.
For simply, is using this project for example, inside OnBindViewHolder method just do like below :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void onBindViewHolder(Holder holder, int position) { | |
//get the user by its position | |
final UserModel user = mListUser.get(position); | |
System.out.println("JUMLAH : " + mListUser.size()); | |
holder.tvUserName.setText("User Name : " + user.getName() +"\n" + | |
"User Age : " + user.getAge()); | |
//this line for onClick | |
holder.llItemUser.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
Toast.makeText(mContexl, "User " + user.getName() + " is clicked", Toast.LENGTH_SHORT).show(); | |
} | |
}); | |
} |
Done! Hope it helps, thank you.
EmoticonEmoticon