Open cbaser opened 4 years ago
add this to your CardStackAdapter class
private lateinit var clickListener: OnItemClickListener
...other overriden methods
// allows clicks events to be caught
fun setOnItemClickListener(itemClickListener: OnItemClickListener) {
clickListener = itemClickListener
}
interace OnItemClickListener {
fun onItemClick(view: View, position: Int)
}
add this to your ViewHolder class
inner class ViewHolder (itemView: View): RecyclerView.ViewHolder(itemView) {
init {
itemView.setOnClickListener {
clickListener.onItemClick(itemView.rootView, adapterPosition)
}
}
...other methods and properties
}
Now use in fragment/activity class like this:
mCardsStackAdapter.setOnItemClickListener(object: CardsStackAdapter.OnItemClickListener{
override fun onItemClick(view: View, position: Int) {
Log.d("yourlogs", "Clicked $position")
//here you can show your alert dialog
}
})
@muramrr Thank you for your nice explanation! Can you please provide the Java Implementation as well? I will close it up then
add this to your CardStackAdapter class
private lateinit var clickListener: OnItemClickListener ...other overriden methods // allows clicks events to be caught fun setOnItemClickListener(itemClickListener: OnItemClickListener) { clickListener = itemClickListener } interace OnItemClickListener { fun onItemClick(view: View, position: Int) }
add this to your ViewHolder class
inner class ViewHolder (itemView: View): RecyclerView.ViewHolder(itemView) { init { itemView.setOnClickListener { clickListener.onItemClick(itemView.rootView, adapterPosition) } } ...other methods and properties }
Now use in fragment/activity class like this:
mCardsStackAdapter.setOnItemClickListener(object: CardsStackAdapter.OnItemClickListener{ override fun onItemClick(view: View, position: Int) { Log.d("yourlogs", "Clicked $position") //here you can show your alert dialog } })
@muramrr Thank you for your nice explanation! Can you please provide the Java Implementation as well? I will close it up then
@cbaser Same code in Java step by step
private static OnItemClickListener mClickListener;
//...other overriden methods
// allows clicks events to be caught
void setOnItemClickListener(OnItemClickListener itemClickListener) {
mClickListener = itemClickListener;
}
// parent activity will implement this method to respond to click events
public interface OnItemClickListener {
void onItemClick(View view, int position);
}
static class ViewHolder extends RecyclerView.ViewHolder {
//some private fields etc
//...
//our constructor
private ViewHolder (@NonNull final View itemView) {
super(itemView);
if (mClickListener != null)
mClickListener.onItemClick(itemView.getRootView(),getAdapterPosition());
}
}
In activity/fragment:
mCardsStackAdapter.setOnItemClickListener(new CardsStackAdapter.OnItemClickListener() {
@Override
public void onItemClick(View view, int position) {
}
});
I would like to add a clicklistener ( Just I want to show an Alertdialog after the Card is click) but I could not find any explanations to how to do that? Can you please inform me how I can manage it? Thank you