[英]Can't get the position for view inside RecylerView Adapter
My recycler view adapter is as follows 我的回收站视图适配器如下
@Override
public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.contacts_list_item, viewGroup, false);
return new ContactViewHolder(itemView);
}
@Override
public void onBindViewHolder(ContactViewHolder viewHolder, int position) {
super.onBindViewHolder(viewHolder, position);
viewHolder.removeContact.setTag(position);
Contact contact = contacts.get(position);
viewHolder.contactName.setText(contact.getContactName());
viewHolder.contactNumber.setText(contact.getContactNumber());
viewHolder.removeContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Integer taggedPosition = (Integer) v.getTag();
Log.wtf("ad","Posirtion"+ Integer.toString(taggedPosition));
contacts.remove(Integer.parseInt(Integer.toString(taggedPosition)));
notifyItemRemoved(Integer.parseInt(Integer.toString(taggedPosition)));
}
});
}
@Override
public int getItemCount() {
return contacts.size();
}
public void removeItem(int pos) {
contacts.remove(pos);
// notifyItemRemoved(pos);
}
public void add(Contact contact, int position) {
contacts.add(position, contact);
notifyItemInserted(position);
}
and my view holder is 我的观点持有者是
class ContactViewHolder extends RecyclerView.ViewHolder {
public TextView contactName,contactNumber;
public ImageButton removeContact;
public ContactViewHolder(View itemView) {
super(itemView);
contactName= (TextView) itemView.findViewById(R.id.tv_contactName);
contactNumber= (TextView) itemView.findViewById(R.id.tv_contactNumber);
removeContact= (ImageButton) itemView.findViewById(R.id.ib_removeItem);
}
}
The issue is i cant seem to get the postion of viewHolder.removeContact
.All i want to achieve is to remove the row when the viewHolder.removeContact
is clicked.I have tried getting the postion by getTag() method also but it gives me only one postion ie 0. 问题是我似乎无法获得viewHolder.removeContact
的位置。我想要实现的是viewHolder.removeContact
viewHolder.removeContact时删除该行。我也尝试通过getTag()方法获得该位置,但它只给我一个位置,即0。
Thanks to @pskink.Thanks. 感谢@ pskink.Thanks。 Solved the issue all i did was to pass the reference of my adapter and contacts
arraylist to my view holder class like this 解决的问题是我所做的就是将适配器和contacts
arraylist的引用传递给这样的视图持有者类
@Override
public ContactViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View itemView = LayoutInflater.from(viewGroup.getContext()).
inflate(R.layout.contacts_list_item, viewGroup, false);
return new ContactViewHolder(itemView,this,contacts);
}
where itemView
is the View to be returned, this
is the adapter refrence and contacts
is the arraylist i have to updated on button click.My ViewHolder class loooks like this 其中itemView
是要返回的View, this
是适配器参考,而contacts
是我必须在单击按钮时更新的arraylist.My ViewHolder类看起来像这样
class ContactViewHolder extends RecyclerView.ViewHolder {
public TextView contactName,contactNumber;
public ImageButton removeContact;
private List<Contact> contacts1;
private ContactsAdapter contactsAdapter1;
public ContactViewHolder(View itemView, ContactsAdapter contactsAdapter, final List<Contact> contacts) {
super(itemView);
this.contacts1=contacts;
this.contactsAdapter1=contactsAdapter;
contactName= (TextView) itemView.findViewById(R.id.tv_contactName);
contactNumber= (TextView) itemView.findViewById(R.id.tv_contactNumber);
removeContact= (ImageButton) itemView.findViewById(R.id.ib_removeItem);
removeContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
contacts1.remove(getPosition());
contactsAdapter1.notifyItemRemoved(getPosition());
}
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.