Android 多片式RealmRecyclServiceAdapter
我正在开发我的应用程序,其中我使用了Android 多片式RealmRecyclServiceAdapter,android,android-recyclerview,realm,Android,Android Recyclerview,Realm,我正在开发我的应用程序,其中我使用了RealmRecyclerViewAdapter。 在适配器中,我想使用多个RecyclerView.ViewHolder,其中包含不同的数据 main活动 adapter = new RVAdapter(realm.where(QuoteObject.class).findAll(), this); 但从另一个片段中,我需要给出另一个数据: adapter = new RVAdapter(realm.where(AuthorObject.class).fi
RealmRecyclerViewAdapter
。
在适配器中,我想使用多个RecyclerView.ViewHolder
,其中包含不同的数据
main活动
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);
}
}
类RVAdapter扩展了RealmRecyclServiceAdapter{
私有静态最终字符串标记=“###”+“RVAdapter”;
私人语境;
私有CustomGridLayoutManager CustomGridLayoutManager;
RVAdapter(OrderedRealmCollection引号、上下文){
超级(引用,真实);
this.context=上下文;
customGridLayoutManager=新建customGridLayoutManager(上下文);
}
@凌驾
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup ViewGroup,int viewType){
开关(视图类型){
案例0:
View quoteView=LayoutInflater.from(viewGroup.getContext())
.充气(R.layout.card\u of _quote,viewGroup,false);
返回新的AllQuotesViewHolder(quoteView);
案例1:
View topicView=LayoutInflater.from(viewGroup.getContext())
.充气(R.layout.card\u of_topic,viewGroup,false);
返回新的TopicViewHolder(topicView);
案例2:
View authorView=LayoutInflater.from(viewGroup.getContext())
.充气(R.layout.card\u作者,视图组,假);
返回新的AuthorViewHolder(authorView);
}
返回null;
}
@凌驾
public void onBindViewHolder(final RecyclerView.ViewHolder ViewHolder,int位置){
开关(viewHolder.getItemViewType()){
案例0:
最终AllQuotesViewHolder AllQuotesViewHolder=(AllQuotesViewHolder)视图持有人;
打破
案例1:
最终AuthorViewHolder AuthorViewHolder=(AuthorViewHolder)viewHolder;
打破
案例2:
最终TopicViewHolder TopicViewHolder=(TopicViewHolder)视图持有人;
打破
}
}
@凌驾
public int getItemCount(){
返回getData().size();
}
@凌驾
public int getItemViewType(int位置){
返回位置%2*2;
}
私有类AllQuotesViewHolder扩展了RecyclerView.ViewHolder{
卡德维尤;
text查看引用的内容,authorQuote;
图像按钮btnViewComment、btnShareQuote、btnSetLike;
文本视图数量;
ProgressBar ProgressBar;
AllQuotesViewHolder(查看项目视图){
超级(项目视图);
this.cardwiew=(cardwiew)itemView.findviewbyd(R.id.cardwiew);
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)itemviewbyd(R.id.my\u progress\u bar);
}
}
私有类TopicViewHolder扩展了RecyclerView.ViewHolder{
卡德维尤;
TopicViewHolder(查看项目视图){
超级(项目视图);
this.cardwiew=(cardwiew)itemView.findviewbyd(R.id.cardwiew);
}
}
私有类AuthorViewHolder扩展了RecyclerView.ViewHolder{
卡德维尤;
AuthorViewHolder(查看项目视图){
超级(项目视图);
this.cardwiew=(cardwiew)itemView.findviewbyd(R.id.cardwiew);
}
}
}
如何将不同的数据传输到一个RealmRecyclServiceAdapter
谢谢。您正在寻找背后的概念,但需要替换
RealmRecyclerViewAdapter您正在寻找背后的概念,但需要替换RealmRecyclerViewAdapter