这是我的解决方案。这对我有用
ViewHolder 类
public class StudentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView textViewName;
public TextView textViewRoll;
public TextView textViewAge;
public MyViewHolderClickListener mListener;
private String TAG= getClass().getSimpleName();
public static interface MyViewHolderClickListener{
public void onTextViewNameClick(View view, int position);
public void onTextViewRollClick(View view, int position);
}
public StudentViewHolder(View itemView) {
super(itemView);
textViewName = (TextView) itemView.findViewById(R.id.textViewName);
textViewRoll = (TextView) itemView.findViewById(R.id.textViewRoll);
textViewAge = (TextView) itemView.findViewById(R.id.textViewAge);
textViewName.setOnClickListener(this);
textViewRoll.setOnClickListener(this);
}
public void bindToStudent(Student student){
if (student == null) {
return;
}
textViewName.setText("Name: " + student.getName());
textViewRoll.setText( "Roll: " + student.getRoll());
textViewAge.setText( "Age: " + student.getAge());
}
public void setCustomOnClickListener(MyViewHolderClickListener listener ){
this.mListener = listener;
}
@Override
public void onClick(View view) {
Log.d(TAG, "onClick at " + getAdapterPosition());
Log.d(TAG, "id: " + view.getId());
if( mListener!= null ){
switch (view.getId()) {
case R.id.textViewName:
mListener.onTextViewNameClick(view, getAdapterPosition());
break;
case R.id.textViewRoll:
mListener.onTextViewRollClick(view, getAdapterPosition());
break;
default:
break;
}
}
}
}
适配器类
public class StudentListAdapter extends FirebaseRecyclerAdapter<Student, StudentViewHolder> {
Activity parentActivity;
StudentViewHolder.MyViewHolderClickListener myViewHolderClickListener;
private String TAG = getClass().getSimpleName();
public StudentListAdapter(Activity mActivity, Class<Student> modelClass, int modelLayout, Class<StudentViewHolder> viewHolderClass, Query ref) {
super(modelClass, modelLayout, viewHolderClass, ref);
parentActivity = mActivity;
}
@Override
public StudentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
StudentViewHolder viewHolder = super.onCreateViewHolder(parent, viewType);
// adding my MyViewHolderClickListener here
viewHolder.setCustomOnClickListener(myViewHolderClickListener);
return viewHolder;
}
@Override
protected void populateViewHolder(StudentViewHolder viewHolder, final Student model, final int position) {
Log.d(TAG, model.toString());
Log.d(TAG, getRef(position).getKey());
// bind view
viewHolder.bindToStudent(model);
}
public void setMyViewHolderClickListener(StudentViewHolder.MyViewHolderClickListener listener){
this.myViewHolderClickListener = listener;
}
}
在活动课上
mAdapter = new StudentListAdapter(this, Student.class, R.layout.list_item, StudentViewHolder.class, studentQuery);
mAdapter.setMyViewHolderClickListener(new StudentViewHolder.MyViewHolderClickListener() {
@Override
public void onTextViewNameClick(View view, int position) {
Log.d(TAG, "Name Click at " +position);
}
@Override
public void onTextViewRollClick(View view, int position) {
Log.d(TAG, "Roll Click at " +position);
}
});
recyclerView.setAdapter(mAdapter);