RecyclerView Implementation
Android SDK
**Adding the Primis Player to a RecyclerView**
Best practice is to place a dedicated cell only for PrimisPlayer. If it is not possible in your case, please contact our dev team and provide more information about your implementation.
- Add a PrimisPlayer property to the RecyclerView.Adapter.
class RecyclerViewAdapter(
....
) : RecyclerView.Adapter() {
....
private var player: PrimisPlayer? = null
....
}
-
Override onAttachedToRecyclerView method of the RecyclerView.Adapter
and set the Primis player configuration object as described in the previous section.Use the PrimisConfiguration.Builder’s recyclerView() function to pass the instance of your RecyclerView.
Call to PrimisPlayer.add() at the end of this method.
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
super.onAttachedToRecyclerView(recyclerView)
....
if (player == null) {
player = PrimisConfiguration.Builder()
.placement("123456")
.recyclerView(recyclerView)
.createPlayer(recyclerView.context)
}
// This call is important since it notifies PrimisPlayer that the adapter attached
// to RecyclerView.
player?.add()
....
}
If not using DataBinding, holder.itemView.findViewById<>() can be used to get a reference and send to the player container from the ViewHolder.
- Override onViewAttachedToWindow() method of the RecyclerView.Adapter, get a reference to the player container view inside the ViewHolder item and send it to the player through method onAttachedToRecyclerView(). This will notify the PrimisPlayer that the designated cell is attached to window and it can be added to its container:
override fun onViewAttachedToWindow(holder: RecyclerViewItemHolder) {
super.onViewAttachedToWindow(holder)
....
if (holder.itemViewType == VIEW_TYPE_PLAYER) {
player?.let {
// Get a reference to the player container view in the player cell.
val playerContainer = DataBindingUtil
.findBinding(holder.itemView)
?.playerContainer
// Send the player container view to the player.
it.onAttachedToRecyclerView(playerContainer)
}
}
....
}
The above implementation uses itemViewType. (can be done in other ways as well)
- Override onViewDetachedFromWindow() method of the RecyclerView.Adapter to notify the player through method onDetachedFromRecyclerView() that it is no longer attached to RecyclerView:
override fun onViewDetachedFromWindow(holder: RecyclerViewItemHolder) {
super.onViewDetachedFromWindow(holder)
....
if (holder.itemViewType == VIEW_TYPE_PLAYER) {
player?.let {
it.onDetachedFromRecyclerView()
}
}
....
}
-
Depending on your implementation:
For a dedicated cell with the Primis Player:
a. Call onAdapterAttachedToRecyclerView() from onAttachedToRecyclerView() when the RecyclerView is attached to the adapter.
b. Call onAttachedToRecyclerView(playerContainer) from onViewAttachedToWindow() to make the player visible.
c. Call onDetachedFromRecyclerView() from onViewDetachedFromWindow() to hide or pause the player.If the player's view is shared within a ViewHolder with other views:
a. Use onAttachedToRecyclerView() to display the player when needed.
b. Use onDetachedFromRecyclerView() to hide the player when not in use. -
Removing the player
Call the PrimisPlayer.remove() from within the onDetachedFromRecyclerView(recyclerView: RecyclerView)
primisPlayer?.let {
it.remove()
primisPlayer = null
}
When adjusting the height of the player container, it is required to utilize fixed size values. Avoid using "match parent" and "wrap content" options to ensure a consistent and predictable user experience.
Code example
You can find a code example for implementing the Primis player in RecyclerView here
Updated about 1 year ago