在适配器中添加Intent。
示例代码:
public class MyAdapter extends RecyclerView.Adapter
private Context mContext;
private List- mItemList;
public MyAdapter(Context context, List
- itemList) {
mContext = context;
mItemList = itemList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.item_layout, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
final Item item = mItemList.get(position);
holder.titleTextView.setText(item.getTitle());
holder.descriptionTextView.setText(item.getDescription());
holder.imageView.setImageResource(item.getImageResId());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(mContext, DetailActivity.class);
intent.putExtra("item", item);
mContext.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return mItemList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView titleTextView;
public TextView descriptionTextView;
public ImageView imageView;
public ViewHolder(View itemView) {
super(itemView);
titleTextView = (TextView) itemView.findViewById(R.id.title_text_view);
descriptionTextView = (TextView) itemView.findViewById(R.id.description_text_view);
imageView = (ImageView) itemView.findViewById(R.id.image_view);
}
}
}