0

How to get the position for clicked button inside the RecyclerView items

Here's my onBindViewHolder :

  public void onBindViewHolder(MyViewHolder holder, int position) {
    Masar masar=masrList.get(position);
    holder.masarName.setText(masar.getMasarTitle());
    holder.masarDesc.setText(masar.getMasarDescreption());

    //How to get the Position 

    holder.masarImg.setImageResource(masar.getMasarImg());

    holder.mapBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v ) {
            //if you need position, just use recycleViewHolder.getAdapterPosition();
            Intent intent = new Intent(v.getContext(), MapsActivity.class);
            mContext.startActivity(intent);
        }
    });

}
Yousef Ahmad
  • 63
  • 1
  • 3
  • 10

2 Answers2

5

If you need in onBindViewHolder only then you can use

holder.getAdapterPosition();

and if you need this position clicked in activity and fragment then you have to use callbacks from holder to activity and fragment and have to pass the same getAdapterPosition();

Edit: Added sample code for listening position click in fragment/activity

step 1: make an interface or callback

public interface RecyclerViewClickListener {

    void onClick(View view, int position);
}

step 2: While initializing adapter class in fragment or activity pass the above-created reference as a parameter

public YourAdapter(List<SomeModel> modelList, RecyclerViewClickListener listener){
 this.clickListener = listener;
}

step 3: In your ViewHolder or similar Class for view initialization do something like this

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    private Button mapBtn;

    ViewHolder(View v, RecyclerViewClickListener listener) {
        super(v);
        mapBtn = findViewById(R.id.mapBtn);
        mListener = listener;
        mapBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        mListener.onClick(view, getAdapterPosition());
    }
}

you will get the position in your fragment or activity where you have passed the callback reference while initializing the adapter.

vikas kumar
  • 10,447
  • 2
  • 46
  • 52
2

Use holder.getAdapterPosition();

public void onBindViewHolder(final MyViewHolder holder, int position) {
Masar masar=masrList.get(position);
holder.masarName.setText(masar.getMasarTitle());
holder.masarDesc.setText(masar.getMasarDescreption());

//How to get the Position 

holder.masarImg.setImageResource(masar.getMasarImg());

holder.mapBtn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v ) {
        Toast.makeText(getContext(), "The position is: "+holder.getAdapterPosition(), Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(v.getContext(), MapsActivity.class);
        mContext.startActivity(intent);
    }
});}
Joey Dalu
  • 1,113
  • 9
  • 13
  • 2
    Never make position final instead use holder.getAdapterPosition(). Makin position final produces incongruencies. Android Studio even provides a lint with more information. – cutiko Feb 11 '18 at 19:12
  • You have to give me an extra reputation , because i helped to give new info :p @JoeyDalu – Yousef Ahmad Feb 11 '18 at 19:24