Android Fragments Android -未填充“材质暴露”下拉菜单选项

x8diyxa7  于 2023-03-08  发布在  Android
关注(0)|答案(1)|浏览(159)

我正在尝试创建一个材质显示的下拉菜单,当一个片段被重新访问时,这个下拉菜单会被重建。在最初访问片段时,这个菜单会被正确地创建(它有助于创建一个计时器)。暴露的下拉菜单有秒,分钟,小时和天作为选项。当你重新访问片段,我重建下拉菜单时,只有之前选择的选项在那里。例如,如果我选择秒,我离开了这个片段,然后又回来了,只有秒是我可以选择的选项。分钟、小时和天都不是选项。下面是我如何在你第一次访问这个片段时构建公开的下拉菜单(这也是我想要的):

val index = stepNumber - 1
        val ctx = ContextThemeWrapper(activity, R.style.timeUnitsMenu)
        val newView = LayoutInflater.from(ctx)
            .inflate(R.layout.timer, null)
        newView.findViewById<ImageButton>(R.id.delete_timer).setOnClickListener { view -> deleteStep(view) }
        stepsContainer = currentView.findViewById(R.id.recipe_steps_container)
        val timerContainer = newView.findViewById<LinearLayout>(R.id.timer_settings_container)
        val timeUnitsMenu = newView.findViewById<AutoCompleteTextView>(R.id.time_units_menu)
        val timer : LinearLayout = newView.findViewById(R.id.timer_field_layout)
        val timeUnits = resources.getStringArray(R.array.time_units)
        val arrayAdapter = ArrayAdapter(requireContext(), R.layout.dropdown_menu_item, timeUnits)
        timeUnitsMenu.setAdapter(arrayAdapter)
        timeUnitsMenu.addTextChangedListener { createTimerFields(timerContainer, timeUnitsMenu.text.toString(), index) }
        timeUnitsMenu.setText(getString(R.string.minutes), false)
        stepsContainer.addView(timer, stepNumber - 1)
        stepNumber++

下面是在onCreateView()中无法运行的版本:

for (i in 0 until stepsList.size) {
    if (stepsList[i] is MutableList<*>) {
        val list = stepsList[i] as MutableList<*>
        val ctx = ContextThemeWrapper(activity, R.style.timeUnitsMenu)
        val timerLayout = LayoutInflater.from(ctx).inflate(R.layout.timer, null)
        timerLayout.findViewById<ImageButton>(R.id.delete_timer).setOnClickListener { timerView -> deleteStep(timerView) }
        val timerContainer = timerLayout.findViewById<LinearLayout>(R.id.timer_settings_container)
        val timeUnitsMenu = timerLayout.findViewById<AutoCompleteTextView>(R.id.time_units_menu)
        val timeUnits = resources.getStringArray(R.array.time_units)
        val arrayAdapter = ArrayAdapter(timerLayout.context, R.layout.dropdown_menu_item, timeUnits)
        println("${arrayAdapter.getItem(0)} ${arrayAdapter.getItem(1)} ${arrayAdapter.getItem(2)} ${arrayAdapter.getItem(3)}")
        timeUnitsMenu.setAdapter(arrayAdapter)
        timeUnitsMenu.addTextChangedListener { restoreTimerFields(timerContainer, i) }
        val timeUnitsList = ArrayList(listOf(*resources.getStringArray(R.array.time_units)))
        timeUnitsMenu.setText(timeUnitsList[list.size - 1], false)
        stepsContainer.addView(timerLayout, i)
    }
}

请注意,我省略了onCreateView()函数中的一些代码,因为它不相关。任何帮助都将不胜感激。

bwleehnv

bwleehnv1#

将适配器的fill和assignment方法移到onResume方法。

相关问题