Answer the question
In order to leave comments, you need to log in
Is there an "analog" for adapter.getRef(position).getKey() in RecyclerView.Adapter?
Hello!
Is there any "analogue" adapter.getRef(position).getKey()
that can be used in RecyclerView.Adapter?
Digging around the net, I found only one solution:
https://stackoverflow.com/questions/50283774/getre...
As for my code, it is presented below (in the onbindviewholder method):
holder.imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!db.isFavorite(adapter.getRef(position).getKey())) {
db.addToFavorites(adapter.getRef(position).getKey());
holder.imageButton.setImageResource(R.drawable.ic_add_to_fav);
Toast.makeText(mContext, medication.get_tradeName() + " добавлен в избранное", Toast.LENGTH_SHORT).show();
} else {
db.removeFromFavorites(adapter.getRef(position).getKey());
holder.imageButton.setImageResource(R.drawable.ic_remove_from_fav);
Toast.makeText(mContext, medication.get_tradeName() + " удален из избранного.", Toast.LENGTH_SHORT).show();
}
}
});
Answer the question
In order to leave comments, you need to log in
Ideally, onBind does not need to add an OnClickListener to the button. That's right - it's to add your own implementation of SimpleOnItemTouchListener and using GestureDetector and onGestureListener and catch a touch on the element, and then look, if you need where the touch fell and change accordingly in the adapter for this element and initialize new data using notifyItemDataChanged. and in onBind only bind data to the holder.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question