来自 RecyclerView/Adapter 的 TimePicker 简单地调暗屏幕

TimePicker From RecyclerView/Adapter Simply Dims Screen

android 开发的新手,似乎无法弄清楚如何在单击我的 RecyclerView 项目中的 item/View 时弹出 TimePicker。我有一个带有 RecyclerView 的 Fragment,我已经关注 https://developer.android.com/guide/topics/ui/controls/pickers,但是,当我单击该项目时,屏幕只是变暗了一点,仅此而已。选择器永远不会显示。

谢谢。

MainActivity.kt

class MainActivity : AppCompatActivity() {
    //...
    override fun onCreate(savedInstanceState: Bundle?) {
        //...
        navigation.setOnNavigationItemSelectedListener(onNavigationItemSelectedListener)
        //...
    }

    private val onNavigationItemSelectedListener = BottomNavigationView.OnNavigationItemSelectedListener { item ->
        when (item.itemId) {
            R.id.navigation_people -> {
                findViewById<TextView>(R.id.title).text = "People"
                supportFragmentManager.transaction(allowStateLoss = true) {
                    replace(R.id.container, PeopleFragment.newInstance())
                }
                return@OnNavigationItemSelectedListener true
            }
            //...
        }
        false
    }
}

Adapter.kt

class Adapter: ListAdapter<Person, PeopleViewAdapter.ViewHolder>(PeopleDiffCallback()) {
//...
    class ViewHolder(val binding: FragmentPeopleDetailBinding) : RecyclerView.ViewHolder(binding.root),
        View.OnClickListener, CompoundButton.OnCheckedChangeListener, ChipGroup.OnCheckedChangeListener {
    //...
        override fun onClick(v: View?) {
            v?.let {
                when (v.id) {
                    //...
                    itemView.start.id -> TimePickerFragment.newInstance(1, 1)
                            .show((v.context as AppCompatActivity).supportFragmentManager, "Test")
                //...
                }
            }
        }
    }
    //..
}

TimePickerFragment.kt

class TimePickerFragment : DialogFragment(), TimePickerDialog.OnTimeSetListener {
    companion object {
        fun newInstance(hour: Int, minute: Int) = DialogFragment().apply {
            arguments = Bundle().apply {
                putInt("hour", hour)
                putInt("minute", minute)
            }
        }
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        super.onCreateDialog(savedInstanceState)

        var hour = 1
        var minute = 1

        arguments?.let {
            hour = it.getInt("hour")
            minute = it.getInt("minute")
        }

        return TimePickerDialog(activity, this, hour, minute, DateFormat.is24HourFormat(context))
    }

    override fun onTimeSet(view: TimePicker?, hourOfDay: Int, minute: Int) {
        Log.d(TAG, "Picked $hourOfDay for hour and $minute for minute.")
    }
}

回答我的一个相当老的问题,我通过使用新的 MaterialTimePicker 解决了这个问题。 TimePickerFragment.kt 不见了,我用下面的内容更新了适配器。虽然我不认为适配器是最好的地方,但这是它目前所在的地方。

MaterialTimePicker.newInstance().apply {
                setTimeFormat(TimeFormat.CLOCK_24H)
                show((context as AppCompatActivity).supportFragmentManager, null)
                setListener { dialog ->
                    ...
                }
            }