![](/img/trans.png)
[英]How to open a fragment from bottom navigation bar which is itself in a fragment?
[英]How to open some fragment from bottom nav bar fragment
我有底部導航欄,在主要活動中定義了 3 個片段。 在其中一個片段中,我想通過單擊日歷視圖(屏幕 1)下的回收器視圖中的項目來打開一些片段。 當我單擊它時,會打開一個帶有底部導航欄的新片段(屏幕 2) 。 如何在沒有顯示架構正確的底部導航欄的情況下打開這個新片段?
class ScheduleFragment : Fragment(), EventCardAdapter.OnEventClickListener{
private lateinit var calendarView: CalendarView
private lateinit var recyclerView: RecyclerView
private lateinit var viewAdapter: EventCardAdapter
private lateinit var viewManager: RecyclerView.LayoutManager
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.schedule_fragment, container, false)
viewManager = LinearLayoutManager(context)
viewAdapter = EventCardAdapter(events, this)
recyclerView = view.findViewById(R.id.events_recycler_view)
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
calendarView = view.findViewById(R.id.schedule_view)
calendarView.setOnDateChangeListener { _, year, month, dayOfMonth ->
Toast.makeText(
context,
"$dayOfMonth/$month/$year",
Toast.LENGTH_LONG
).show()
}
recyclerView.apply {
setHasFixedSize(true)
layoutManager = viewManager
adapter = viewAdapter
}
}
// moving to fragment by clicking item in recycler
override fun onEventClick(view: View, position: Int) {
view.findNavController().navigate(R.id.action_scheduleFragment_to_attendanceEntryFragment)
}
}
在托管活動上實現目標更改偵聽器回調,然后當目標 ID 等於您想要在其上顯示導航欄的片段之一時,將導航欄可見性設置為可見,否則將其設置為消失
navController.addOnDestinationChangedListener{}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.