FragmentContainerView
class FragmentContainerView : FrameLayout
| kotlin.Any | ||||
| ↳ | android.view.View | |||
| ↳ | android.view.ViewGroup | |||
| ↳ | android.widget.FrameLayout | |||
| ↳ | androidx.fragment.app.FragmentContainerView |
FragmentContainerView is a customized Layout designed specifically for Fragments. It extends FrameLayout, so it can reliably handle Fragment Transactions, and it also has additional features to coordinate with fragment behavior.
FragmentContainerView should be used as the container for Fragments, commonly set in the xml layout of an activity, e.g.:
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.fragment.app.FragmentContainerView>
FragmentContainerView can also be used to add a Fragment by using the android:name attribute. FragmentContainerView will perform a one time operation that:
-
Creates a new instance of the Fragment
-
Calls
Fragment.onInflate -
Executes a FragmentTransaction to add the Fragment to the appropriate FragmentManager
You can optionally include an android:tag which allows you to use FragmentManager.findFragmentByTag to retrieve the added Fragment.
<androidx.fragment.app.FragmentContainerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/fragment_container_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.MyFragment"
android:tag="my_tag">
</androidx.fragment.app.FragmentContainerView>
FragmentContainerView should not be used as a replacement for other ViewGroups (FrameLayout, LinearLayout, etc) outside of Fragment use cases.
FragmentContainerView will only allow views returned by a Fragment's Fragment.onCreateView. Attempting to add any other view will result in an IllegalStateException.
Layout animations and transitions are disabled for FragmentContainerView for APIs above 17. Otherwise, Animations should be done through FragmentTransaction.setCustomAnimations. If animateLayoutChanges is set to true or setLayoutTransition is called directly an UnsupportedOperationException will be thrown.
Fragments using exit animations are drawn before all others for FragmentContainerView. This ensures that exiting Fragments do not appear on top of the view.
Summary
Public constructors |
|---|
FragmentContainerView(context: Context) |
FragmentContainerView(Do not call this constructor directly. |
Public functions |
|
|---|---|
open Unit |
addView(child: View, index: Int, params: ViewGroup.LayoutParams?)FragmentContainerView will only allow views returned by a Fragment's |
open WindowInsets |
@RequiresApi(value = 20){@inheritDoc} |
open Unit |
endViewTransition(view: View) |
F |
<F : Fragment?> getFragment()This method grabs the |
open WindowInsets |
@RequiresApi(value = 20) |
open Unit |
|
open Unit |
removeView(view: View) |
open Unit |
removeViewAt(index: Int) |
open Unit |
removeViewInLayout(view: View) |
open Unit |
removeViews(start: Int, count: Int) |
open Unit |
removeViewsInLayout(start: Int, count: Int) |
open Unit |
setLayoutTransition(transition: LayoutTransition?)When called, this method throws a |
open Unit |
|
open Unit |
startViewTransition(view: View) |
Protected functions |
|
|---|---|
open Unit |
dispatchDraw(canvas: Canvas) |
open Boolean |
Public constructors
FragmentContainerView
FragmentContainerView(
context: Context,
attrs: AttributeSet?,
defStyleAttr: Int = 0
)
Do not call this constructor directly. Doing so will result in an UnsupportedOperationException.
Public functions
addView
open fun addView(child: View, index: Int, params: ViewGroup.LayoutParams?): Unit
FragmentContainerView will only allow views returned by a Fragment's Fragment.onCreateView. Attempting to add any other view will result in an IllegalStateException.
{@inheritDoc}
dispatchApplyWindowInsets
@RequiresApi(value = 20)
open fun dispatchApplyWindowInsets(insets: WindowInsets): WindowInsets
{@inheritDoc}
The sys ui flags must be set to enable extending the layout into the window insets.
getFragment
fun <F : Fragment?> getFragment(): F
This method grabs the Fragment whose view was most recently added to the container. This may used as an alternative to calling FragmentManager.findFragmentById and passing in the FragmentContainerView's id.
| Returns | |
|---|---|
F |
The fragment if any exist, null otherwise. |
onApplyWindowInsets
@RequiresApi(value = 20)
open fun onApplyWindowInsets(insets: WindowInsets): WindowInsets
setLayoutTransition
open fun setLayoutTransition(transition: LayoutTransition?): Unit
When called, this method throws a UnsupportedOperationException on APIs above 17. On APIs 17 and below, it calls FrameLayout.setLayoutTransition. This can be called either explicitly, or implicitly by setting animateLayoutChanges to true.
View animations and transitions are disabled for FragmentContainerView for APIs above 17. Use FragmentTransaction.setCustomAnimations and FragmentTransaction.setTransition.
ref android.R.styleable#ViewGroup_animateLayoutChanges
| Parameters | |
|---|---|
transition: LayoutTransition? |
The LayoutTransition object that will animated changes in layout. A value of |
setOnApplyWindowInsetsListener
open fun setOnApplyWindowInsetsListener(
listener: View.OnApplyWindowInsetsListener?
): Unit