// RepeatCycleDialogFragment.kt
|
package com.example.firstapp.ui.reminderOther
|
|
import android.os.Bundle
|
import android.view.LayoutInflater
|
import android.view.View
|
import android.view.ViewGroup
|
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter
|
import com.contrarywind.view.WheelView
|
import com.example.firstapp.R
|
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
|
|
class RepeatCycleDialogFragment : BottomSheetDialogFragment() {
|
|
private lateinit var wheelEvery: WheelView
|
private lateinit var wheelInterval: WheelView
|
private lateinit var wheelUnit: WheelView
|
|
private var listener: OnCycleSelectedListener? = null
|
|
interface OnCycleSelectedListener {
|
fun onCycleSelected(every: String, interval: String, unit: String)
|
}
|
|
fun setOnCycleSelectedListener(listener: OnCycleSelectedListener) {
|
this.listener = listener
|
}
|
|
override fun onCreateView(
|
inflater: LayoutInflater, container: ViewGroup?,
|
savedInstanceState: Bundle?
|
): View? {
|
val view = inflater.inflate(R.layout.fragment_repeat_cycle_dialog_list_dialog, container, false)
|
|
wheelEvery = view.findViewById(R.id.wheelEvery)
|
wheelInterval = view.findViewById(R.id.wheelInterval)
|
wheelUnit = view.findViewById(R.id.wheelUnit)
|
|
// 设置 "每" 数据源
|
val everyOptions = listOf("每")
|
val everyAdapter = ArrayWheelAdapter(everyOptions)
|
wheelEvery.setAdapter(everyAdapter)
|
wheelEvery.currentItem = 0
|
|
// 如果只有一个元素,禁止滚动
|
if (everyOptions.size <= 1) {
|
wheelEvery.setCyclic(false) // 禁止循环
|
wheelEvery.isEnabled = false // 禁止滚动
|
} else {
|
wheelEvery.setCyclic(true) // 允许循环
|
wheelEvery.isEnabled = true // 允许滚动
|
}
|
|
// 设置间隔数据源
|
val intervalOptions = listOf("不重复") + (1..30).map { it.toString() } // 你可以根据需要调整范围
|
val intervalAdapter = ArrayWheelAdapter(intervalOptions)
|
wheelInterval.setAdapter(intervalAdapter)
|
wheelInterval.currentItem = 0
|
|
// 设置单位数据源
|
val unitOptions = listOf("天", "周", "月", "年")
|
val unitAdapter = ArrayWheelAdapter(unitOptions)
|
wheelUnit.setAdapter(unitAdapter)
|
|
// 设置显示的项数,不要重复显示
|
// wheelUnit.setItemsVisibleCount(4) // 设置可见项数,适当调整这个值
|
|
// 禁止循环滚动
|
wheelUnit.setCyclic(false)
|
wheelUnit.currentItem = 0
|
|
// 初始状态:如果 "不重复" 被选中,隐藏 wheelEvery 和 wheelUnit
|
if (intervalOptions[wheelInterval.currentItem] == "不重复") {
|
wheelEvery.visibility = View.GONE
|
wheelUnit.visibility = View.GONE
|
} else {
|
wheelEvery.visibility = View.VISIBLE
|
wheelUnit.visibility = View.VISIBLE
|
}
|
|
// 设置间隔选择监听器
|
wheelInterval.setOnItemSelectedListener { index ->
|
val selectedInterval = intervalOptions[index]
|
if (selectedInterval == "不重复") {
|
wheelEvery.visibility = View.GONE
|
wheelUnit.visibility = View.GONE
|
} else {
|
wheelEvery.visibility = View.VISIBLE
|
wheelUnit.visibility = View.VISIBLE
|
}
|
}
|
|
|
|
// 设置确认按钮
|
view.findViewById<View>(R.id.repeat_cycle_btn_confirm).setOnClickListener {
|
val selectedEvery = everyOptions[wheelEvery.currentItem]
|
val selectedInterval = intervalOptions[wheelInterval.currentItem]
|
val selectedUnit = unitOptions[wheelUnit.currentItem]
|
listener?.onCycleSelected(selectedEvery, selectedInterval, selectedUnit)
|
dismiss()
|
}
|
|
// 设置取消按钮
|
view.findViewById<View>(R.id.repeat_cycle_btn_cancel).setOnClickListener {
|
dismiss()
|
}
|
|
return view
|
}
|
|
companion object {
|
|
// TODO: Customize parameters
|
fun newInstance(): RepeatCycleDialogFragment =
|
RepeatCycleDialogFragment().apply {
|
arguments = Bundle().apply {
|
// 你可以在这里传递参数,但在这个例子中不需要
|
}
|
}
|
|
}
|
}
|