Skip to content

Instantly share code, notes, and snippets.

@realpacific
Created May 17, 2019 05:46
Show Gist options
  • Save realpacific/4b79a743b1ed7b6a68d39149563a117f to your computer and use it in GitHub Desktop.
Save realpacific/4b79a743b1ed7b6a68d39149563a117f to your computer and use it in GitHub Desktop.
Data binding in RecyclerView's Adapter and implementing the listener
interface PlayerStateCallback {
/**
* Callback to when the [PlayerView] has fetched the duration of video
**/
fun onVideoDurationRetrieved(duration: Long, player: Player)
fun onVideoBuffering(player: Player)
fun onStartedPlaying(player: Player)
fun onFinishedPlaying(player: Player)
}
class VideoAdapter : RecyclerView.Adapter<RecyclerView.ViewHolder>(), PlayerStateCallback {
private val mList = mutableListOf<String>()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val binding = DataBindingUtil.inflate<ItemVideoBinding>(
LayoutInflater.from(parent.context), R.layout.item_video, parent, false
)
return VideoViewHolder(binding)
}
override fun onBindViewHolder(viewHolder: VideoViewHolder, position: Int) {
val videoUrl = mList[position]
with(viewHolder.binding) {
url = videoUrl
callback = this@VideoAdapter
executePendingBindings()
}
}
// Setter to set [mList] and notify adapter...
// Overriden methods from PlayerStateCallback interface
class VideoViewHolder constructor(val binding: ItemVideoBinding) : RecyclerView.ViewHolder(binding.root)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment