1 /*
<lambda>null2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package androidx.navigation.testapp
18 
19 import android.os.Bundle
20 import android.view.LayoutInflater
21 import android.view.View
22 import android.view.ViewGroup
23 import androidx.navigation.NavOptions
24 import androidx.navigation.fragment.AbstractListDetailFragment
25 import androidx.navigation.fragment.NavHostFragment
26 import androidx.recyclerview.widget.RecyclerView
27 
28 class TwoPaneFragment : AbstractListDetailFragment() {
29 
30     override fun onCreateListPaneView(
31         inflater: LayoutInflater,
32         container: ViewGroup?,
33         savedInstanceState: Bundle?
34     ): View {
35         return inflater.inflate(R.layout.list_pane, container, false)
36     }
37 
38     override fun onCreateDetailPaneNavHostFragment(): NavHostFragment {
39         return NavHostFragment.create(R.navigation.two_pane_navigation)
40     }
41 
42     override fun onListPaneViewCreated(view: View, savedInstanceState: Bundle?) {
43         super.onListPaneViewCreated(view, savedInstanceState)
44         val recyclerView = view as RecyclerView
45         recyclerView.adapter =
46             TwoPaneAdapter(map.keys.toTypedArray()) {
47                 map[it]?.let { destId -> openDetails(destId) }
48             }
49     }
50 
51     private fun openDetails(destinationId: Int) {
52         val detailNavController = detailPaneNavHostFragment.navController
53         detailNavController.navigate(
54             destinationId,
55             null,
56             NavOptions.Builder()
57                 .setPopUpTo(detailNavController.graph.startDestinationId, true)
58                 .apply {
59                     if (slidingPaneLayout.isOpen) {
60                         setEnterAnim(R.anim.nav_default_enter_anim)
61                         setExitAnim(R.anim.nav_default_exit_anim)
62                     }
63                 }
64                 .build()
65         )
66         slidingPaneLayout.open()
67     }
68 
69     companion object {
70         val map =
71             mapOf(
72                 "first" to R.id.first_fragment,
73                 "second" to R.id.second_fragment,
74                 "third" to R.id.third_fragment,
75                 "fourth" to R.id.fourth_fragment,
76                 "fifth" to R.id.fifth_fragment
77             )
78     }
79 }
80