Last active
April 27, 2023 11:07
-
-
Save PrimisMobile/252a0e26d7d4717a570de853df752b53 to your computer and use it in GitHub Desktop.
RecyclerViewAdapterExample.kt
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
package .... | |
import .... | |
import tech.primis.player.PrimisPlayer | |
.... | |
class RecyclerViewAdapter( | |
.... | |
) : RecyclerView.Adapter<RecyclerViewItemHolder>() { | |
.... | |
private var primisPlayer: PrimisPlayer? = null | |
.... | |
companion object { | |
/** | |
* Indicate that the view type is a mock. It is used to build the RecyclerView mock | |
* items. | |
*/ | |
const val VIEW_TYPE_MOCK = 1 | |
/** | |
* Indicate that the view type is for the Primis player. Use this to return a view that | |
* holds the player. | |
*/ | |
const val VIEW_TYPE_PLAYER = 2 | |
} | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewItemHolder { | |
.... | |
if (viewType == VIEW_TYPE_MOCK) { | |
val binding = | |
DataBindingUtil.inflate<RecyclerViewMockItemBinding>( | |
LayoutInflater.from(parent.context), | |
R.layout.recycler_view_mock_item, | |
parent, | |
false | |
) | |
return RecyclerViewItemHolder(binding, viewType) | |
} else { | |
val binding = | |
DataBindingUtil.inflate<RecyclerViewPlayerItemBinding>( | |
LayoutInflater.from(parent.context), | |
R.layout.recycler_view_player_item, | |
parent, | |
false | |
) | |
return RecyclerViewItemHolder(binding, viewType) | |
} | |
.... | |
} | |
override fun onBindViewHolder(holder: RecyclerViewItemHolder, position: Int) { | |
.... | |
holder.bind() | |
.... | |
} | |
override fun onViewDetachedFromWindow(holder: RecyclerViewItemHolder) { | |
super.onViewDetachedFromWindow(holder) | |
.... | |
if (holder.itemViewType == VIEW_TYPE_PLAYER) { | |
primisPlayer?.let { | |
//Notify the player that the RecyclerView.ViewHolder has detached | |
it.onDetachedFromRecyclerView() | |
} | |
} | |
.... | |
} | |
override fun onViewAttachedToWindow(holder: RecyclerViewItemHolder) { | |
super.onViewAttachedToWindow(holder) | |
.... | |
if (holder.itemViewType == VIEW_TYPE_PLAYER) { | |
primisPlayer?.let { | |
//Get a reference to the RecyclerView cell’s player container | |
val playerContainer = | |
DataBindingUtil.findBinding<RecyclerViewPlayerItemBinding>( | |
holder.itemView | |
)?.playerContainer | |
//Notify the player that the RecyclerView.ViewHolder has attached and send the | |
// player container for the Primis player to be added to. | |
it.onAttachedToRecyclerView(playerContainer) | |
} | |
} | |
.... | |
} | |
override fun onAttachedToRecyclerView(recyclerView: RecyclerView) { | |
super.onAttachedToRecyclerView(recyclerView) | |
.... | |
if (primisPlayer == null) { | |
player = PrimisConfiguration.Builder() | |
.placement("123456") | |
.recyclerView(recyclerView) | |
.createPlayer(recyclerView.context) | |
} | |
primisPlayer?.add() | |
.... | |
} | |
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) { | |
super.onDetachedFromRecyclerView(recyclerView) | |
.... | |
primisPlayer?.let { | |
it.remove() | |
} | |
primisPlayer = null | |
.... | |
} | |
override fun getItemCount(): Int { | |
.... | |
} | |
override fun getItemViewType(position: Int): Int { | |
if (position == VIEW_TYPE_PLAYER) { | |
return VIEW_TYPE_PLAYER | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment