Idris Stack Idris Stack - 10 days ago 6
Android Question

Images + Videos mixed in a RecyclerView

Am trying to check both images and Videos availability in the

RecyclerView
, but the challenge is the
VideoView
is the only one Visible in all the items List , the
ImageView
is not visible at all, what i want is to check if the Image is present in the ImageView the Videoview GOES, and when the Video is available in the
VideoView
the
ImageView
Goes.

Am developing an App which posts both Videos and Images and retrieves them in the same
RecyclerView
, so please help me how can i achieve this

Below is my code but it doesn't work :

boolean hasDrawable = (viewHolder.imagePost.getDrawable()!= null);

String hasVideo_string =(String)viewHolder.videoLayout.getTag();
boolean hasVideo = Boolean.parseBoolean(hasVideo_string);

if(hasDrawable){
viewHolder.setTitle(model.getEventTitle());
viewHolder.setDesc(model.getEventDescription());
viewHolder.setImage(c, model.getEventImage());
viewHolder.videoLayout.setVisibility(View.INVISIBLE);
}

else if(hasVideo) {
viewHolder.setTitle(model.getEventTitle());
viewHolder.setDesc(model.getEventDescription());
viewHolder.setVideo(c, model.getEventVideo());
viewHolder.imagePost.setVisibility(View.GONE);
}


This is how i set my Image and Video in a
ViewHolder
Class

public void setImage(final Context c,final String imageUrl){
//
Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).fit().centerInside().placeholder(R.mipmap.add_btn)
.networkPolicy(NetworkPolicy.OFFLINE).into(imagePost, new Callback() {
@Override
public void onSuccess() {

}

@Override
public void onError() {

//Reloading an image again ...
Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).placeholder(R.mipmap.add_btn)
.into(imagePost);
}
});

}


and the video (I used a Library called FullscreenVideoLayout , its like a VideoView but much customized)

public void setVideo(final Context c, final String videoUrl){

// videoLayout.setActivity(this);
// videoLayout.setActivity(get);

Uri videoUri = Uri.parse(videoUrl);
try {
videoLayout.setVideoURI(videoUri);
videoLayout.setTag(videoUrl);

} catch (IOException e) {
e.printStackTrace();
}
}


This is the Full
ViewHolder
class

public static class BlogViewHolder extends RecyclerView.ViewHolder{


View mView;
private ImageView imagePost;
private FullscreenVideoLayout videoLayout;
private VideoView videoView;

public BlogViewHolder(View itemView) {
super(itemView);
mView=itemView;
imagePost =(ImageView)mView.findViewById(R.id.post_image);
videoLayout = (FullscreenVideoLayout) mView.findViewById(R.id.post_video);

}
public void setTitle(String title){

TextView post_title = (TextView)mView.findViewById(R.id.post_title);
post_title.setText(title);

}

public void setDesc(String desc){

TextView post_desc = (TextView)mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}


public void setImage(final Context c,final String imageUrl){

//

Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).fit().centerInside().placeholder(R.mipmap.add_btn)
.networkPolicy(NetworkPolicy.OFFLINE).into(imagePost, new Callback() {
@Override
public void onSuccess() {

}

@Override
public void onError() {

//Reloading an image again ...
Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).placeholder(R.mipmap.add_btn)
.into(imagePost);
}
});

}

public void setVideo(final Context c, final String videoUrl){


// videoLayout.setActivity(this);
// videoLayout.setActivity(get);

Uri videoUri = Uri.parse(videoUrl);
try {
videoLayout.setVideoURI(videoUri);
videoLayout.setTag(videoUrl);

} catch (IOException e) {
e.printStackTrace();
}


}

}

Answer

I see you are using Firebase, so since you are getting the Urls of both Videos and Images getEventImage() and getEventVideo() , just check the Urls if they are Empty and Remove the Visibility of the ImageView and VideoView.

Below is the sample code , Try this out:

 public void setImage(final Context c,final String imageUrl){

            try {
                if (imageUrl!=null) {
                    //

                    Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).fit().centerInside().placeholder(R.mipmap.add_btn)
                            .networkPolicy(NetworkPolicy.OFFLINE).into(imagePost, new Callback() {
                        @Override
                        public void onSuccess() {

                        }

                        @Override
                        public void onError() {

                            //Reloading an image again ...
                            Picasso.with(c).load(imageUrl).error(R.mipmap.add_btn).placeholder(R.mipmap.add_btn)
                                    .into(imagePost);
                        }
                    });





                } else {

                    imagePost.setVisibility(View.GONE);
                }
            }
            catch (Exception e){

            }

        }

        public void setVideo(final Context c, final String videoUrl){
            try {
                if (videoUrl!=null) {
                    try {
                        Uri videoUri = Uri.parse(videoUrl);
                        try {
                            videoLayout.setVideoURI(videoUri);
                            videoLayout.setTag(videoUrl);
                            String hasVideo_string = (String) videoLayout.getTag();
                            boolean hasVideo = Boolean.parseBoolean(hasVideo_string);


                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    } catch (Exception e) {
                        System.out.println("Error :" + e);
                    }

                } else {
                    videoLayout.setVisibility(View.GONE);
                }
            }
            catch (Exception e){

            }


        }

Hope it works for you .