问题描述
我正在使用带有 ImageView 和 TextView 的单行布局的 RecyclerView.
I am using a RecyclerView with a single row layout with an ImageView and a TextView.
我想为视图而不是为单独的 ViewHolder 对象实现一个 OnClickListener.如何在 Adapter 中获取视图的位置?
I want to implement a OnClickListener for the View and not for seperate ViewHolder objects. How can i get the position of the view in the Adapter?
现在我正在删除点击评论,但我无法选择点击的视图.我在适当的行中添加了一个 TODO.
Right now i'm deleting comments on click, but i cannot select the clicked View. I added a TODO in the appropriate line.
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);
}
}
}
推荐答案
您不能在回调中使用 onBindViewHolder
的 position
参数.如果上面添加了新项目,RecyclerView 不会重新绑定您的项目,因此该位置过时.相反,RecyclerView 在 ViewHolder 上提供了一个 getAdapterPosition
方法.
You cannot use the position
parameter of onBindViewHolder
in a callback.If a new item is added above, RecyclerView will not rebind your item so the position is obsolete.Instead, RecyclerView provides a getAdapterPosition
method on the ViewHolder.
@Override
public ViewHolder onCreateViewHolder(final ViewGroup parent, int viewType) {
final View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.single_line_row, parent, false);
final ViewHolder holder = new ViewHolder(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final int position = holder.getAdapterPosition();
if (position != RecyclerView.NO_POSITION) {
remove(mCommentList.get(position));
}
}
});
return holder;
}
我添加了 position != RecyclerView.NO_POSITION
检查,因为当项目被移除时,RecyclerView 将淡出视图,因此它仍可能被用户点击,但其适配器位置将返回 NO_POSITION
.
I've added position != RecyclerView.NO_POSITION
check because when item is removed, RecyclerView will fade out the View so it may still be clicked by the user but its adapter position will return NO_POSITION
.
这篇关于在 onCreateViewHolder 中获取 View 的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!