Get the position of clicked item on Recycler View

前端 未结 3 997
旧时难觅i
旧时难觅i 2021-01-15 17:57

I have implement a RecyclerView and it works fine. I have an ArrayList which contains the data for the recycler view. The layout of each item is co

相关标签:
3条回答
  • 2021-01-15 18:51

    Try getAdapterPosition() from inside the view holder so that you may get the adapter position of the click the user made.

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    
       public ViewHolder(View itemView) {
           super(itemView);
           itemView.setOnClickListener(this);
       }
    
       @Override
       public void onClick(View v) {
           Toast.makeText(context, String.valueOf(getAdapterPosition()), Toast.LENGTH_SHORT).show();
           }
     }
    

    For more in getAdapterPosition() follow this link

    0 讨论(0)
  • 2021-01-15 18:52

    Try this

    public class ClosetListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
        ClosetListAdapter (CallBack callback){
           this.callback = callback
        }
    
        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder baseholder, int position) {
            ViewHolder holder = (ViewHolder) baseholder;
            holder.setPosition(position);
            holder.name.setText(product.getName());
        }
    
        static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    
            public TextView name = null;
    
            private OnProductClickListener onProductClickListener;
    
            public ViewHolder(View itemView, OnProductClickListener onClickListener) {
                super(itemView);
    
                name = (TextView) itemView.findViewById(R.id.item_name);
                itemView.setOnClickListener(this)
    
            }
    
            public void setProdcut(Product product) {
                this.product = product;
            }
    
            @Override
            public void onClick(View v) {
                if (callback!= null) {
                    callback.itemClicked(pos);
                }
            }
    
            public void setPosition(int position){
                this.pos = position;
            }
        }
    
        interface CallBack {
           void itemClicked(int position);
        }
    }
    
    0 讨论(0)
  • 2021-01-15 18:58

    I've also faced the same problem.

    I wanted to find of the position of the clicked/selected item of the RecyclerView() and perform some specific operations on that particular item.

    getAdapterPosition() method works like a charm for these kind of stuff. I found this method after a day of long research and after trying numerous other methods.

    int position = getAdapterPosition();
    Toast.makeText(this, "Position is: "+position, Toast.LENGTH_SHORT).show();
    

    You do not have to use any extra method. Just create a global variable named 'position' and initialize it with getAdapterPosition() in any of the major method of the adapter (class or similar).

    Here is a brief documentation from this link.

    getAdapterPosition added in version 22.1.0 int getAdapterPosition () Returns the Adapter position of the item represented by this ViewHolder. Note that this might be different than the getLayoutPosition() if there are pending adapter updates but a new layout pass has not happened yet. RecyclerView does not handle any adapter updates until the next layout traversal. This may create temporary inconsistencies between what user sees on the screen and what adapter contents have. This inconsistency is not important since it will be less than 16ms but it might be a problem if you want to use ViewHolder position to access the adapter. Sometimes, you may need to get the exact adapter position to do some actions in response to user events. In that case, you should use this method which will calculate the Adapter position of the ViewHolder.

    Happy to help. Feel free to ask doubts.

    0 讨论(0)
提交回复
热议问题