Recyclerview - How Do I Pass The Position Of Onbindviewholder To My Viewholder Class?
Solution 1:
this adapter from my other project :
publicclassChildrenRvAdapterextendsRecyclerView.Adapter<ChildrenRvAdapter.ViewHolder> {
private List<String> mData;
private LayoutInflater mInflater;
private ItemClickListener mClickListener;
private Context context;
publicChildrenRvAdapter(Context context, List<String> data) {
this.mInflater = LayoutInflater.from(context);
this.mData = data;
this.context = context;
}
@Overridepublic ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Viewview= mInflater.inflate(R.layout.children_rv_row, parent, false);
returnnewViewHolder(view);
}
@OverridepublicvoidonBindViewHolder(ViewHolder holder, int position) {
Stringname= mData.get(position);
holder.tv_name.setText(name);
setAnimation(holder.itemView, position);
}
@OverridepublicintgetItemCount() {
return mData.size();
}
publicclassViewHolderextendsRecyclerView.ViewHolder implementsView.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);
}
@OverridepublicvoidonClick(View view) {
if (mClickListener != null) mClickListener.onItemClick(view, getAdapterPosition());
}
}
String getItem(int id) {
return mData.get(id);
}
publicvoidsetClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
publicinterfaceItemClickListener {
voidonItemClick(View view, int position);
}
}
}
so you create interface
in your adapter and also declare it in local mClickListener
and implement it in ViewHolder class and setonclick
where you create the 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);
}
just note that i set it on a button
but you can set it on itemView
also on any view
that have the setOnClickListener
so you can use :
itemView.setOnClickListener(this);
then from your activity or fragment and wherever using the adapter, just implemet the onlick
interface
and after initializing the adapter use the method that we created public void setClickListener
in our adapter, to local interface in the adapter
publicvoidsetClickListener(ItemClickListener itemClickListener) {
this.mClickListener = itemClickListener;
}
activity:
publicclassChildrenEditDialogextendsDialogimplementsIChildren.IView, ChildrenRvAdapter.ItemClickListener
and in activity onCreate
or somewhere else:
childrenRvAdapter = newChildrenRvAdapter(c, dataSource);
childrenRvAdapter.setClickListener(this);
and you have onlcick in your activity/fragment/dialog/etc...
Solution 2:
Use ViewHolders getAdapterPosition()
for getting current position of ViewHolder.
Solution 3:
To get the current position of the onBindViewHolder to ViewHolder class getAdapterPosition()
can be used.
As an example,
publicclassViewHolderextendsRecyclerView.ViewHolder{
LinearLayout viewHolderLayout;
viewHolderLayout = itemView.findViewById(R.id.item_child_layout);
publicViewHolder(@NonNull View itemView) {
super(itemView);
viewHolderLayout.setOnClickListener(v -> {
lastClickedPosition = getAdapterPosition();
});
}
}
Post a Comment for "Recyclerview - How Do I Pass The Position Of Onbindviewholder To My Viewholder Class?"