Obtenir la position de View dans onCreateViewHolder

Je suis à l'aide d'un RecyclerView avec une seule ligne de mise en page avec une ImageView et un TextView.

Je veux mettre en œuvre un OnClickListener pour la Vue et non pas pour séparer ViewHolder objets. Comment puis-je obtenir la position de la vue dans la Carte?

Je suis en train de supprimer les commentaires sur la souris, mais je ne peut pas sélectionner l'cliqué sur Afficher. J'ai ajouté des choses à faire dans la ligne appropriée.

public class CommentAdapter extends RecyclerView.Adapter<CommentAdapter.ViewHolder> {
/** List of Comment objects */
private List<Comment> mCommentList;
/** Database with Comment objects */
private CommentsDataSource mDataSource;
/**
* Construcutor for CommentAdapter
*
* @param commentList   List of Comment objects
* @param dataSource    Database with Comment objects
*/
public CommentAdapter(List<Comment> commentList, CommentsDataSource dataSource) {
this.mCommentList = commentList;
this.mDataSource = dataSource;
}
/**
* Add Comment objects to RecyclerView
*
* @param position  The position where the Comment object is added
* @param comment   Comment Object
*/
public void add(int position, Comment comment) {
mCommentList.add(position, comment);
notifyItemInserted(position);
}
/**
* Remove Comment objects from RecyclerView
*
* @param comment Comment Object
*/
public void remove(Comment comment) {
int position = mCommentList.indexOf(comment);
//Avoid double tap remove
if (position != -1) {
mCommentList.remove(position);
mDataSource.deleteComment(comment);
notifyItemRemoved(position);
}
}
@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_line_row, parent, false);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//TODO get position
remove(mCommentList.get(getItemCount() - 1));
}
});
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Comment comment = mCommentList.get(position);
holder.comment.setText(comment.getComment());
}
@Override
public int getItemCount() {
return mCommentList.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
/** ImageView icon */
public ImageView icon;
/** TextView comment */
public TextView comment;
/**
* Constructor for ViewHolder
*
* @param itemView Layout for each row of RecyclerView
*/
public ViewHolder(final View itemView) {
super(itemView);
icon = (ImageView) itemView.findViewById(R.id.icon);
comment = (TextView) itemView.findViewById(R.id.comment);
}
}
}

source d'informationauteur Thomas Mohr | 2015-04-24