Есть фрагмент с recycle, нужно чтобы при нажатии на какой-либо элемент открывался новый фрагмент, я не особо понимаю как это сделать с navigation
MainFragment.kt
class MainFragment: Fragment() {
private var _binding: FragmentMainBinding? = null
private val binding get() = _binding!!
// private var binding: FragmentMainBinding? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
_binding = FragmentMainBinding.inflate(inflater, container, false)
val view = binding.root
return view
// val rootView = inflater.inflate(R.layout.fragment_main, container, false)
//return rootView
// return super.onCreateView(inflater, container, savedInstanceState)?.also {
// binding = FragmentMainBinding.bind(it)
// }
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding?.recyclerView?.adapter = RecycleAdapter(fillList())
val c = Calendar.getInstance()
val year = c.get(Calendar.YEAR)
val month = c.get(Calendar.MONTH)
val day = c.get(Calendar.DAY_OF_MONTH)
binding?.pickDateBtn?.setOnClickListener {
val dpd = DatePickerDialog(requireContext(), DatePickerDialog.OnDateSetListener{ view: DatePicker?, mYear: Int, mMonth: Int, dayOfMonth: Int ->
binding?.pickDateBtn?.setText("" + dayOfMonth + "." + (mMonth+1) + "." + mYear)}, year,month,day)
dpd.show()
}
}
override fun onDestroyView(){
_binding = null
super.onDestroyView()
}
private fun fillList(): List<String> {
val data = mutableListOf<String>()
(0..30).forEach { i -> data.add("$i element") }
return data
}
}
RecycleAdapter.kt
class RecycleAdapter(private val names: List<String>):
RecyclerView.Adapter<RecycleAdapter.MyViewHolder>(){
class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var textViewUp: TextView? = null
var textViewDown: TextView? = null
init {
textViewUp = itemView.findViewById(R.id.textViewUp)
textViewDown = itemView.findViewById(R.id.textViewDown)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.recycleview_item, parent, false)
return MyViewHolder(itemView)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.textViewDown?.text = names[position]
holder.textViewUp?.text = "РУБЛЬ"
}
override fun getItemCount() = names.size}