I need to get the clicked item position in Android RecyclerView which is created in a fragment. Since recyclerview doesn't have any listener like listview, i searched and find some methods on the internet. Some doesn't worked and some doesn't provided the position value. Then i used my own refined code from examples i saw and it worked after spending a whole day. Now my problem is that i need to know any drawbacks of this including performance issues. or even better ways of doing this(if it is possible).
also can someone help me to reuse this code for multiple recyclerview in same project
RecyclerButtonClick.java
public class RecyclerButtonClick implements View.OnClickListener {
YourFragment yourFragment=new YourFragment();
public int pos;
public RecyclerButtonClick(int position){
this.pos=position;
}
@Override
public void onClick(View v) {
yourFragment.recyclerButtonClicked(pos);
}
}
Now that is set we can write what to do in a function in the Fragment java file. It is then called by creating and instance of the Fragment.
public void recyclerButtonClicked(int position) {
...
//position will have the index value of the rows in RecyclerView.
...
}
RecyclerButtonClick class is called in setOnClickListener in the onBindViewHolder() of the RecycleViewAdapter
public void onBindViewHolder(MyViewHolder holder, int position) {
...
holder.YOUR_BUTTON.setOnClickListener(new RecyclerButtonClick(position));
...
}
help much appreciated
update on this github link if you want. https://github.com/abhinanduN/Android--RecyclerView-onClick-Listener/blob/master/README.md