I am trying to implement an Instagram like recycler view in a fragment which shows Images and Videos. The images work fine. The videoview too works fine for the first time. But when I scroll down and the video view is removed from the displayed elements and then scroll back up, the video does not play.
Pretty sure the videoview isn't being initialized the second time.
What can I do to make it work?
Also when exactly is the onBindViewHolder called?
class PostListAdapter(var posts: ArrayList<Post>) :
RecyclerView.Adapter<PostListAdapter.PostViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = PostViewHolder(
LayoutInflater.from(parent.context).inflate(com.example.mehfil.R.layout.item_post_v2, parent, false)
)
override fun getItemCount() = posts.size
override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
holder.bind(posts[position])
}
My bind method looks like this...
fun bind(post: Post) {
if(post.type!!equals("video")) {
//trigger video
imageView.visibility = View.GONE
videoView.visibility = View.VISIBLE
try {
videoView.setSource("mp4 video link")
} catch (E: Exception) {
Log.d("Video Error", E.toString())
}
}
else
{
videoView.visibility=View.GONE
imageView.visibility=View.VISIBLE
//trigger images
if(post.media!= null)
{
if(post.media!!.indexOf("http:")!= -1)
post.media=post.media!!.replace("http","https")
imageView.loadImage(post.media , progressDrawable)
Log.d("Media link ",post.media)
}
}