Kotlin中这种简单的var分配有什么问题?

时间:2018-12-19 00:33:26

标签: android kotlin

我有这个RecyclerView适配器类,其中有一个offsetRound方法。

class LeagueRoundAdapter(private val rounds: List<RoundVO>) : RecyclerView.Adapter<MatchViewHolder>() {
    private var round: Int = 0
        set(value) {
            matches = rounds[value].matches

            notifyDataSetChanged()
        }


    fun offsetRound(offset: Int): Int {
        var newround = round + offset;
        if (newround !in 0 until rounds.size) {
            return round
        }
        round = newround
        return newround
    }

    private var matches = rounds[round].matches


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

    override fun getItemCount(): Int {
        return rounds[round].matches.size
    }


    override fun onBindViewHolder(holder: MatchViewHolder, position: Int) {
        val matchVO = matches[position]
        holder.bind(matchVO)
    }
}

按下上一个/下一个按钮时,我想增加/减少当前回合:

private fun setupEvents(){
    btn_next_round.setOnClickListener{
        val round = getAdapter().offsetRound(Integer.parseInt(btn_next_round.tag as String))
        txt_round_info.text = "$round"
    }

    btn_prev_round.setOnClickListener{
        val round = getAdapter().offsetRound(Integer.parseInt(btn_prev_round.tag as String))
        txt_round_info.text = "$round"
    }
}

按钮标签为“ 1”和“ -1”。

问题在于,在此行round = newround中,变量round不采用newround的值,如下面的调试打印所示:

分配前:

enter image description here

之后:

enter image description here

我现在有点困。我在做愚蠢的事我无法弄清楚吗?

编辑

我在前后放置一个日志:

println("1. round: $round newround: $newround")
round = newround
println("2. round: $round newround: $newround")
println("----")

多次按下按钮:

2018-12-18 23:08:11.830 3349-3349/com.github.alexpfx.soccerchampionship I/System.out: 1. round: 0 newround: 1
2018-12-18 23:08:11.830 3349-3349/com.github.alexpfx.soccerchampionship I/System.out: 2. round: 0 newround: 1
2018-12-18 23:08:11.830 3349-3349/com.github.alexpfx.soccerchampionship I/System.out: ----
2018-12-18 23:08:11.856 3349-3354/com.github.alexpfx.soccerchampionship I/zygote64: Do full code cache collection, code=504KB, data=333KB
2018-12-18 23:08:11.857 3349-3354/com.github.alexpfx.soccerchampionship I/zygote64: After code cache collection, code=496KB, data=281KB
2018-12-18 23:08:14.910 3349-3349/com.github.alexpfx.soccerchampionship I/System.out: 1. round: 0 newround: 1
2018-12-18 23:08:14.911 3349-3349/com.github.alexpfx.soccerchampionship I/System.out: 2. round: 0 newround: 1
2018-12-18 23:08:14.911 3349-3349/com.github.alexpfx.soccerchampionship I/System.out: ----
2018-12-18 23:08:15.661 3349-3349/com.github.alexpfx.soccerchampionship I/System.out: 1. round: 0 newround: 1
2018-12-18 23:08:15.661 3349-3349/com.github.alexpfx.soccerchampionship I/System.out: 2. round: 0 newround: 1
2018-12-18 23:08:15.661 3349-3349/com.github.alexpfx.soccerchampionship I/System.out: ----
2018-12-18 23:08:16.422 3349-3349/com.github.alexpfx.soccerchampionship I/System.out: 1. round: 0 newround: 1
2018-12-18 23:08:16.422 3349-3349/com.github.alexpfx.soccerchampionship I/System.out: 2. round: 0 newround: 1
2018-12-18 23:08:16.423 3349-3349/com.github.alexpfx.soccerchampionship I/System.out: ----

1 个答案:

答案 0 :(得分:2)

我看到您为set定义了round

private var round: Int = 0
    set(value) {
        matches = rounds[value].matches

        notifyDataSetChanged()
    }

但是您实际上忘记了update the value of the backing field。设置器应如下所示:

private var round: Int = 0
    set(value) {
        field = value // Note this line
        matches = rounds[value].matches

        notifyDataSetChanged()
    }