我正在尝试将此RecyclerAdapter
之间的数据传递给DetailsActivity,即当用户点击每个项目时,获取项目position
,然后例如从{{1}获取标题并将其显示在recyclerView
MainRecyclerDetails
中,这是我的尝试:
TextView
获取public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.FeedListRowHolder> {
private List<FeedItem> feedItemList;
private Context mContext;
public MyRecyclerAdapter(Context context, List<FeedItem> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
@Override
public FeedListRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
return new FeedListRowHolder(v);
}
@Override
public void onBindViewHolder(FeedListRowHolder feedListRowHolder, int i) {
FeedItem feedItem = feedItemList.get(i);
Picasso.with(mContext).load(feedItem.getThumbnail())
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(feedListRowHolder.thumbnail);
feedListRowHolder.title.setText(Html.fromHtml(feedItem.getTitle()));
feedListRowHolder.date.setText(Html.fromHtml(feedItem.getDate()));
feedListRowHolder.tags.setText(Html.fromHtml(feedItem.getTags()));
feedListRowHolder.thumbnail.setOnClickListener(clickListener);
feedListRowHolder.title.setOnClickListener(clickListener);
feedListRowHolder.date.setOnClickListener(clickListener);
feedListRowHolder.tags.setOnClickListener(clickListener);
feedListRowHolder.thumbnail.setTag(feedListRowHolder);
feedListRowHolder.title.setTag(feedListRowHolder);
feedListRowHolder.date.setTag(feedListRowHolder);
feedListRowHolder.tags.setTag(feedListRowHolder);
}
@Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
View.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View view) {
FeedListRowHolder holder = (FeedListRowHolder) view.getTag();
int position = holder.getAdapterPosition();
FeedItem feedItem = feedItemList.get(position);
Toast.makeText(mContext, feedItem.getTitle(), Toast.LENGTH_SHORT).show();
Intent i = new Intent(mContext, MainRecyclerDetails.class);
String getrec = feedItem.getTitle();
Bundle bundle = new Bundle();
bundle.putString("title", getrec);
i.putExtras(bundle);
startActivity(i);
}
};
class FeedListRowHolder extends RecyclerView.ViewHolder {
protected ImageView thumbnail;
protected TextView title;
protected TextView date;
protected TextView tags;
public FeedListRowHolder(View view) {
super(view);
this.thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
this.title = (TextView) view.findViewById(R.id.title);
this.date = (TextView) view.findViewById(R.id.date);
this.tags = (TextView) view.findViewById(R.id.tagsItems);
}
}
}
:
DetailsActivity
但是,它没有用。
我做错了什么?
干杯!
答案 0 :(得分:2)
你可以这样实现
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.FeedListRowHolder> {
private List<FeedItem> feedItemList;
private Context mContext;
public MyRecyclerAdapter(Context context, List<FeedItem> feedItemList) {
this.feedItemList = feedItemList;
this.mContext = context;
}
@Override
public FeedListRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_row, null);
return new FeedListRowHolder(v);
}
@Override
public void onBindViewHolder(FeedListRowHolder feedListRowHolder, int i) {
FeedItem feedItem = feedItemList.get(i);
Picasso.with(mContext).load(feedItem.getThumbnail())
.error(R.drawable.placeholder)
.placeholder(R.drawable.placeholder)
.into(feedListRowHolder.thumbnail);
feedListRowHolder.title.setText(Html.fromHtml(feedItem.getTitle()));
feedListRowHolder.date.setText(Html.fromHtml(feedItem.getDate()));
feedListRowHolder.tags.setText(Html.fromHtml(feedItem.getTags()));
}
@Override
public int getItemCount() {
return (null != feedItemList ? feedItemList.size() : 0);
}
class FeedListRowHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected ImageView thumbnail;
protected TextView title;
protected TextView date;
protected TextView tags;
public FeedListRowHolder(View view) {
super(view);
this.thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
this.title = (TextView) view.findViewById(R.id.title);
this.date = (TextView) view.findViewById(R.id.date);
this.tags = (TextView) view.findViewById(R.id.tagsItems);
thumbnail.setOnClickListener(this);
title.setOnClickListener(this);
date.setOnClickListener(this);
tags.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int position = getAdapterPosition();
FeedItem feedItem = feedItemList.get(position);
Toast.makeText(mContext, feedItem.getTitle(), Toast.LENGTH_SHORT).show();
Intent i = new Intent(mContext, MainRecyclerDetails.class);
String getrec = feedItem.getTitle();
i.putExtra("title", getrec);
mContext.startActivity(i);
}
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extra = getIntent().getExtras();
String title = extra.getString("title");
}