Has anyone using
Answers:RecyclerView
found a way to set an onClickListener
to items in the RecyclerView
? 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 the RecyclerView
to listen for the onClick
event but I can't quite figure it out.
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<MyViewHolder>
, you should have:private final OnClickListener mOnClickListener = new MyOnClickListener();
@Override
public MyViewHolder onCreateViewHolder(final ViewGroup parent, final int position) {
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();
}
Note that this is still in preview, so API's may change.
0 nhận xét:
Post a Comment