Skip to content

Instantly share code, notes, and snippets.

@esabook
Last active March 16, 2021 14:31
Show Gist options
  • Save esabook/b6533c4f84d136a79eccc18248275bfa to your computer and use it in GitHub Desktop.
Save esabook/b6533c4f84d136a79eccc18248275bfa to your computer and use it in GitHub Desktop.
[Android] Single Fragment for multiple layout/*.xml in <navigation>
package *
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class LayoutFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val layout = getString(R.string.base_fragment_layout)
arguments?.getInt(layout)?.let {
return inflater.inflate(it, container, false)
}
return null
}
}
package *
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.appcompat.widget.LinearLayoutCompat
import androidx.navigation.fragment.FragmentNavigator
import androidx.navigation.fragment.findNavController
/**
* A simple [Fragment] subclass as the default destination in the navigation.
*/
class MainFragment : Fragment(R.layout.fragment_first) {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val viewGroup = view.findViewById<LinearLayoutCompat>(R.id.vg_content)
viewGroup?.run {
findNavController().graph.forEach graph@{ d ->
if ((d as FragmentNavigator.Destination).className == MainFragment::class.java.name) return@graph
val button = Button(requireContext())
button.text = d.label
button.setOnClickListener { findNavController().navigate(d.id) }
this.addView(button)
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/nav_graph"
app:startDestination="@id/FirstFragment">
<fragment
android:id="@+id/FirstFragment"
android:name="*.MainFragment"
android:label="@string/first_fragment_label"
tools:layout="@layout/fragment_first" />
<fragment
android:id="@+id/TypographFragment"
android:name="*.LayoutFragment"
android:label="Typograph"
tools:layout="@layout/fragment_typograph">
<argument
android:name="@string/base_fragment_layout"
android:defaultValue="@layout/fragment_typograph"
app:argType="reference" />
</fragment>
<fragment
android:id="@+id/WidgetFragment"
android:name="*.LayoutFragment"
android:label="Widget"
tools:layout="@layout/fragment_widget">
<argument
android:name="@string/base_fragment_layout"
android:defaultValue="@layout/fragment_widget"
app:argType="reference" />
</fragment>
<fragment
android:id="@+id/AlertFragment"
android:name="*.LayoutFragment"
android:label="Alert"
tools:layout="@layout/fragment_alert">
<argument
android:name="@string/base_fragment_layout"
android:defaultValue="@layout/fragment_alert"
app:argType="reference" />
</fragment>
</navigation>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment