Inflate different Layouts In Same RecyclerView Adapter

808 views Asked by At

I'm trying to inflate different layouts in the same recyclerView Adapter . I'm Fetching data from server and here is the response looks like :

[
  {
    "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"
      }
    ]
  }
]

My Screen should have posts , Images and Videos (Like FB Timeline) . Problem is When I run my code on my Device it seems that the Adapter doesn't Inflate anything Except text "posts" Only (No Images Appears ) , but if we take a look to response it should show 5 posts then 3 images .

My IslamicTimeline Adapter looks like :

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);
        }
    }


}

I see many posts in stackoverflow but , I can't solve my problem , I Expect that my problem is in the adapter .

1

There are 1 answers

9
Jaymin Panchal On

You are returning the view type based on the content and every time you are going to get the content in your JSON reaponse for the key content_text

 if (islamicList.get(position).postContent.length() > 0 && islamicList.get(position).postContent != null)
        return ISLAMIC_POST;

{EDIT}

Just try to reverse the condition:

@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;
}