У меня есть RecyclerView со списком игроков. Я хочу позвонить своему слушателю, когда игрок нажал. У меня есть журналы, когда игрок нажимается и другой, где реализован слушатель. Журнал, в котором реализован слушатель, не распечатывается. Кто-нибудь видит что-то не так?
TeamSelectionAdapter:
class TeamSelectionAdapter internal constructor(
context: Context,
val listener: TeamSelectionAdapterListener
) : ListAdapter<Player, RecyclerView.ViewHolder>(DIFF_CALLBACK) {
private val inflater: LayoutInflater = LayoutInflater.from(context)
interface TeamSelectionAdapterListener {
fun onPlayerTouched(currentPlayer: Player)
}
inner class TeamViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
private val playerCard = itemView.findViewById<MaterialCardView>(R.id.player_card)
fun bind(player: Player){
playerCard.setOnClickListener {
logDebug("Clicked! Now to call listener")
listener.onPlayerTouched(player)
}
}
}
override fun getItemCount() = currentList.size
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
val playerItemView: View = inflater.inflate(R.layout.playercard_draggable, parent, false)
return TeamViewHolder(playerItemView)
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
(holder as TeamViewHolder).bind(getItem(position))
}
internal fun setTeams(playerList: List<Player>){
submitList(playerList)
}
companion object {
val TAG = PlayerListAdapter::class.java.toString()
private val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Player>() {
override fun areItemsTheSame(oldItem: Player, newItem: Player): Boolean {
return oldItem == newItem
}
override fun areContentsTheSame(oldItem: Player, newItem: Player): Boolean {
return oldItem.id == newItem.id
}
}
}
}
TeamFragment:
class TeamCardFragment: BaseFragment() {
private lateinit var teamBackgroundImage: ImageView
private lateinit var teamRecyclerView: RecyclerView
private lateinit var teamSelectionAdapter: TeamSelectionAdapter
private var teamNumber: Int = -1
private var isDropped: Boolean = false
override fun onInflate(context: Context, attrs: AttributeSet, savedInstanceState: Bundle?) {
super.onInflate(context, attrs, savedInstanceState)
context.obtainStyledAttributes(attrs, R.styleable.TeamSelection).apply {
teamNumber = getInt(R.styleable.TeamSelection_teamNumber, -1)
logDebug("teamNumber: $teamNumber")
recycle()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
teamSelectionAdapter = TeamSelectionAdapter(requireContext(), object : TeamSelectionAdapter.TeamSelectionAdapterListener {
override fun onPlayerTouched(currentPlayer: Player) {
Log.d("******", "****player selected: $currentPlayer")
playerViewModel.setSelectedPlayerId(currentPlayer.playerId)
}
})
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.team_card, container, false)
teamBackgroundImage = view.findViewById(R.id.teamBackground)
teamBackgroundImage.setBackgroundColor(resources.getColor(R.color.color_on_secondary,null))
teamBackgroundImage.background.alpha = 50
teamRecyclerView = view.findViewById(R.id.player_card_recyclerView)
teamRecyclerView.adapter = teamSelectionAdapter
teamRecyclerView.setOnDragListener { v, event ->
val viewSource = event.localState as View
val viewId = v.id
when(event.action){
DragEvent.ACTION_DRAG_ENTERED -> {
when(viewId){
teamRecyclerView.id -> {
logDebug("team $teamNumber drag entered")
}
}
}
DragEvent.ACTION_DROP -> {
isDropped = true
when(viewId){
teamRecyclerView.id -> {
playerViewModel.selectedPlayer.value?.let{
playerViewModel.updatePlayerTeam(teamNumber,it.playerId)
}
}
}
}
}
true
}
playerViewModel.allPlayers.reobserve(this){
logDebug("players changed: $it")
it?.let{ playerList ->
val teamPlayers = playerList.filter { player -> player.team == teamNumber }
teamSelectionAdapter.setTeams(teamPlayers)
}
}
return view
}
}