我正在尝试在同一个recyclerView适配器中扩展不同的布局。我正在从服务器获取数据,这里的响应如下:
[
{
"id": 1,
"title": "test",
"content_text": "this is just for testing\r\n",
"content_audio": "",
"content_video": "",
"language": "english",
"images": []
},
{
"id": 2,
"title": "test",
"content_text": "this is just for testing\r\n",
"content_audio": "",
"content_video": "",
"language": "english",
"images": []
},
{
"id": 3,
"title": "test",
"content_text": "this is just for testing\r\n",
"content_audio": "",
"content_video": "",
"language": "english",
"images": []
},
{
"id": 4,
"title": "test",
"content_text": "this is just for testing\r\n",
"content_audio": "",
"content_video": "",
"language": "english",
"images": []
},
{
"id": 5,
"title": "test",
"content_text": "this is just for testing\r\n",
"content_audio": "",
"content_video": "",
"language": "english",
"images": [
{
"id": 1,
"timeline_id": 5,
"image": "uploads/islamicContent/e23ae768bea547ccf5df10533e5a4f56.jpg"
},
{
"id": 2,
"timeline_id": 5,
"image": "uploads/islamicContent/64d45166442f6ebfee11777a0ce364d0.jpg"
},
{
"id": 3,
"timeline_id": 5,
"image": "uploads/islamicContent/01c924e77b37264fe00197c316cddbd3.jpg"
}
]
}
]
我的屏幕应该包含帖子,图片和视频(如FB时间轴)。 问题是,当我在我的设备上运行我的代码时,似乎适配器不会给文件“帖子”增加任何内容(仅显示没有图像),但是如果我们看看响应它应该显示5个帖子然后3个图像。
我的伊斯兰时间线适配器看起来像:
public class IslamicTimelineAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private List<IslamicTimelineResponse> islamicList = Collections.emptyList();
private Activity context;
private LayoutInflater inflater;
public IslamicTimelineAdapter(Activity mContext) {
this.context = mContext;
this.islamicList = new ArrayList<>();
inflater = LayoutInflater.from(context);
}
@Override
public int getItemViewType(int position) {
if (islamicList.get(position).postContent.length() > 0 && islamicList.get(position).postContent != null)
return ISLAMIC_POST;
else if (islamicList.get(position).postImages.size() > 0 && islamicList.get(position).postImages != null)
return ISLAMIC_IMAGE;
else if (islamicList.get(position).postVideoUrl.length() > 0 && islamicList.get(position).postVideoUrl != null)
return ISLAMIC_VIDEO;
return -1;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerView.ViewHolder viewHolder = null;
switch (viewType) {
case ISLAMIC_POST:
View postRow = inflater.inflate(R.layout.index_islamic_timeline_posts, parent, false);
viewHolder = new IslamicTimelinePostHolder(postRow);
break;
case ISLAMIC_IMAGE:
View imageRow = inflater.inflate(R.layout.index_islamic_timeline_images, parent, false);
viewHolder = new IslamicTimelineImageHolder(imageRow);
break;
case ISLAMIC_VIDEO:
View videoRow = inflater.inflate(R.layout.index_islamic_timeline_videos, parent, false);
viewHolder = new IslamicTimelineVideoHolder(videoRow);
break;
}
return viewHolder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
IslamicTimelineResponse content = islamicList.get(position);
if (content != null) {
switch (holder.getItemViewType())
{
case ISLAMIC_POST:
if (content.postContent.length() > 0 && content.postContent != null)
((IslamicTimelinePostHolder) holder).islamicTimelinePost.setText(content.postID + " : " +content.postContent);
break;
case ISLAMIC_IMAGE:
if (content.postImages.size() > 0 && content.postImages != null) {
for (int i = 0; i < content.postImages.size(); i++)
Glide.with(context).load(content.postImages.get(i)).into(((IslamicTimelineImageHolder) holder).islamicTimelineImage);
}
break;
case ISLAMIC_VIDEO:
if (content.postVideoUrl.length() > 0 && content.postVideoUrl != null)
((IslamicTimelineVideoHolder) holder).islamicTimelineVideo.setVideoURI(Uri.parse(content.postVideoUrl));
break;
}
}
}
@Override
public int getItemCount() {
return (islamicList != null ? islamicList.size() : 0 );
}
public void setContent(List<IslamicTimelineResponse> contents) {
this.islamicList.addAll(contents);
notifyDataSetChanged();
}
private class IslamicTimelinePostHolder extends RecyclerView.ViewHolder {
private TextView islamicTimelinePost;
private IslamicTimelinePostHolder(View itemView) {
super(itemView);
islamicTimelinePost = (TextView) itemView.findViewById(R.id.islamic_timeline_post);
}
}
private class IslamicTimelineImageHolder extends RecyclerView.ViewHolder {
private ImageView islamicTimelineImage;
private IslamicTimelineImageHolder(View itemView) {
super(itemView);
islamicTimelineImage = (ImageView) itemView.findViewById(R.id.islamic_timeline_image);
}
}
private class IslamicTimelineVideoHolder extends RecyclerView.ViewHolder {
private VideoView islamicTimelineVideo;
private IslamicTimelineVideoHolder(View itemView) {
super(itemView);
islamicTimelineVideo = (VideoView) itemView.findViewById(R.id.islamic_timeline_video);
}
}
}
我在stackoverflow中看到很多帖子但是,我无法解决我的问题,我期待我的问题在适配器中。
答案 0 :(得分:0)
您将根据内容返回视图类型,并且每次要获取JSON reaponse中的内容以获取密钥content_text
if (islamicList.get(position).postContent.length() > 0 && islamicList.get(position).postContent != null)
return ISLAMIC_POST;
<强> {EDIT} 强>
试着改变这个条件:
@Override
public int getItemViewType(int position) {
if (islamicList.get(position).postVideoUrl.length() > 0 && islamicList.get(position).postVideoUrl != null)
return ISLAMIC_VIDEO;
else if (islamicList.get(position).postImages.size() > 0 && islamicList.get(position).postImages != null)
return ISLAMIC_IMAGE;
else if (islamicList.get(position).postContent.length() > 0 && islamicList.get(position).postContent != null)
return ISLAMIC_POST;
return -1;
}