I want to display list of images and videos(fetch from server) in recyclerView with exoPlayer android, not auto play. Only if user click on any video thumbnail then it should to play.
Asked
Active
Viewed 2,601 times
0
-
1Possible duplicate of [I want to display data from Json in recyclerview in Android](https://stackoverflow.com/questions/42667259/i-want-to-display-data-from-json-in-recyclerview-in-android) – Prakash Karena Nov 05 '19 at 09:51
1 Answers
2
For displaying images and video in recycler view you need to create 2 different view holder in the adapter and use below code to get view type for image and video:-
@Override
public int getItemViewType(int position) {
if (data.get(0).isVideo) {
return Constants.LAYOUT_VIDEO;
} else {
return Constants.LAYOUT_IMAGE;
}
}
and below is code for onCreateViewHolder:-
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
if (viewType == Constants.LAYOUT_VIDEO) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_video, parent, false);
return new VideoViewHolder(view);
} else if (viewType == Constants.LAYOUT_IMAGE) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_image, parent, false);
return new ImageViewHolder(view);
}
}
For more info related to display video in recycler view, you can visit the below-given link:-
https://androidwave.com/exoplayer-in-recyclerview-in-android/
And to disable autoplay just write below line before setting URL to exoplayer:-
player.setPlayWhenReady(true);
Ankit Lathiya
- 199
- 1
- 12
-
1I have adapted your solution to mine, and it works pretty well congrats! – Chus Muñoz Nov 09 '21 at 22:21