简体   繁体   English

无法在RecylerView Adapter中获得视图的位置

[英]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.

相关问题 通过使用带有 .get(position) 的回收器视图适配器,我无法从回收器视图 java 中的 edittext 获取文本 - I can't get the text from edittext in recycler view java by using recycler view adapter with .get(position) RecylerView onClick获取项目位置 - RecylerView onClick get position of item 无法从转换视图获取父级职位 - Can't get parent position from convert view 无法使用简单适配器查看数据Sqlite - Can't View Data Sqlite with Simple Adapter 不能在 RecyclerView 适配器中使用 Intent - can't use Intent inside a RecyclerView Adapter 如何在RecylerView布局中修复按钮的位置? 我不希望按钮与RecyclerView中的图像一起滚动 - How can I fix the position of buttons in a RecylerView layout? I don't want the buttons to scroll along with the image in RecyclerView 在不清除适配器的情况下重置 RecylerView - Reset RecylerView without clear the Adapter 无法获取设备位置 - Can't get device position 如何从“共享首选项”中获取数据到RecylerView Adapter中并传递到下一个片段 - How to get data from Shared Preferences into RecylerView Adapter and pass to next fragment Recyclerview内的Recyclerview,获取父适配器内子行的点击位置 - Recyclerview inside Recyclerview , get click position of child row inside parent Adapter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM