We handling the click events, I have tried below method
Create an interface RecyclerOnItemClickListener
import android.view.View;
/**
* A click listener for items.
*/
public interface RecyclerOnItemClickListener {
/**
* Called when an item is clicked.
*
* @param childView View of the item that was clicked.
* @param position Position of the item that was clicked.
*/
public void onItemClick(View childView, int position);
}
In the adapter class, you can take the RecyclerOnItemClickListener object which is implemented in the calling Activity/Fragment
RecyclerOnItemClickListener mItemClickListener;
You can then initialize that variable in the constructor of the adapter
mItemClickListener = recyclerOnItemClickListener;
The activity/Fragment implements the RecyclerOnItemClickListener interface, so it will have onItemClick method implemented. You can write the code for passing data in the next activity here
@Override
public void onItemClick(View childView, int position)
{
Intent intent = new Intent(getActivity(), NextActivity.class);
intent.putExtra("ITEM", mModels.get(position));
startActivity(intent);
}
In the adapter class, you will need to initialize the ViewHolder class as shown below:
class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
TextView content;
TextView proba;
ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
content = (TextView) itemView.findViewById(R.id.textView);
proba = (TextView) itemView.findViewById(R.id.textView2);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
}
@Override
public void onClick(View v)
{
if (mItemClickListener != null)
{
mItemClickListener.onItemClick(v, getPosition());
}
}
}
Hope this helps you..
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…