Created
May 17, 2019 05:46
-
-
Save realpacific/4b79a743b1ed7b6a68d39149563a117f to your computer and use it in GitHub Desktop.
Data binding in RecyclerView's Adapter and implementing the listener
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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