Get the position of clicked item on Recycler View

前端 未结 3 996
旧时难觅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

提交回复
热议问题