I\'m using RecyclerView
with CardView
and inside the CardView
have 2 buttons. Now, have implemented the onClick
events by imp
Here my solution if it could help someone.
You could click in one item of the recyclerview or one of the two buttons.
The interface implements:
onStudentItemClick, for click in the recyclerview item
onChatItemClick and onAbsenceItemClick for click the buttons
With
holder.imgChat.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mOnClickListener.onChatItemClick(mStudents.get(index));
}
});
I set the click listener for one button
And in the class StudentViewHolder I implement OnClickListener and override onClik
@Override
public void onClick(View v) {
if (mOnClickListener != null){
int clickedPosition = getAdapterPosition();
mOnClickListener.onStudentItemClick(mStudents.get(clickedPosition));
}
}
Here the Adapter:
public class StudentsAdapter extends RecyclerView.Adapter {
private ListItemClickListener mOnClickListener;
private ArrayList mStudents;
public interface ListItemClickListener {
void onStudentItemClick(Student student);
void onChatItemClick(Student student);
void onAbsenceItemClick(Student student);
}
public StudentsAdapter(ArrayList students, ListItemClickListener listener) {
mStudents = students;
mOnClickListener = listener;
}
@Override
public StudentsViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
Context context = viewGroup.getContext();
int layoutIdForListItem = R.layout.student_list_item;
LayoutInflater inflater = LayoutInflater.from(context);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
StudentsViewHolder viewHolder = new StudentsViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(StudentsViewHolder holder, int position) {
final Integer index = position;
holder.tvStudent.setText(mStudents.get(position).nameForList());
holder.imgChat.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mOnClickListener.onChatItemClick(mStudents.get(index));
}
});
holder.imgAbsence.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
mOnClickListener.onAbsenceItemClick(mStudents.get(index));
}
});
}
public Student getItem(Integer position){
return mStudents.get(position);
}
@Override
public int getItemCount() {
return mStudents.size();
}
class StudentsViewHolder extends RecyclerView.ViewHolder
implements OnClickListener {
ImageView imgStudent;
TextView tvStudent;
ImageButton imgChat, imgAbsence;
public StudentsViewHolder(View itemView) {
super(itemView);
imgStudent = (ImageView) itemView.findViewById(R.id.img_student);
tvStudent = (TextView) itemView.findViewById(R.id.tv_student);
imgChat = (ImageButton) itemView.findViewById(R.id.btn_chat_student);
imgAbsence = (ImageButton) itemView.findViewById(R.id.btn_absence);
itemView.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (mOnClickListener != null){
int clickedPosition = getAdapterPosition();
mOnClickListener.onStudentItemClick(mStudents.get(clickedPosition));
}
}
}
}
Fragment the implement the interface:
public class SubjectDetailFragment extends Fragment implements StudentsAdapter.ListItemClickListener {
...
@Override
public void onStudentItemClick(Student student) {
StudentDetailFragment detailFragment = StudentDetailFragment.newInstance();
Bundle bundle = new Bundle();
bundle.putParcelable("student", student);
detailFragment.setArguments(bundle);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_layout, detailFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
@Override
public void onChatItemClick(Student student) {
...
}
@Override
public void onAbsenceItemClick(Student student) {
...
}