我正在尝试在ViewHolder类中设置onClickListeners,但是我需要每个位置。如何将位置从onBindViewHolder传递给我 ViewHolder类?
@NonNull
@Override
public AdapterAllComments.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.model_rv_allcomments_container, viewGroup, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull AdapterAllComments.ViewHolder viewHolder, int position) {
}
@Override
public int getItemCount() {
return mCommentId.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
LinearLayout viewHolderLayout;
public ViewHolder(@NonNull View itemView) {
super(itemView);
viewHolderLayout = itemView.findViewById(R.id.item_child_layout);
}
}
答案 0 :(得分:2)
这个适配器来自我的其他项目:
public class ChildrenRvAdapter extends RecyclerView.Adapter<ChildrenRvAdapter.ViewHolder> {
private List<String> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
private Context context;
public ChildrenRvAdapter(Context context, List<String> data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = mInflater.inflate(R.layout.children_rv_row, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
String name = mData.get(position);
holder.tv_name.setText(name);
setAnimation(holder.itemView, position);
}
@Override
public int getItemCount() {
return mData.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
TextView tv_name;
Button btn_remove;
ViewHolder(View itemView) {
super(itemView);
tv_name = itemView.findViewById(R.id.tv_name);
btn_remove = itemView.findViewById(R.id.btn_remove);
btn_remove.setOnClickListener(this);
}
@Override
public void onClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
String getItem(int id) {
return mData.get(id);
}
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
public interface ItemClickListener {
void onItemClick(View view, int position);
}
}
}
因此,您需要在适配器中创建interface
,并在本地mClickListener
中声明它,并在ViewHolder类和setonclick
中创建viewholder
的地方实现它:
ViewHolder(View itemView) {
super(itemView);
tv_name = itemView.findViewById(R.id.tv_name);
btn_remove = itemView.findViewById(R.id.btn_remove);
btn_remove.setOnClickListener(this);
}
请注意,我将其设置在button
上,但是您可以将其设置在itemView
上
也可以在任何具有view
的{{1}}上使用,因此您可以使用:
setOnClickListener
然后从您的活动或片段以及无论使用适配器的任何地方,只需实现itemView.setOnClickListener(this);
onlick
,并在初始化适配器后,使用我们在适配器中创建的interface
的方法即可适配器中的界面
public void setClickListener
活动:
public void setClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
和活动public class ChildrenEditDialog extends Dialog implements IChildren.IView, ChildrenRvAdapter.ItemClickListener
或其他地方:
onCreate
您的活动/片段/对话/等中有onlcick ...
答案 1 :(得分:1)
使用ViewHolders getAdapterPosition()
获取ViewHolder的当前位置。
答案 2 :(得分:0)
要获取onBindViewHolder的当前位置,可以使用ViewHolder类getAdapterPosition()
。
例如,
public class ViewHolder extends RecyclerView.ViewHolder{
LinearLayout viewHolderLayout;
viewHolderLayout = itemView.findViewById(R.id.item_child_layout);
public ViewHolder(@NonNull View itemView) {
super(itemView);
viewHolderLayout.setOnClickListener(v -> {
lastClickedPosition = getAdapterPosition();
});
}
}