使用.isChecked = true / setChecked(true)时复选框可绘制不切换

时间:2018-12-11 13:27:54

标签: android android-view android-checkbox

我将直接处理问题:是否存在已知问题或代码导致复选框(或其他视图)的可绘制对象无法根据其状态正确刷新/更新?如果是这样,有解决方案吗?

设置:我有一个活动,其中添加了一个包含复选框的片段。复选框表现为桃红色,对单击事件做出响应,我可以在onCheckedChangeListener中处理它们。当用户勾选了三个复选框时,该片段将保存在Backstack中,并由后续片段替换。

预期的行为::如果用户要返回到上一个“复选框”片段,则应在正确的复选框上打勾。这些框的CheckState因此应为true,以便在onViewCreated中调用prev

行为未显示为选中框,但在移回上一个片段时,其行为与此相同。所示的可绘制对象代表next + checkbox.isChecked = true

  1. 当我按下一个上述框时,它会显示代表checked = false + pressed false的可绘制对象,然后在第一次按下时返回代表checked = true + pressed = true的可绘制对象,导致我怀疑它们实际上已经按预期进行了检查,但显示为checked = false
  2. 如果我再次单击同一框,它将被选中,这支持了怀疑。
  3. onCheckedChangeListener内的“ isChecked”也支持这种怀疑,因为它们在@ 1和#2上都是false

我所做的事情:我试图将安装程序隔离到另一个应用程序中,并且一切正常。我可以在onViewCreated中调用“ .isChecked = true”,这些复选框将与代表pressed = false的drawable一起出现。有人会认为,当我将片段剥离到最低限度但仍然没有运气时,问题的根源将很容易找到。

随时提出澄清的问题。我已经坚持了几个小时,因此不胜感激!

片段:

checked = false

XML:

checked = true

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.support.v7.widget.AppCompatCheckBox
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CompoundButton
import co.joyship.android.R
import co.joyship.android.base.BaseFragment
import co.joyship.android.co.joyship.android.constants.Screen
import co.joyship.android.data.enums.Interest
import co.joyship.android.data.enums.Interest.*
import kotlinx.android.synthetic.main.fragment_top_interests.*
import java.util.*


class InterestFragment : BaseFragment(), View.OnClickListener {

    private var listOfInterests = ArrayList<String>()

    private var mListener: PreferencesRegistrationInterface? = null
    private var checkAccumulator: Int = 0
    private var interestViewMap = mapOf<Interest, AppCompatCheckBox>()
    private var viewInterestMap = mapOf<Int, Interest>()
    private lateinit var mOnCheckedListener: CompoundButton.OnCheckedChangeListener

    override fun onResume() {
        super.onResume()
        checkBoxesInitially()
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_top_interests, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        mOnCheckedListener = instantiateOnCheckedListener()
        generateViewMap()
        generateInterestMap()

        btnTopInterestsBack?.setOnClickListener(this)
        btnTopInterestsForward?.setOnClickListener(this)

        btnTopInterestsForward?.isEnabled = false
    }

    private fun generateViewMap() {
        interestViewMap = mapOf<Interest, AppCompatCheckBox>(
                Pair(MUSIC, cbMusic),
                Pair(TV_SERIES, cbTv),
                Pair(MOVIES, cbMovies),
                Pair(GAMES, cbGames),
                Pair(READING, cbReading),
                Pair(TRAVEL, cbTravel),
                Pair(SPORTS, cbSports),
                Pair(EXERCISE, cbExercise),
                Pair(PARTY, cbParty),
                Pair(FOOD_BEVERAGE, cbFood),
                Pair(FASHION, cbFashion),
                Pair(POLITICS, cbPolitics),
                Pair(BUSINESS, cbBusiness),
                Pair(TECHNOLOGY, cbTechnology),
                Pair(SCIENCE, cbScience))
    }

    private fun generateInterestMap() {
        viewInterestMap = mapOf(
                Pair(cbMusic.id, MUSIC),
                Pair(cbTv.id, TV_SERIES),
                Pair(cbMovies.id, MOVIES),
                Pair(cbGames.id, GAMES),
                Pair(cbReading.id, READING),
                Pair(cbTravel.id, TRAVEL),
                Pair(cbSports.id, SPORTS),
                Pair(cbExercise.id, EXERCISE),
                Pair(cbParty.id, PARTY),
                Pair(cbFood.id, FOOD_BEVERAGE),
                Pair(cbFashion.id, FASHION),
                Pair(cbPolitics.id, POLITICS),
                Pair(cbBusiness.id, BUSINESS),
                Pair(cbTechnology.id, TECHNOLOGY),
                Pair(cbScience.id, SCIENCE))
    }

    override fun onClick(view: View) {

        if (!clickThrottled())
        when (view.id) {
            R.id.btnTopInterestsForward -> saveInterestsAndMoveForward()
            R.id.btnTopInterestsBack ->
                mListener?.onInterestsSelectionFragmentInteraction(
                        null,
                        null,
                        null,
                        view)
        }
    }

    override fun onAttach(activityContext: Context?) {
        super.onAttach(activityContext)
        if (activityContext is PreferencesRegistrationInterface) {
            mListener = activityContext
        } else {
            throw RuntimeException(activityContext!!.toString() + " must implement PreferencesRegistrationInterface")
        }
    }

    override fun onDetach() {
        mListener = null
        super.onDetach()
    }


    private fun countCheck(isChecked: Boolean) {
        checkAccumulator += if (isChecked) 1 else -1
    }

    private fun checkBoxesInitially() {
        for (i in listOfInterests.indices) {
            interestViewMap
                    .filterKeys { it.toString() == listOfInterests[i] }
                    .values
                    .first().isChecked = true
        }
        checkAccumulator = listOfInterests.size
        interestViewMap.values.forEach { it.setOnCheckedChangeListener(mOnCheckedListener) }
    }

    private fun instantiateOnCheckedListener() =
            CompoundButton.OnCheckedChangeListener { buttonView, isChecked ->


                val boxId = buttonView.id
                if (!clickThrottled()) {
                    countCheck(isChecked)

                    if (checkAccumulator <= 3) {
                        btnTopInterestsForward?.isEnabled = false


                        if (isChecked)
                            listOfInterests.add(viewInterestMap[boxId].toString())
                        else listOfInterests.remove(viewInterestMap[boxId].toString())
                    }

                    if (checkAccumulator == 3 && isChecked) {
                        saveInterestsAndMoveForward()
                    } else if (checkAccumulator > 3) {
                        btnTopInterestsForward!!.isEnabled = true
                        buttonView.isChecked = !isChecked
                        checkAccumulator--
                    }
                    btnTopInterestsForward!!.isEnabled = checkAccumulator == 3

                } else {
                    (buttonView as AppCompatCheckBox).apply {
                        setOnCheckedChangeListener(null)
                        setChecked(!isChecked)
                        setOnCheckedChangeListener(mOnCheckedListener)
                    }
                }
            }


    private fun saveInterestsAndMoveForward() {

        val interest1 = listOfInterests[0]
        val interest2 = listOfInterests[1]
        val interest3 = listOfInterests[2]
        mListener?.onInterestsSelectionFragmentInteraction(interest1, interest2, interest3, null)
    }

    companion object {
        fun newInstance(): InterestFragment {
            return InterestFragment().apply { type = "Interest fragment" }
        }
    }
}

0 个答案:

没有答案