OiO.lk Blog Android Previous selected items doesn't update their views when are unselected android studio
Android

Previous selected items doesn't update their views when are unselected android studio


How can I redraw or update a few items when two or more of them are unselected? The background tint changes correctly on the first click, but after that, unselecting doesn’t work for the second item. Look at this video to understand what is the problem. It feels like the notifyItemChanged(position) only works with the first unselected item.

Adapter

class ScorePreviewAdapter(
    private val listener: OnBallClickListener,
    private var scorePreviewBallList: List<BallInfo> = emptyList()
) :
    RecyclerView.Adapter<ScorePreviewViewHolder>() {

    fun updateList(list: List<BallInfo>) {
        scorePreviewBallList = list
        notifyDataSetChanged()
    }

    fun updateItemView(position: Int){
        notifyItemChanged(position)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ScorePreviewViewHolder {
        val view =
            LayoutInflater.from(parent.context).inflate(R.layout.item_score_ball, parent, false)
        return ScorePreviewViewHolder(view, listener)
    }

    override fun getItemCount(): Int = scorePreviewBallList.size

    override fun onBindViewHolder(holder: ScorePreviewViewHolder, position: Int) {
        holder.render(scorePreviewBallList[position])
    }
}

ViewHolder:

class ScorePreviewViewHolder(view: View, private var listener: OnBallClickListener) :
    RecyclerView.ViewHolder(view) {

    //    setting binding
    private val binding = ItemScoreBallBinding.bind(view)


    fun render(
        ballInfo: BallInfo
    ) {
        binding.ivScoreBall.alpha = setAlpha(ballInfo)
        binding.ivScoreBall.setImageResource(ballInfo.img)
        if (ballInfo.teamInfo != null) {
            changeColor(ballInfo.teamInfo!!.color)
        }
        binding.ivScoreBall.setOnClickListener {
            listener.onBallClicked(adapterPosition, ballInfo, this)

        }
    }

    private fun setAlpha(ballInfo: BallInfo): Float {
        return if (ballInfo.isSelected) 0.5f else 1f
    }

    private fun changeColor(color: Int) {
        binding.ivScoreBall.setBackgroundTintList(
            ColorStateList.valueOf(
                ContextCompat.getColor(
                    binding.ivScoreBall.context,
                    color
                )
            )
        )
    }
}

Interface

interface OnBallClickListener {
    fun onBallClicked(position: Int, ball: BallInfo, holder: ScorePreviewViewHolder)
}

Fragment

private val onBallListener = object : OnBallClickListener {
        override fun onBallClicked(position: Int, ball: BallInfo, holder: ScorePreviewViewHolder) {
            if (ball.teamInfo == null || ball.teamInfo == turn) {
                when (ball.isSelected) {
                    true -> {
                        holder.itemView.alpha = 1f
                        changeColor(R.color.transparent, holder)
                        if (ball.teamInfo == turn) {
                            ball.teamInfo = null
                        }
                    }

                    false -> {
                        holder.itemView.alpha = 0.5f
                        changeColor(R.color.red, holder)

                    }
                }
                scoreBoardViewModel.onBallClickedLogic(ball)
            }
            scoreAdapter.updateItemView(position)
        }
    }

[...]

private fun initRecyclerView() {
        scoreAdapter = ScorePreviewAdapter(onBallListener)

        binding.rvScoreBall.apply {
            layoutManager = GridLayoutManager(context, 5)
            adapter = scoreAdapter
        }
    }

private fun initUIState() {
        lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                scorePreviewViewModel.score.collect {
                    scoreAdapter.updateList(it)
                }
            }
        }
[...]



You need to sign in to view this answers

Exit mobile version