Android开发的新手,似乎无法弄清楚单击RecyclerView项目中的项目/视图时如何弹出TimePicker。我有一个带有RecyclerView的片段,并且遵循了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.")
}
}
答案 0 :(得分:0)
回答了我的一个很老的问题,我改用了新的MaterialTimePicker解决了这个问题。 TimePickerFragment.kt不见了,我用下面的内容更新了适配器。虽然我不认为适配器是实现此目的的最佳选择,但这是当前的位置。
MaterialTimePicker.newInstance().apply {
setTimeFormat(TimeFormat.CLOCK_24H)
show((context as AppCompatActivity).supportFragmentManager, null)
setListener { dialog ->
...
}
}