我正在开发使用RealmRecyclerViewAdapter
的应用。
在适配器中,我想使用多个RecyclerView.ViewHolder
,其中包含不同的数据。
MainActivity
adapter = new RVAdapter(realm.where(QuoteObject.class).findAll(), this);
但是从另一个片段我需要提供另一个数据:
adapter = new RVAdapter(realm.where(AuthorObject.class).findAll(), this);
我的适配器
class RVAdapter extends RealmRecyclerViewAdapter<QuoteObject, RecyclerView.ViewHolder> {
private static final String TAG = "###" + "RVAdapter";
private Context context;
private CustomGridLayoutManager customGridLayoutManager;
RVAdapter(OrderedRealmCollection<QuoteObject> quotes, Context context) {
super(quotes, true);
this.context = context;
customGridLayoutManager = new CustomGridLayoutManager(context);
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
switch (viewType) {
case 0:
View quoteView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.card_of_quote, viewGroup, false);
return new AllQuotesViewHolder(quoteView);
case 1:
View topicView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.card_of_topic, viewGroup, false);
return new TopicViewHolder(topicView);
case 2:
View authorView = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.card_of_author, viewGroup, false);
return new AuthorViewHolder(authorView);
}
return null;
}
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, int position) {
switch (viewHolder.getItemViewType()){
case 0:
final AllQuotesViewHolder allQuotesViewHolder = (AllQuotesViewHolder) viewHolder;
break;
case 1:
final AuthorViewHolder authorViewHolder = (AuthorViewHolder) viewHolder;
break;
case 2:
final TopicViewHolder topicViewHolder = (TopicViewHolder) viewHolder;
break;
}
}
@Override
public int getItemCount() {
return getData().size();
}
@Override
public int getItemViewType(int position) {
return position % 2 * 2;
}
private class AllQuotesViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TextView contentOfQuote, authorQuote;
ImageButton btnViewComment, btnShareQuote, btnSetLike;
TextView amountLikes;
ProgressBar progressBar;
AllQuotesViewHolder(View itemView) {
super(itemView);
this.cardView = (CardView) itemView.findViewById(R.id.cardView);
this.contentOfQuote = (TextView) itemView.findViewById(R.id.contentOfQuote);
this.authorQuote = (TextView) itemView.findViewById(R.id.authorQuote);
this.btnViewComment = (ImageButton) itemView.findViewById(R.id.btnViewComment);
this.btnShareQuote = (ImageButton) itemView.findViewById(R.id.btnShareQuote);
this.btnSetLike = (ImageButton) itemView.findViewById(R.id.btnSetLike);
this.amountLikes = (TextView) itemView.findViewById(R.id.amountLike);
this.progressBar = (ProgressBar) itemView.findViewById(R.id.my_progress_bar);
}
}
private class TopicViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
TopicViewHolder(View itemView) {
super(itemView);
this.cardView = (CardView) itemView.findViewById(R.id.cardView);
}
}
private class AuthorViewHolder extends RecyclerView.ViewHolder {
CardView cardView;
AuthorViewHolder(View itemView) {
super(itemView);
this.cardView = (CardView) itemView.findViewById(R.id.cardView);
}
}
}
如何将不同的数据传输到一个RealmRecyclerViewAdapter
?
感谢。