As the API's have radically changed, It wouldn't surprise me if you were to create an OnClickListener
for each item. It isn't that much of a hassle though. In your implementation of RecyclerView.Adapter
, you should have:
private final OnClickListener mOnClickListener = new MyOnClickListener();
@Override
public MyViewHolder onCreateViewHolder(final ViewGroup parent, final int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.myview, parent, false);
view.setOnClickListener(mOnClickListener);
return new MyViewHolder(view);
}
The onClick
method:
@Override
public void onClick(final View view) {
int itemPosition = mRecyclerView.getChildLayoutPosition(view);
String item = mList.get(itemPosition);
Toast.makeText(mContext, item, Toast.LENGTH_LONG).show();
}
manpreet
Best Answer
2 years ago
Has anyone using
RecyclerView
found a way to set anonClickListener
to items in theRecyclerView
? I thought of setting a listener to each of the layouts for each item but that seems a little too much hassle I'm sure there is a way for theRecyclerView
to listen for theonClick
event but I can't quite figure it out.