How can I load Images and Videos in the same list of a RecyclerView
, so that when I click on a video it can play (like Facebook blog style), on Facebook images are seen and as well videos on the cardViews
of a list , Is there any tutorial on this please help me. Am really stuck, I haven't got any solution on this.
How to make a single RecyclerView
with both Images and Videos?
Below is my onStart
Method its where there's FirebaseRecyclerAdapter
it launches the list
@Override
protected void onStart() {
super.onStart();
//checkUserExists();
// mAuth.addAuthStateListener(mAuthLitsener);
try {
FirebaseRecyclerAdapter<Posts, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Posts, BlogViewHolder>(Posts.class,
R.layout.item_activity,
BlogViewHolder.class,
mDatabase) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, final Posts model, final int position) {
final String key_post = getRef(position).getKey();
viewHolder.setTitle(model.getEventTitle());
viewHolder.setDesc(model.getEventDescription());
viewHolder.setImage(c, model.getEventImage());
viewHolder.setVideo(c,model.getEventImage());
viewHolder.imagePost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),""+key_post,Toast.LENGTH_LONG).show();
Intent singleView = new Intent(MainActivity.this,ViewSingleImage.class);
singleView.putExtra("Blog",key_post);
singleView.putExtra("EventName",eventname);
startActivity(singleView);
}
});
}
};
mRecyclerview.setAdapter(firebaseRecyclerAdapter);
}
catch (Exception ex){
System.out.println("Error "+ ex);
}
}
And this is MyViewHolder
Class where I declared both the Images and the Url
public static class BlogViewHolder extends RecyclerView.ViewHolder{
View mView;
private ImageView imagePost;
public BlogViewHolder(View itemView) {
super(itemView);
mView=itemView;
imagePost =(ImageView)mView.findViewById(R.id.post_image);
}
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 = (FullscreenVideoLayout) mView.findViewById(R.id.post_video);
// videoLayout.setActivity(this);
// videoLayout.setActivity(get);
Uri videoUri = Uri.parse(videoUrl);
try {
videoLayout.setVideoURI(videoUri);
} catch (IOException e) {
e.printStackTrace();
}
}
}
So generally, in the ViewHolder
I declared the Image and the Video, so in the OnStart
Method is where I placed My Adapter in order to download the ImageUrl
and the VideoUrl
, but the logic of checking them is still tricky, on my side