问题描述
我使用库中显示多类型的RecyclerView内部的看法。
在这里,我使用EnumMapBindAdapter显示userposts列表。
我有4 DataBinder的类,每个绑定有大小3(大小的名单可能会根据变化
用户帖子)。我有问题getEnumFromPosition(),根据我的假设getEnumFromPosition的执行数()方法应该等于(4)粘合剂的数量。但它没有发生。我共有4粘合剂和我假设getEnumFromPosition()方法应该只执行4次(0,1,2,3),但执行正在excedeed。我的假设可能是错误的,如果我错了,那么何来
我管理的内部getEnumFromPosition 4粘合剂()为不同的位置。如果我把其他部分内getEnumFromPosition()方法,那么当位置超过它会返回一个单一视图。
这里是SampleEnumMapAdapter.java的code
公共类SampleEnumMapAdapter扩展EnumMapBindAdapter< SampleEnumMapAdapter.SampleViewType> { 枚举SampleViewType {
TEXT,IMAGE_AND_TEXT,AUDIO_AND_TEXT,VIDEO_AND_TEXT
} 公共SampleEnumMapAdapter(){
putBinder(SampleViewType.TEXT,新TextBinder(本));
putBinder(SampleViewType.IMAGE_AND_TEXT,新ImageAndTextBinder(本));
putBinder(SampleViewType.AUDIO_AND_TEXT,新AudioAndTextBinder(本));
putBinder(SampleViewType.VIDEO_AND_TEXT,新VideoAndTextBinder(本));
} 公共无效使用setData(列表<&的TextData GT; dataSetText,列表与LT; ImageAndTextData> dataSetTextAndImage,列表与LT; AudioAndTextData> dataSetTextAndAudio,列表与LT; VideoAndTextData> dataSetTextAndVideo){
((TextBinder)getDataBinder(SampleViewType.TEXT))的addAll(dataSetText)。
((ImageAndTextBinder)getDataBinder(SampleViewType.IMAGE_AND_TEXT))的addAll(dataSetTextAndImage)。
((AudioAndTextBinder)getDataBinder(SampleViewType.AUDIO_AND_TEXT))的addAll(dataSetTextAndAudio)。
((VideoAndTextBinder)getDataBinder(SampleViewType.VIDEO_AND_TEXT))的addAll(dataSetTextAndVideo)。
} @覆盖
公共SampleViewType getEnumFromPosition(INT位置){
如果(位置== 0){
返回SampleViewType.TEXT;
}否则如果(位置== 1){
返回SampleViewType.IMAGE_AND_TEXT;
}否则如果(位置== 2){
返回SampleViewType.AUDIO_AND_TEXT;
}否则如果(位置== 3){
返回SampleViewType.VIDEO_AND_TEXT;
}
返回null;
} @覆盖
公共SampleViewType getEnumFromOrdinal(INT序){
返回SampleViewType.values()[序];
}
}
我在做类似的问题前面,但我配置RecyclerView适配器手动,我没有使用任何库这一点。我在这里给你一个样本code。这是一个完整的视图多重类型适配器
公共类UserPostAdapter扩展RecyclerView.Adapter< UserPostAdapter.PostViewHolder> { 私人列表< UserPostsListItem> postlist;
上下文conetxt;
LayoutInflater吹气;
私人ViewClickListener mClickListener;
私人ItemViewClickListener itemViewClickListener;
诠释计数; 公共UserPostAdapter(){ } 公共UserPostAdapter(上下文conetxt,列表与LT; UserPostsListItem> postlist){
this.postlist = postlist;
this.conetxt = conetxt;
吹气= LayoutInflater.from(this.conetxt);
} @覆盖
公众诠释getItemCount(){
INT大小= 0;
如果(postlist!= NULL)
大小= postlist.size();
返回的大小;
} @覆盖
公众诠释getItemViewType(INT位置){
INT viewTypePosition = 0; 字符串attachment_type = postlist.get(位置).getAttachment_type(); 开关(attachment_type){ 案文:
viewTypePosition = 0;
打破;
案形象:
viewTypePosition = 1;
打破;
案声音:
viewTypePosition = 2;
打破;
案视频:
viewTypePosition = 3;
打破;
默认:
} 返回viewTypePosition;
} @覆盖
公共无效onBindViewHolder(最终PostViewHolder videoViewHolder,最终诠释位置){ //Log.d(\"onBindViewHolder\",Integer.toString(position));
最后UserPostsListItem项目= postlist.get(位置); 开关(item.getAttachment_type()){
案声音: 打破;
案视频: 打破;
案文: 打破;
案形象: 打破;
默认:
} }赶上(例外五){
e.printStackTrace();
} } 公共接口ItemViewClickListener {
无效onItemChildClicked(INT位置,字符串型);
} 公共无效setItemViewClickListener(ItemViewClickListener viewClickListener){
itemViewClickListener = viewClickListener;
}
公开名单< UserPostsListItem> getUserPostsList(){
返回postlist;
} @覆盖
公共PostViewHolder onCreateViewHolder(ViewGroup中ViewGroup中,诠释viewType){ Log.d(ViewType,Integer.toString(viewType));
开关(viewType){
情况下0:
查看itemViewText = inflater.inflate(R.layout.userposts_listitem_text,ViewGroup中,FALSE);
返回新PostViewHolder(itemViewText,viewType); 情况1:
查看itemViewTextImage = inflater.inflate(R.layout.userposts_listitem_textandimage,ViewGroup中,FALSE);
返回新PostViewHolder(itemViewTextImage,viewType); 案例2:
查看itemViewTextAudio = inflater.inflate(R.layout.userposts_listitem_audioandtext,ViewGroup中,FALSE);
返回新PostViewHolder(itemViewTextAudio,viewType); 案例3:
查看itemViewTextVideo = inflater.inflate(R.layout.userposts_listitem_videoandtext,ViewGroup中,FALSE);
返回新PostViewHolder(itemViewTextVideo,viewType); 默认:
} 返回null; } 公共接口ViewClickListener {
无效onVideoClicked(INT位置,查看V);
} 公开名单< UserPostsListItem> getVideoItems(){
返回postlist;
} 公共无效setViewClickListener(ViewClickListener viewClickListener){
mClickListener = viewClickListener;
} 公共静态类PostViewHolder扩展RecyclerView.ViewHolder {
ImageView的附件;
CircleImageView circleImageView;
ImageView的声音;
公共PostViewHolder(视图V,INT viewtype){
超级(五);
开关(viewtype){
//音频
案例2:
打破;
//对于视频
案例3:
打破;
//文字清晰
情况下0:
打破;
//对于图像
情况1:
打破;
默认: }
}
}}
I am using this library to display mutiple types of views inside a RecyclerView .
Here I am using EnumMapBindAdapter to display userposts list .
I have 4 DataBinder classes ,each binder has a list of size 3 (size may change depending on theuser posts ) . I have problem with getEnumFromPosition() ,According to my assumptions number of executions of getEnumFromPosition() method should be equal to number of binders (4) . But it is not happening .I have total 4 binders and I assume getEnumFromPosition() method should execute 4 times only (0,1,2,3) ,but executions are being excedeed . My assumption may be wrong ,If I am wrong then how canI manage 4 binders inside getEnumFromPosition() for different positions . If I put else section inside getEnumFromPosition() method then it will return a single view when position exceeds .
Here is the code of SampleEnumMapAdapter.java
public class SampleEnumMapAdapter extends EnumMapBindAdapter<SampleEnumMapAdapter.SampleViewType> {
enum SampleViewType{
TEXT,IMAGE_AND_TEXT, AUDIO_AND_TEXT, VIDEO_AND_TEXT
}
public SampleEnumMapAdapter(){
putBinder(SampleViewType.TEXT, new TextBinder(this));
putBinder(SampleViewType.IMAGE_AND_TEXT, new ImageAndTextBinder(this));
putBinder(SampleViewType.AUDIO_AND_TEXT, new AudioAndTextBinder(this));
putBinder(SampleViewType.VIDEO_AND_TEXT, new VideoAndTextBinder(this));
}
public void setData(List<TextData> dataSetText ,List<ImageAndTextData> dataSetTextAndImage ,List<AudioAndTextData> dataSetTextAndAudio ,List<VideoAndTextData> dataSetTextAndVideo ) {
((TextBinder) getDataBinder(SampleViewType.TEXT)).addAll(dataSetText);
((ImageAndTextBinder) getDataBinder(SampleViewType.IMAGE_AND_TEXT)).addAll(dataSetTextAndImage);
((AudioAndTextBinder) getDataBinder(SampleViewType.AUDIO_AND_TEXT)).addAll(dataSetTextAndAudio);
((VideoAndTextBinder) getDataBinder(SampleViewType.VIDEO_AND_TEXT)).addAll(dataSetTextAndVideo);
}
@Override
public SampleViewType getEnumFromPosition(int position){
if (position == 0) {
return SampleViewType.TEXT;
} else if (position == 1) {
return SampleViewType.IMAGE_AND_TEXT;
} else if(position == 2){
return SampleViewType.AUDIO_AND_TEXT;
}else if(position == 3){
return SampleViewType.VIDEO_AND_TEXT;
}
return null;
}
@Override
public SampleViewType getEnumFromOrdinal(int ordinal) {
return SampleViewType.values()[ordinal];
}
}
I was working on the similar problem earlier , But I configured RecyclerView Adapter manually ,I have not used any library for this . I am giving you a sample code here . This is a complete Multile View Type Adapter
public class UserPostAdapter extends RecyclerView.Adapter<UserPostAdapter.PostViewHolder> {
private List<UserPostsListItem> postlist;
Context conetxt;
LayoutInflater inflater;
private ViewClickListener mClickListener;
private ItemViewClickListener itemViewClickListener;
int count;
public UserPostAdapter() {
}
public UserPostAdapter(Context conetxt, List<UserPostsListItem> postlist) {
this.postlist = postlist;
this.conetxt = conetxt;
inflater = LayoutInflater.from(this.conetxt);
}
@Override
public int getItemCount() {
int size = 0;
if (postlist != null)
size = postlist.size();
return size;
}
@Override
public int getItemViewType(int position) {
int viewTypePosition =0;
String attachment_type = postlist.get(position).getAttachment_type();
switch (attachment_type){
case "text":
viewTypePosition = 0 ;
break;
case "image":
viewTypePosition = 1 ;
break;
case "audio":
viewTypePosition = 2 ;
break;
case "video":
viewTypePosition = 3 ;
break;
default:
}
return viewTypePosition;
}
@Override
public void onBindViewHolder(final PostViewHolder videoViewHolder, final int position) {
//Log.d("onBindViewHolder",Integer.toString(position));
final UserPostsListItem item = postlist.get(position);
switch (item.getAttachment_type()){
case "audio":
break;
case "video":
break;
case "text":
break;
case "image":
break;
default:
}
}catch (Exception e){
e.printStackTrace();
}
}
public interface ItemViewClickListener {
void onItemChildClicked(int position ,String type);
}
public void setItemViewClickListener (ItemViewClickListener viewClickListener) {
itemViewClickListener = viewClickListener;
}
public List<UserPostsListItem> getUserPostsList() {
return postlist;
}
@Override
public PostViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType){
Log.d("ViewType", Integer.toString(viewType));
switch (viewType) {
case 0:
View itemViewText = inflater.inflate(R.layout.userposts_listitem_text, viewGroup, false);
return new PostViewHolder(itemViewText ,viewType);
case 1:
View itemViewTextImage = inflater.inflate(R.layout.userposts_listitem_textandimage, viewGroup, false);
return new PostViewHolder(itemViewTextImage,viewType);
case 2:
View itemViewTextAudio = inflater.inflate(R.layout.userposts_listitem_audioandtext, viewGroup, false);
return new PostViewHolder(itemViewTextAudio,viewType);
case 3:
View itemViewTextVideo = inflater.inflate(R.layout.userposts_listitem_videoandtext, viewGroup, false);
return new PostViewHolder(itemViewTextVideo,viewType);
default:
}
return null;
}
public interface ViewClickListener {
void onVideoClicked(int position,View v);
}
public List<UserPostsListItem> getVideoItems() {
return postlist;
}
public void setViewClickListener (ViewClickListener viewClickListener) {
mClickListener = viewClickListener;
}
public static class PostViewHolder extends RecyclerView.ViewHolder {
ImageView attachment;
CircleImageView circleImageView;
ImageView audio;
public PostViewHolder(View v,int viewtype) {
super(v);
switch (viewtype){
//For Audio
case 2:
break;
//For Video
case 3:
break;
//For Text only
case 0:
break;
//For Image
case 1:
break;
default:
}
}
}
}
这篇关于管理多个视图类型 - RecyclerView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!