I have a simple RecyclerView with an Interface that is linked to the activity. The Issue is the items in the recyclerView need double click in order to perform an action.
This is the AllProductsAdapter Code :
public class AllProductsAdapter extends RecyclerView.Adapter<AllProductsAdapter.ViewHolder> {
RecyclerViewClickInterfaceNew recyclerViewClickInterface;
List<ProductsModel> productsModelList;
Context context;
public AllProductsAdapter(List<ProductsModel> productsModelList, Context context, RecyclerViewClickInterfaceNew recyclerViewClickInterface) {
this.productsModelList = productsModelList;
this.context = context;
this.recyclerViewClickInterface = recyclerViewClickInterface;
}
#NonNull
#Override
public AllProductsAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.product_items, parent, false);
return new AllProductsAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull AllProductsAdapter.ViewHolder holder, int position) {
ProductsModel productsModel = productsModelList.get(position);
PicassoTrustAll.getInstance(context)
.load(productsModel.getProduct_image())
.into(holder.catImage);
}
#Override
public int getItemCount() {
return productsModelList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
ImageView catImage;
TextView catName;
CardView categoryCard;
public ViewHolder(#NonNull View itemView) {
super(itemView);
catImage = itemView.findViewById(R.id.catImage);
catName = itemView.findViewById(R.id.catName);
categoryCard = itemView.findViewById(R.id.categoryCard);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (recyclerViewClickInterface != null)
recyclerViewClickInterface.onItemClick(v, getAdapterPosition());
}
});
}
}
This is my RecyclerViewClickInterfaceNew :
public interface RecyclerViewClickInterfaceNew {
void onItemClick(View view , int position);
void onLongItemClick(int position);
}
This is my MainActivity code :
#Override
public void onItemClick(View view , int position) {
ImageView img= view.findViewById(R.id.catImage);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(AllProductsList.this, "Imgclciked", Toast.LENGTH_SHORT).show();
}
});
}
Here in the MainActivity, the Toast shows after clicking 2 times on the Image. How can i fix it to a normal single click ? Please let me know your answer.
From this code
#Override
public void onItemClick(View view , int position) {
//when different item is FIRST clicked, a new instance of catImage is produced
ImageView img= view.findViewById(R.id.catImage);
//the img onClick is set here to require a second click
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(AllProductsList.this, "Imgclciked", Toast.LENGTH_SHORT).show();
}
});
}
For each new item on the Recycler View, you reset the click handling.
I'll suggest you set the onClickListener ON the ViewHolder. It's not the best practice but it'll suffice for your code
I think that's because of you are using NestedScrollView ,
you should add this line to your recyclerview
android:nestedScrollingEnabled="false"
I'm new on Android material design and wanna use Recycle View instead of List View but having problem on implement OnClickListener.
I find out that RecyclerView.Adapter a bit different from ListView.Adapter.
first I impelement OnClick listener in onBindViewHolder but it returns wrong item numbers and lead to out of-bound after some removing.
#Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
final int index = i ;
personViewHolder.cv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
... items.get(index).gettext();
}
so I find out that I should implement OnClikListener in my ViewHolder class as this link.
but in this case never inter to OnClick.
public static class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
#Override
public void onClick(View v) {
Log.i("log","position="+getAdapterPosition());
}
}
public CardViewAdapter(List<MessageTO> persons) {
this.items = persons;
}
#Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
#Override
public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.deposite_card_view, viewGroup, false);
PersonViewHolder pvh = new PersonViewHolder(v);
return pvh;
}
#Override
public void onBindViewHolder(PersonViewHolder personViewHolder, int i) {
...
}
also I find out that I should do my Initializing item values all in onBindViewHolder is it right or not? I am very confused.
First you need to set the onclicklistener as shown below. Then it should work.
public static class PersonViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ViewHolder(View itemView) {
super(itemView);
.
.
.
itemView.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Log.i("log","position="+getAdapterPosition());
}
}
You need to call viewHolder.itemView.setOnClickListener(viewHolder) somewhere to make it work
When a button is clicked, I'm trying to delete the row. At the very bottom, I call the delete method that I made. However it gives me an error (in the question). I tried making the delete method static, tried everything that android suggested
public class myAdapter extends RecyclerView.Adapter<myAdapter.ViewHolder> {
private List<numbers> mNumbers;
public myAdapter (List<numbers> numbers) {
mNumbers = numbers;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_slot, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bindNumbers(mNumbers.get(position));
}
#Override
public int getItemCount() {
return mNumbers.size();
}
public void delete(int position) {
mNumbers.remove(position);
notifyItemRemoved(position);
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView numberText;
EditText checklistText;
Button deleteButton;
CheckBox checkDisBox;
public ViewHolder(View itemView) {
super(itemView);
numberText = (TextView) itemView.findViewById(R.id.number);
checklistText = (EditText) itemView.findViewById(R.id.editText);
deleteButton = (Button) itemView.findViewById(R.id.deleteButton);
checkDisBox = (CheckBox) itemView.findViewById(R.id.checkBox);
deleteButton.setOnClickListener(this);
}
public void bindNumbers(numbers numbers) {
numberText.setText(numbers.getInt() + "");
}
#Override
public void onClick(View v) {
delete(getAdapterPosition());
}
}
}
EDITED PROBLEM BELOW
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.bindNumbers(mNumbers.get(position));
holder.deleteButton.setOnClickListener(this);
}
public void delete(int position) {
mNumbers.remove(position);
notifyItemRemoved(position);
}
#Override
public void onClick(View v) {
delete(getAdapterPosition());
}
Edit
I just went and implemented this myself, and this is what worked for me.
Create an interface for deleting items
public interface DeleteItem {
void delete(int position);
}
Make your adapter implement this interface
public class myAdapter
extends RecyclerView.Adapter<myAdapter.ViewHolder>
implements DeleteItem {
#Override
public void delete(int position) {
mNumbers.remove(position);
notifyItemRemoved(position);
}
}
Set your viewholder to take a reference to the interface
DeleteItem mDeleteItem;
public ViewHolder(View itemView, DeleteItem deleteItem) {
super(itemView);
mDeleteItem = deleteItem;
...
}
Modify your view holders on click handler
#Override
public void onClick(View v) {
mDeleteItem.delete(getAdapterPosition());
}
Finally when creating your view holder pass it the reference
ViewHolder vh = new ViewHolder(v, this);
Original Answer
A ViewHolder is to save your application from having to find the views inside of list items. It should only contain the myview = itemView.findViewById() type calls and nothing else.
The bindViewHolder() method should contain all the code that uses those views you found in the view holder.
#Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.bindNumbers(mNumbers.get(position));
// add listener here
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
delete(position);
}
}
}
Code hasnt really changed from my question but removing the static keyword really got rid of a lot of my struggles
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView numberText;
EditText checklistText;
Button deleteButton;
CheckBox checkDisBox;
public ViewHolder(View itemView) {
super(itemView);
numberText = (TextView) itemView.findViewById(R.id.number);
checklistText = (EditText) itemView.findViewById(R.id.editText);
deleteButton = (Button) itemView.findViewById(R.id.deleteButton);
checkDisBox = (CheckBox) itemView.findViewById(R.id.checkBox);
deleteButton.setOnClickListener(this);
}
public void bindNumbers(numbers numbers) {
numberText.setText(numbers.getInt() + "");
}
#Override
public void onClick(View v) {
delete(getAdapterPosition());
}
}
my delete method
public void delete(int position) {
mNumbers.remove(position);
notifyItemRemoved(position);
}
I am having trouble removing items from RecyclerView. When I click on delete, the item is removed from RecyclerView, but comes back when I open the app again. I'm hoping it is just a minor issue that someone here can point out or direct me to what area to troubleshoot. The removeItem(String item) in bold is what I think is the issue. You can't see it in this post, but it is "not used".
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
private List<Grocery> mListData;
private SQLGroceryHelper helper;
RecyclerViewAdapter adapter;
//Adapter's Constructor//
public RecyclerViewAdapter(List<Grocery> mDataList) {
this.mListData = mDataList;
}
//Provide a reference to the views for each contact item//
public class MyViewHolder extends RecyclerView.ViewHolder {
TextView rowItem;
ImageButton purchasedButton;
ImageButton deleteButton;
LinearLayout linearLayout;
public MyViewHolder(View itemView) {
super(itemView);
linearLayout = (LinearLayout) itemView.findViewById(R.id.recycler_row);
rowItem = (TextView) itemView.findViewById(R.id.item_field1);
purchasedButton = (ImageButton) itemView.findViewById(R.id.item_purchased);
deleteButton = (ImageButton) itemView.findViewById(R.id.delete_item);
}
}
//Inflate the view based on the viewtype provided//
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//Create a new view by inflating the row item xml//
View row = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_row, parent, false);
//Set the view to the ViewHolder//
MyViewHolder holder = new MyViewHolder(row);
return holder;
}
//Display data at the specified position//
#Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
holder.rowItem.setText(mListData.get(position).getTextItem());
holder.purchasedButton.setOnClickListener(new View.OnClickListener() {
//Ignore this click for now//
#Override
public void onClick(View v) {
removeItem(position);
}
});
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeItem(position);
}
});
}
public void **removeItem**(String item) {
int position = mListData.indexOf(item);
if (position != -1) {
mListData.remove(item);
notifyItemRemoved(position);
}
}
public void removeItem(int position) {
mListData.remove(position);
notifyItemRemoved(position);
}
#Override
public int getItemCount() {
if (mListData == null) {
return 0;
}
return mListData.size();
}
}
You are removing the data from local object, mListData I guess the original data object remains intact. Remove the data item from the original data object as well
Declare a interface
public interface AdapterCommunication{
void removeStringItem(int position);
}
then in your adapter
private AdapterCommunication mListener;
public void setOnClickListener(AdapterCommunication listener){
mListener = listener;
}
Then from your activity where you initialize the adapter
RecyclerViewAdapter adapter = new RecyclerViewAdapter(list);
adapter.setOnClickListener(new AdapterCommunication{
public void removeStringItem(int position){
list.remove(position);
adapter.notifyDataSetChanged();
}
});
In your adaper,
holder.deleteButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mListener.remove(position);
}
});
I am experimenting with the support library's recyclerview and cards. I have a recyclerview of cards. Each card has an 'x' icon at the top right corner to remove it:
The card xml, list_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/taskDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textSize="40sp"
android:text="hi"/>
<ImageView
android:id="#+id/xImg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:src="#drawable/ic_remove"/>
</RelativeLayout>
</android.support.v7.widget.CardView>
I attempted to tag the row with the position I would use in notifyItemRemoved(position) in TaskAdapter.java:
public class TaskAdapter extends RecyclerView.Adapter<TaskAdapter.TaskViewHolder> {
private List<Task> taskList;
private TaskAdapter thisAdapter = this;
// cache of views to reduce number of findViewById calls
public static class TaskViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected TextView taskTV;
protected ImageView closeBtn;
public TaskViewHolder(View v) {
super(v);
taskTV = (TextView)v.findViewById(R.id.taskDesc);
}
#Override
public void onClick(View v) {
int position = v.getTag();
adapter.notifyItemRemoved(position);
}
}
public TaskAdapter(List<Task> tasks) {
if(tasks == null)
throw new IllegalArgumentException("tasks cannot be null");
taskList = tasks;
}
// onBindViewHolder binds a model to a viewholder
#Override
public void onBindViewHolder(TaskViewHolder taskViewHolder, int pos) {
final int position = pos;
Task currTask = taskList.get(pos);
taskViewHolder.taskTV.setText(currTask.getDescription());
taskViewHolder.closeBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
thisAdapter.notifyItemRemoved(position);
}
});
}
#Override
public int getItemCount() {
return taskList.size();
}
// inflates row to create a viewHolder
#Override
public TaskViewHolder onCreateViewHolder(ViewGroup parent, int pos) {
View itemView = LayoutInflater.from(parent.getContext()).
inflate(R.layout.list_item, parent, false);
return new TaskViewHolder(itemView);
}
}
This won't work because you can't set a tag nor can I access the adapter from onClick.
Set your onClickListeners on onBindViewHolder() and you can access the position from there. If you set them in your ViewHolder you won't know what position was clicked unless you also pass the position into the ViewHolder
EDIT
As pskink pointed out ViewHolder has a getPosition() so the way you were originally doing it was correct.
When the view is clicked you can use getPosition() in your ViewHolder and it returns the position
Update
getPosition() is now deprecated and replaced with getAdapterPosition()
Update 2020
getAdapterPosition() is now deprecated and replaced with getAbsoluteAdapterPosition() or getBindingAdapterPosition()
Kotlin code:
override fun onBindViewHolder(holder: MyHolder, position: Int) {
// - get element from your dataset at this position
val item = myDataset.get(holder.absoluteAdapterPosition)
}
A different method - using setTag() and getTag() methods of the View class.
use setTag() in the onBindViewHolder method of your adapter
#Override
public void onBindViewHolder(myViewHolder viewHolder, int position) {
viewHolder.mCardView.setTag(position);
}
where mCardView is defined in the myViewHolder class
private class myViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public View mCardView;
public myViewHolder(View view) {
super(view);
mCardView = (CardView) view.findViewById(R.id.card_view);
mCardView.setOnClickListener(this);
}
}
use getTag() in your OnClickListener implementation
#Override
public void onClick(View view) {
int position = (int) view.getTag();
//display toast with position of cardview in recyclerview list upon click
Toast.makeText(view.getContext(),Integer.toString(position),Toast.LENGTH_SHORT).show();
}
see https://stackoverflow.com/a/33027953/4658957 for more details
To complement #tyczj answer:
Generic Adapter Pseido code:
public abstract class GenericRecycleAdapter<T, K extends RecyclerView.ViewHolder> extends RecyclerView.Adapter{
private List<T> mList;
//default implementation code
public abstract int getLayout();
#Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(getLayout(), parent, false);
return getCustomHolder(v);
}
public Holders.TextImageHolder getCustomHolder(View v) {
return new Holders.TextImageHolder(v){
#Override
public void onClick(View v) {
onItem(mList.get(this.getAdapterPosition()));
}
};
}
abstract void onItem(T t);
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
onSet(mList.get(position), (K) holder);
}
public abstract void onSet(T item, K holder);
}
ViewHolder:
public class Holders {
public static class TextImageHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
public TextView text;
public TextImageHolder(View itemView) {
super(itemView);
text = (TextView) itemView.findViewById(R.id.text);
text.setOnClickListener(this);
}
#Override
public void onClick(View v) {
}
}
}
Adapter usage:
public class CategoriesAdapter extends GenericRecycleAdapter<Category, Holders.TextImageHolder> {
public CategoriesAdapter(List<Category> list, Context context) {
super(list, context);
}
#Override
void onItem(Category category) {
}
#Override
public int getLayout() {
return R.layout.categories_row;
}
#Override
public void onSet(Category item, Holders.TextImageHolder holder) {
}
}
public static class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
FrameLayout root;
public ViewHolder(View itemView) {
super(itemView);
root = (FrameLayout) itemView.findViewById(R.id.root);
root.setOnClickListener(this);
}
#Override
public void onClick(View v) {
LogUtils.errorLog("POS_CLICKED: ",""+getAdapterPosition());
}
}
Get focused child, and use it to get position in adapter.
mRecyclerView.getChildAdapterPosition(mRecyclerView.getFocusedChild())
Personally, the simplest way that I have found and works great for me is as follows:
Create an interface inside your "RecycleAdapter" Class (Subclass)
public interface ClickCallback {
void onItemClick(int position);
}
Add a variable of the interface as a parameter in the Constructor.
private String[] items;
private ClickCallback callback;
public RecyclerAdapter(String[] items, ClickCallback clickCallback) {
this.items = items;
this.callback = clickCallback;
}
Set a Click listener in the ViewHolder (another subclass) and pass the 'position' to through the interface
AwesomeViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
callback.onItemClick(getAdapterPosition());
}
});
mTextView = (TextView) itemView.findViewById(R.id.mTextView);
}
Now, when initializing the recycler adapter in an activity/fragment, just Create a new 'ClickCallback' (interface)
String[] values = {"Hello","World"};
RecyclerAdapter recyclerAdapter = new RecyclerAdapter(values, new RecyclerAdapter.ClickCallback() {
#Override
public void onItemClick(int position) {
// Do anything with the item position
}
});
That's it for me. :)
I solved this way
class MyOnClickListener implements View.OnClickListener {
#Override
public void onClick(View v) {
int itemPosition = mRecyclerView.getChildAdapterPosition(v);
myResult = results.get(itemPosition);
}
}
And in the adapter
#Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_wifi, parent, false);
v.setOnClickListener(new MyOnClickListener());
ViewHolder vh = new ViewHolder(v);
return vh;
}
1. Create class Name RecyclerTouchListener.java
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public class RecyclerTouchListener implements RecyclerView.OnItemTouchListener
{
private GestureDetector gestureDetector;
private ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
#Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
#Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildAdapterPosition(child));
}
}
});
}
#Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildAdapterPosition(child));
}
return false;
}
#Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
#Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
public interface ClickListener {
void onClick(View view, int position);
void onLongClick(View view, int position);
}
}
2. Call RecyclerTouchListener
recycleView.addOnItemTouchListener(new RecyclerTouchListener(this, recycleView,
new RecyclerTouchListener.ClickListener() {
#Override
public void onClick(View view, int position) {
Toast.makeText(MainActivity.this,Integer.toString(position),Toast.LENGTH_SHORT).show();
}
#Override
public void onLongClick(View view, int position) {
}
}));
onBindViewHolder() is called for each and every item and setting the click listener inside onBindVieHolder() is an unnecessary option to repeat when you can call it once in your ViewHolder constructor.
public class MyViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener{
public final TextView textView;
public MyViewHolder(View view){
textView = (TextView) view.findViewById(R.id.text_view);
view.setOnClickListener(this);
// getAdapterPosition() retrieves the position here.
}
#Override
public void onClick(View v){
// Clicked on item
Toast.makeText(mContext, "Clicked on position: " + getAdapterPosition(), Toast.LENGTH_SHORT).show();
}
}
I think the most correct way to get item position is
View.OnClickListener onClickListener = new View.OnClickListener() {
#Override public void onClick(View v) {
View view = v;
View parent = (View) v.getParent();
while (!(parent instanceof RecyclerView)){
view=parent;
parent = (View) parent.getParent();
}
int position = recyclerView.getChildAdapterPosition(view);
}
Because view, you click not always the root view of your row layout. If view is not a root one (e.g buttons), you will get Class cast exception. Thus at first we need to find the view, which is the a dirrect child of you reciclerview. Then, find position using recyclerView.getChildAdapterPosition(view);
No need to have your ViewHolder implementing View.OnClickListener. You can get directly the clicked position by setting a click listener in the method onCreateViewHolder of RecyclerView.Adapter here is a sample of code :
public class ItemListAdapterRecycler extends RecyclerView.Adapter<ItemViewHolder>
{
private final List<Item> items;
public ItemListAdapterRecycler(List<Item> items)
{
this.items = items;
}
#Override
public ItemViewHolder onCreateViewHolder(final ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_row, parent, false);
view.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
int currentPosition = getClickedPosition(view);
Log.d("DEBUG", "" + currentPosition);
}
});
return new ItemViewHolder(view);
}
#Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, int position)
{
...
}
#Override
public int getItemCount()
{
return items.size();
}
private int getClickedPosition(View clickedView)
{
RecyclerView recyclerView = (RecyclerView) clickedView.getParent();
ItemViewHolder currentViewHolder = (ItemViewHolder) recyclerView.getChildViewHolder(clickedView);
return currentViewHolder.getAdapterPosition();
}
}
#Override
public void onClick(View v) {
int pos = getAdapterPosition();
}
Simple as that, on ViewHolder
When using data binding and you need to know a RecyclerView click position from inside of an item's click listener:
Kotlin
val recyclerView = view.parent as RecyclerView
val position = recyclerView.getChildAdapterPosition(view)