java.lang.Object | ||
↳ | android.view.View | |
↳ | android.view.ViewGroup |
Known Direct Subclasses |
Known Indirect Subclasses |
A ViewGroup
is a special view that can contain other views
(called children.) The view group is the base class for layouts and views
containers. This class also defines the
ViewGroup.LayoutParams
class which serves as the base
class for layouts parameters.
Also see ViewGroup.LayoutParams
for layout attributes.
For more information about creating user interface layouts, read the XML Layouts developer guide.
Here is a complete implementation of a custom ViewGroup that implements
a simple FrameLayout
along with the ability to stack
children in left and right gutters.
import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.RemoteViews; /** * Example of writing a custom layout manager. This is a fairly full-featured * layout manager that is relatively general, handling all layout cases. You * can simplify it for more specific cases. */ @RemoteViews.RemoteView public class CustomLayout extends ViewGroup { /** The amount of space used by children in the left gutter. */ private int mLeftWidth; /** The amount of space used by children in the right gutter. */ private int mRightWidth; /** These are used for computing child frames based on their gravity. */ private final Rect mTmpContainerRect = new Rect(); private final Rect mTmpChildRect = new Rect(); public CustomLayout(Context context) { super(context); } public CustomLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CustomLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } /** * Any layout manager that doesn't scroll will want this. */ @Override public boolean shouldDelayChildPressedState() { return false; } /** * Ask all children to measure themselves and compute the measurement of this * layout based on the children. */ @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int count = getChildCount(); // These keep track of the space we are using on the left and right for // views positioned there; we need member variables so we can also use // these for layout later. mLeftWidth = 0; mRightWidth = 0; // Measurement will ultimately be computing these values. int maxHeight = 0; int maxWidth = 0; int childState = 0; // Iterate through all children, measuring them and computing our dimensions // from their size. for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (child.getVisibility() != GONE) { // Measure the child. measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0); // Update our size information based on the layout params. Children // that asked to be positioned on the left or right go in those gutters. final LayoutParams lp = (LayoutParams) child.getLayoutParams(); if (lp.position == LayoutParams.POSITION_LEFT) { mLeftWidth += Math.max(maxWidth, child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin); } else if (lp.position == LayoutParams.POSITION_RIGHT) { mRightWidth += Math.max(maxWidth, child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin); } else { maxWidth = Math.max(maxWidth, child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin); } maxHeight = Math.max(maxHeight, child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin); childState = combineMeasuredStates(childState, child.getMeasuredState()); } } // Total width is the maximum width of all inner children plus the gutters. maxWidth += mLeftWidth + mRightWidth; // Check against our minimum height and width maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight()); maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth()); // Report our final dimensions. setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState), resolveSizeAndState(maxHeight, heightMeasureSpec, childState << MEASURED_HEIGHT_STATE_SHIFT)); } /** * Position all children within this layout. */ @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { final int count = getChildCount(); // These are the far left and right edges in which we are performing layout. int leftPos = getPaddingLeft(); int rightPos = right - left - getPaddingRight(); // This is the middle region inside of the gutter. final int middleLeft = leftPos + mLeftWidth; final int middleRight = rightPos - mRightWidth; // These are the top and bottom edges in which we are performing layout. final int parentTop = getPaddingTop(); final int parentBottom = bottom - top - getPaddingBottom(); for (int i = 0; i < count; i++) { final View child = getChildAt(i); if (child.getVisibility() != GONE) { final LayoutParams lp = (LayoutParams) child.getLayoutParams(); final int width = child.getMeasuredWidth(); final int height = child.getMeasuredHeight(); // Compute the frame in which we are placing this child. if (lp.position == LayoutParams.POSITION_LEFT) { mTmpContainerRect.left = leftPos + lp.leftMargin; mTmpContainerRect.right = leftPos + width + lp.rightMargin; leftPos = mTmpContainerRect.right; } else if (lp.position == LayoutParams.POSITION_RIGHT) { mTmpContainerRect.right = rightPos - lp.rightMargin; mTmpContainerRect.left = rightPos - width - lp.leftMargin; rightPos = mTmpContainerRect.left; } else { mTmpContainerRect.left = middleLeft + lp.leftMargin; mTmpContainerRect.right = middleRight - lp.rightMargin; } mTmpContainerRect.top = parentTop + lp.topMargin; mTmpContainerRect.bottom = parentBottom - lp.bottomMargin; // Use the child's gravity and size to determine its final // frame within its container. Gravity.apply(lp.gravity, width, height, mTmpContainerRect, mTmpChildRect); // Place the child. child.layout(mTmpChildRect.left, mTmpChildRect.top, mTmpChildRect.right, mTmpChildRect.bottom); } } } // ---------------------------------------------------------------------- // The rest of the implementation is for custom per-child layout parameters. // If you do not need these (for example you are writing a layout manager // that does fixed positioning of its children), you can drop all of this. @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { return new CustomLayout.LayoutParams(getContext(), attrs); } @Override protected LayoutParams generateDefaultLayoutParams() { return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); } @Override protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) { return new LayoutParams(p); } @Override protected boolean checkLayoutParams(ViewGroup.LayoutParams p) { return p instanceof LayoutParams; } /** * Custom per-child layout information. */ public static class LayoutParams extends MarginLayoutParams { /** * The gravity to apply with the View to which these layout parameters * are associated. */ public int gravity = Gravity.TOP | Gravity.START; public static int POSITION_MIDDLE = 0; public static int POSITION_LEFT = 1; public static int POSITION_RIGHT = 2; public int position = POSITION_MIDDLE; public LayoutParams(Context c, AttributeSet attrs) { super(c, attrs); // Pull the layout param values from the layout XML during // inflation. This is not needed if you don't care about // changing the layout behavior in XML. TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.CustomLayoutLP); gravity = a.getInt(R.styleable.CustomLayoutLP_android_layout_gravity, gravity); position = a.getInt(R.styleable.CustomLayoutLP_layout_position, position); a.recycle(); } public LayoutParams(int width, int height) { super(width, height); } public LayoutParams(ViewGroup.LayoutParams source) { super(source); } } }
If you are implementing XML layout attributes as shown in the example, this is the
corresponding definition for them that would go in res/values/attrs.xml
:
<declare-styleable name="CustomLayoutLP"> <attr name="android:layout_gravity" /> <attr name="layout_position"> <enum name="middle" value="0" /> <enum name="left" value="1" /> <enum name="right" value="2" /> </attr> </declare-styleable>
Finally the layout manager can be used in an XML layout like so:
<com.example.android.apis.view.CustomLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res/com.example.android.apis" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- put first view to left. --> <TextView android:background="@drawable/filled_box" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_position="left" android:layout_gravity="fill_vertical|center_horizontal" android:text="l1"/> <!-- stack second view to left. --> <TextView android:background="@drawable/filled_box" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_position="left" android:layout_gravity="fill_vertical|center_horizontal" android:text="l2"/> <!-- also put a view on the right. --> <TextView android:background="@drawable/filled_box" android:layout_width="wrap_content" android:layout_height="wrap_content" app:layout_position="right" android:layout_gravity="fill_vertical|center_horizontal" android:text="r1"/> <!-- by default views go in the middle; use fill vertical gravity --> <TextView android:background="@drawable/green" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="fill_vertical|center_horizontal" android:text="fill-vert"/> <!-- by default views go in the middle; use fill horizontal gravity --> <TextView android:background="@drawable/green" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_vertical|fill_horizontal" android:text="fill-horiz"/> <!-- by default views go in the middle; use top-left gravity --> <TextView android:background="@drawable/blue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|left" android:text="top-left"/> <!-- by default views go in the middle; use center gravity --> <TextView android:background="@drawable/blue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="center"/> <!-- by default views go in the middle; use bottom-right --> <TextView android:background="@drawable/blue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|right" android:text="bottom-right"/> </com.example.android.apis.view.CustomLayout>
Nested Classes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
ViewGroup.LayoutParams | LayoutParams are used by views to tell their parents how they want to be laid out. | ||||||||||
ViewGroup.MarginLayoutParams | Per-child layout information for layouts that support margins. | ||||||||||
ViewGroup.OnHierarchyChangeListener | Interface definition for a callback to be invoked when the hierarchy within this view changed. |
XML Attributes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
android:addStatesFromChildren | Sets whether this ViewGroup's drawable states also include its children's drawable states. | ||||||||||
android:alwaysDrawnWithCache | Defines whether the ViewGroup should always draw its children using their drawing cache or not. | ||||||||||
android:animateLayoutChanges | setLayoutTransition(LayoutTransition) | Defines whether changes in layout (caused by adding and removing items) should cause a LayoutTransition to run. | |||||||||
android:animationCache | Defines whether layout animations should create a drawing cache for their children. | ||||||||||
android:clipChildren | setClipChildren(boolean) | Defines whether a child is limited to draw inside of its bounds or not. | |||||||||
android:clipToPadding | setClipToPadding(boolean) | Defines whether the ViewGroup will clip its drawing surface so as to exclude the padding area. | |||||||||
android:descendantFocusability | Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus. | ||||||||||
android:layoutAnimation | Defines the layout animation to use the first time the ViewGroup is laid out. | ||||||||||
android:layoutMode | setLayoutMode(int) | Defines the layout mode of this ViewGroup. | |||||||||
android:persistentDrawingCache | Defines the persistence of the drawing cache. | ||||||||||
android:splitMotionEvents | setMotionEventSplittingEnabled(boolean) | Sets whether this ViewGroup should split MotionEvents to separate child views during touch event dispatch. |
[Expand]
Inherited XML Attributes | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.view.View
|
Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
int | CLIP_TO_PADDING_MASK | We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL are set at the same time. | |||||||||
int | FOCUS_AFTER_DESCENDANTS | This view will get focus only if none of its descendants want it. | |||||||||
int | FOCUS_BEFORE_DESCENDANTS | This view will get focus before any of its descendants. | |||||||||
int | FOCUS_BLOCK_DESCENDANTS | This view will block any of its descendants from getting focus, even if they are focusable. | |||||||||
int | LAYOUT_MODE_CLIP_BOUNDS | This constant is a layoutMode . |
|||||||||
int | LAYOUT_MODE_OPTICAL_BOUNDS | This constant is a layoutMode . |
|||||||||
int | PERSISTENT_ALL_CACHES | Used to indicate that all drawing caches should be kept in memory. | |||||||||
int | PERSISTENT_ANIMATION_CACHE | Used to indicate that the animation drawing cache should be kept in memory. | |||||||||
int | PERSISTENT_NO_CACHE | Used to indicate that no drawing cache should be kept in memory. | |||||||||
int | PERSISTENT_SCROLLING_CACHE | Used to indicate that the scrolling drawing cache should be kept in memory. |
[Expand]
Inherited Constants | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.view.View
|
[Expand]
Inherited Fields | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.view.View
|
Public Constructors | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Adds the children of a given View for accessibility.
| |||||||||||
Adds any focusable views that are descendants of this view (possibly
including this view if it is focusable itself) to views.
| |||||||||||
Returns whether this ViewGroup's drawable states also include
its children's drawable states.
| |||||||||||
Add any touchable views that are descendants of this view (possibly
including this view if it is touchable itself) to views.
| |||||||||||
Adds a child view with the specified layout parameters.
| |||||||||||
Adds a child view with the specified layout parameters.
| |||||||||||
Adds a child view.
| |||||||||||
Adds a child view. | |||||||||||
Adds a child view with this ViewGroup's default layout parameters and the
specified width and height.
| |||||||||||
Change the z order of the child so it's on top of all other children.
| |||||||||||
If
addStatesFromChildren() is true, refreshes this group's
drawable state (to include the states from its children). | |||||||||||
Called when a child view has changed whether or not it is tracking transient state.
| |||||||||||
Called when a child of this parent is giving up focus
| |||||||||||
Removes any pending animations for views that have been removed.
| |||||||||||
Called when this view wants to give up focus.
| |||||||||||
Dispatch a notification about a resource configuration change down
the view hierarchy.
| |||||||||||
Dispatch a hint about whether this view is displayed.
| |||||||||||
Detects if this View is enabled and has a drag event listener.
| |||||||||||
Dispatch a key event to the next view on the focus path.
| |||||||||||
Dispatch a key event before it is processed by any input method
associated with the view hierarchy.
| |||||||||||
Dispatches a key shortcut event.
| |||||||||||
Dispatch setActivated to all of this View's children.
| |||||||||||
Dispatch setSelected to all of this View's children.
| |||||||||||
Dispatch callbacks to
setOnSystemUiVisibilityChangeListener(View.OnSystemUiVisibilityChangeListener) down
the view hierarchy. | |||||||||||
Pass the touch screen motion event down to the target view, or this
view if it is the target.
| |||||||||||
Pass a trackball motion event down to the focused view.
| |||||||||||
This method is the last chance for the focused view and its ancestors to
respond to an arrow key.
| |||||||||||
Called when the window containing this view gains or loses window focus.
| |||||||||||
Dispatch callbacks to
onWindowSystemUiVisibilityChanged(int) down
the view hierarchy. | |||||||||||
Dispatch a window visibility change down the view hierarchy.
| |||||||||||
This method should always be called following an earlier call to
startViewTransition(View) . | |||||||||||
Find the view in the hierarchy rooted at this view that currently has
focus.
| |||||||||||
Finds the Views that contain given text.
| |||||||||||
Find the nearest view in the specified direction that wants to take
focus.
| |||||||||||
Tells the parent that a new focusable view has become available.
| |||||||||||
This is used by the RootView to perform an optimization when
the view hierarchy contains one or several SurfaceView.
| |||||||||||
Returns a new set of layout parameters based on the supplied attributes set.
| |||||||||||
Returns the view at the specified position in the group.
| |||||||||||
Returns the number of children in the group.
| |||||||||||
Does the hard part of measureChildren: figuring out the MeasureSpec to
pass to a particular child.
| |||||||||||
Compute the visible part of a rectangular region defined in terms of a child view's
coordinates.
| |||||||||||
Returns whether ths group's children are clipped to their bounds before drawing.
| |||||||||||
Gets the descendant focusability of this view group.
| |||||||||||
Returns the focused child of this view, if any.
| |||||||||||
Returns the layout animation controller used to animate the group's
children.
| |||||||||||
Returns the animation listener to which layout animation events are
sent.
| |||||||||||
Returns the basis of alignment during layout operations on this ViewGroup:
either
LAYOUT_MODE_CLIP_BOUNDS or LAYOUT_MODE_OPTICAL_BOUNDS . | |||||||||||
Gets the LayoutTransition object for this ViewGroup.
| |||||||||||
Returns the ViewGroupOverlay for this view group, creating it if it does
not yet exist.
| |||||||||||
Returns an integer indicating what types of drawing caches are kept in memory.
| |||||||||||
Returns true if this view has or contains focus
| |||||||||||
Returns true if this view is focusable or if it contains a reachable View
for which
hasFocusable() returns true.
| |||||||||||
Indicates whether the view is currently tracking transient state that the
app should not need to concern itself with saving and restoring, but that
the framework should take special note to preserve when possible.
| |||||||||||
Returns the position in the group of the specified child view.
| |||||||||||
Don't call or override this method.
| |||||||||||
Don't call or override this method.
| |||||||||||
Indicates whether this ViewGroup will always try to draw its children using their
drawing cache.
| |||||||||||
Indicates whether the children's drawing cache is used during a layout
animation.
| |||||||||||
Returns true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
| |||||||||||
Call
Drawable.jumpToCurrentState()
on all Drawable objects associated with this view. | |||||||||||
Assign a size and position to a view and all of its
descendants
This is the second phase of the layout mechanism. | |||||||||||
Notifies a view parent that the accessibility state of one of its
descendants has changed and that the structure of the subtree is
different.
| |||||||||||
Offset a rectangle that is in a descendant's coordinate
space into our coordinate space.
| |||||||||||
Offset a rectangle that is in our coordinate space into an ancestor's
coordinate space.
| |||||||||||
Implement this method to intercept hover events before they are handled
by child views.
| |||||||||||
Implement this method to intercept all touch screen motion events.
| |||||||||||
Called when a child has requested sending an
AccessibilityEvent and
gives an opportunity to its parent to augment the event. | |||||||||||
Tell view hierarchy that the global view attributes need to be
re-evaluated.
| |||||||||||
Call this method to remove all child views from the
ViewGroup.
| |||||||||||
Called by a ViewGroup subclass to remove child views from itself,
when it must first know its size on screen before it can calculate how many
child views it will render.
| |||||||||||
Note: do not invoke this method from
| |||||||||||
Removes the view at the specified position in the group.
| |||||||||||
Removes a view during layout.
| |||||||||||
Removes the specified range of views from the group.
| |||||||||||
Removes a range of views during layout.
| |||||||||||
Called when a child of this parent wants focus
| |||||||||||
Called when a child of this group wants a particular rectangle to be
positioned onto the screen.
| |||||||||||
Called when a child does not want this parent and its ancestors to
intercept touch events with
onInterceptTouchEvent(MotionEvent) .
| |||||||||||
Call this to try to give focus to a specific view or to one of its descendants
and give it hints about the direction and a specific rectangle that the focus
is coming from.
Looks for a view to give focus to respecting the setting specified by
getDescendantFocusability() . | |||||||||||
Called by a child to request from its parent to send an
AccessibilityEvent .
| |||||||||||
Called when a child wants the view hierarchy to gather and report
transparent regions to the window compositor.
| |||||||||||
Schedules the layout animation to be played after the next layout pass
of this view group.
| |||||||||||
Sets whether this ViewGroup's drawable states also include
its children's drawable states.
| |||||||||||
Indicates whether this ViewGroup will always try to draw its children using their
drawing cache.
| |||||||||||
Enables or disables the children's drawing cache during a layout animation.
| |||||||||||
By default, children are clipped to their bounds before drawing.
| |||||||||||
By default, children are clipped to the padding of the ViewGroup.
| |||||||||||
Set the descendant focusability of this view group.
| |||||||||||
Sets the layout animation controller used to animate the group's
children after the first layout.
| |||||||||||
Specifies the animation listener to which layout animation events must
be sent.
| |||||||||||
Sets the basis of alignment during the layout of this ViewGroup.
| |||||||||||
Sets the LayoutTransition object for this ViewGroup.
| |||||||||||
Enable or disable the splitting of MotionEvents to multiple children during touch event
dispatch.
| |||||||||||
Register a callback to be invoked when a child is added to or removed
from this view.
| |||||||||||
Indicates what types of drawing caches should be kept in memory after
they have been created.
| |||||||||||
Return true if the pressed state should be delayed for children or descendants of this
ViewGroup.
| |||||||||||
Bring up a context menu for the specified view or its ancestors.
| |||||||||||
Start an action mode for the specified view.
| |||||||||||
Runs the layout animation.
| |||||||||||
This method tells the ViewGroup that the given View object, which should have this
ViewGroup as its parent,
should be kept around (re-displayed when the ViewGroup draws its children) even if it
is removed from its parent.
| |||||||||||
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Adds a view during layout.
| |||||||||||
Adds a view during layout.
| |||||||||||
Subclasses should override this method to set layout animation
parameters on the supplied child.
| |||||||||||
Attaches a view to this view group.
| |||||||||||
Indicates whether the view group has the ability to animate its children
after the first layout.
| |||||||||||
Prevents the specified child to be laid out during the next layout pass.
| |||||||||||
Prints information about this view in the log output, with the tag
VIEW_LOG_TAG .
| |||||||||||
Detaches all views from the parent.
| |||||||||||
Detaches a view from its parent.
| |||||||||||
Detaches a view from its parent.
| |||||||||||
Detaches a range of views from their parents.
| |||||||||||
Called by draw to draw the child views.
| |||||||||||
Perform dispatching of a
saveHierarchyState(android.util.SparseArray) freeze()}
to only this view, not to its children. | |||||||||||
Dispatch a generic motion event to the currently focused view.
| |||||||||||
Dispatch a generic motion event to the view under the first pointer.
| |||||||||||
Dispatch a hover event.
| |||||||||||
Called by
restoreHierarchyState(android.util.SparseArray) to retrieve the
state for this view and its children.
| |||||||||||
Called by
saveHierarchyState(android.util.SparseArray) to store the state for
this view and its children.
| |||||||||||
Dispatch setPressed to all of this View's children.
| |||||||||||
Perform dispatching of a
restoreHierarchyState(android.util.SparseArray)
to only this view, not to its children. | |||||||||||
Dispatch a view visibility change down the view hierarchy.
| |||||||||||
Draw one child of this View Group.
| |||||||||||
This function is called whenever the state of the view changes in such
a way that it impacts the state of drawables being shown.
| |||||||||||
Called by the view hierarchy when the content insets for a window have
changed, to allow it to adjust its content to fit within those windows.
| |||||||||||
Returns a set of default layout parameters.
| |||||||||||
Returns a safe set of layout parameters based on the supplied layout params.
| |||||||||||
Returns the index of the child to draw for this iteration.
| |||||||||||
Sets
t to be the static transformation of the child, if set, returning a
boolean to indicate whether a static transform was set. | |||||||||||
Indicates whether the ViewGroup is drawing its children in the order defined by
getChildDrawingOrder(int, int) . | |||||||||||
Indicates whether the ViewGroup is currently drawing its children using
their drawing cache.
| |||||||||||
Ask one of the children of this view to measure itself, taking into
account both the MeasureSpec requirements for this view and its padding.
| |||||||||||
Ask one of the children of this view to measure itself, taking into
account both the MeasureSpec requirements for this view and its padding
and margins.
| |||||||||||
Ask all of the children of this view to measure themselves, taking into
account both the MeasureSpec requirements for this view and its padding.
| |||||||||||
Invoked by a parent ViewGroup to notify the end of the animation
currently associated with this view.
| |||||||||||
Invoked by a parent ViewGroup to notify the start of the animation
currently associated with this view.
| |||||||||||
This is called when the view is attached to a window.
| |||||||||||
Generate the new
Drawable state for
this view. | |||||||||||
This is called when the view is detached from a window.
| |||||||||||
Called from layout when this view should
assign a size and position to each of its children.
| |||||||||||
Look for a descendant to call
requestFocus() on. | |||||||||||
Finishes the removal of a detached view.
| |||||||||||
Enables or disables the drawing cache for each child of this view group.
| |||||||||||
Tells the ViewGroup whether to draw its children in the order defined by the method
getChildDrawingOrder(int, int) . | |||||||||||
Tells the ViewGroup to draw its children using their drawing cache.
| |||||||||||
When this property is set to true, this ViewGroup supports static transformations on
children; this causes
getChildStaticTransformation(View, android.view.animation.Transformation) to be
invoked when a child is drawn. |
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class
android.view.View
| |||||||||||
From class
java.lang.Object
| |||||||||||
From interface
android.graphics.drawable.Drawable.Callback
| |||||||||||
From interface
android.view.KeyEvent.Callback
| |||||||||||
From interface
android.view.ViewManager
| |||||||||||
From interface
android.view.ViewParent
| |||||||||||
From interface
android.view.accessibility.AccessibilityEventSource
|
Sets whether this ViewGroup's drawable states also include its children's drawable states. This is used, for example, to make a group appear to be focused when its child EditText or button is focused.
Must be a boolean value, either "true
" or "false
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol addStatesFromChildren
.
Defines whether the ViewGroup should always draw its children using their drawing cache or not. The default value is true.
Must be a boolean value, either "true
" or "false
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol alwaysDrawnWithCache
.
Defines whether changes in layout (caused by adding and removing items) should cause a LayoutTransition to run. When this flag is set to true, a default LayoutTransition object will be set on the ViewGroup container and default animations will run when these layout changes occur.
Must be a boolean value, either "true
" or "false
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol animateLayoutChanges
.
Defines whether layout animations should create a drawing cache for their children. Enabling the animation cache consumes more memory and requires a longer initialization but provides better performance. The animation cache is enabled by default.
Must be a boolean value, either "true
" or "false
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol animationCache
.
Defines whether a child is limited to draw inside of its bounds or not. This is useful with animations that scale the size of the children to more than 100% for instance. In such a case, this property should be set to false to allow the children to draw outside of their bounds. The default value of this property is true.
Must be a boolean value, either "true
" or "false
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol clipChildren
.
Defines whether the ViewGroup will clip its drawing surface so as to exclude the padding area. This property is set to true by default.
Must be a boolean value, either "true
" or "false
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol clipToPadding
.
Defines the relationship between the ViewGroup and its descendants when looking for a View to take focus.
Must be one of the following constant values.
Constant | Value | Description |
---|---|---|
beforeDescendants | 0 | The ViewGroup will get focus before any of its descendants. |
afterDescendants | 1 | The ViewGroup will get focus only if none of its descendants want it. |
blocksDescendants | 2 | The ViewGroup will block its descendants from receiving focus. |
This corresponds to the global attribute
resource symbol descendantFocusability
.
Defines the layout animation to use the first time the ViewGroup is laid out. Layout animations can also be started manually after the first layout.
Must be a reference to another resource, in the form "@[+][package:]type:name
"
or to a theme attribute in the form "?[package:][type:]name
".
This corresponds to the global attribute
resource symbol layoutAnimation
.
Defines the layout mode of this ViewGroup.
Must be one of the following constant values.
Constant | Value | Description |
---|---|---|
clipBounds | 0 | Use the children's clip bounds when laying out this container. |
opticalBounds | 1 | Use the children's optical bounds when laying out this container. |
This corresponds to the global attribute
resource symbol layoutMode
.
Defines the persistence of the drawing cache. The drawing cache might be enabled by a ViewGroup for all its children in specific situations (for instance during a scrolling.) This property lets you persist the cache in memory after its initial usage. Persisting the cache consumes more memory but may prevent frequent garbage collection is the cache is created over and over again. By default the persistence is set to scrolling.
Must be one or more (separated by '|') of the following constant values.
Constant | Value | Description |
---|---|---|
none | 0x0 | The drawing cache is not persisted after use. |
animation | 0x1 | The drawing cache is persisted after a layout animation. |
scrolling | 0x2 | The drawing cache is persisted after a scroll. |
all | 0x3 | The drawing cache is always persisted. |
This corresponds to the global attribute
resource symbol persistentDrawingCache
.
Sets whether this ViewGroup should split MotionEvents
to separate child views during touch event dispatch.
If false (default), touch events will be dispatched to
the child view where the first pointer went down until
the last pointer goes up.
If true, touch events may be dispatched to multiple children.
MotionEvents for each pointer will be dispatched to the child
view where the initial ACTION_DOWN event happened.
See setMotionEventSplittingEnabled(boolean)
for more information.
Must be a boolean value, either "true
" or "false
".
This may also be a reference to a resource (in the form
"@[package:]type:name
") or
theme attribute (in the form
"?[package:][type:]name
")
containing a value of this type.
This corresponds to the global attribute
resource symbol splitMotionEvents
.
We clip to padding when FLAG_CLIP_TO_PADDING and FLAG_PADDING_NOT_NULL are set at the same time.
This view will get focus only if none of its descendants want it.
This view will get focus before any of its descendants.
This view will block any of its descendants from getting focus, even if they are focusable.
This constant is a layoutMode
.
Clip bounds are the raw values of left
, top
,
right
and bottom
.
This constant is a layoutMode
.
Optical bounds describe where a widget appears to be. They sit inside the clip
bounds which need to cover a larger area to allow other effects,
such as shadows and glows, to be drawn.
Used to indicate that all drawing caches should be kept in memory.
Used to indicate that the animation drawing cache should be kept in memory.
Used to indicate that no drawing cache should be kept in memory.
Used to indicate that the scrolling drawing cache should be kept in memory.
Adds the children of a given View for accessibility. Since some Views are not important for accessibility the children for accessibility are not necessarily direct children of the view, rather they are the first level of descendants important for accessibility.
childrenForAccessibility | The list of children for accessibility. |
---|
Adds any focusable views that are descendants of this view (possibly including this view if it is focusable itself) to views. This method adds all focusable views regardless if we are in touch mode or only views focusable in touch mode if we are in touch mode or only views that can take accessibility focus if accessibility is enabeld depending on the focusable mode paramater.
views | Focusable views found so far or null if all we are interested is the number of focusables. |
---|---|
direction | The direction of the focus. |
focusableMode | The type of focusables to be added. |
Returns whether this ViewGroup's drawable states also include its children's drawable states. This is used, for example, to make a group appear to be focused when its child EditText or button is focused.
Add any touchable views that are descendants of this view (possibly including this view if it is touchable itself) to views.
views | Touchable views found so far |
---|
Adds a child view with the specified layout parameters.
Note: do not invoke this method from
draw(android.graphics.Canvas)
, onDraw(android.graphics.Canvas)
,
dispatchDraw(android.graphics.Canvas)
or any related method.
child | the child view to add |
---|---|
index | the position at which to add the child |
params | the layout parameters to set on the child |
Adds a child view with the specified layout parameters.
Note: do not invoke this method from
draw(android.graphics.Canvas)
, onDraw(android.graphics.Canvas)
,
dispatchDraw(android.graphics.Canvas)
or any related method.
child | the child view to add |
---|---|
params | the layout parameters to set on the child |
Adds a child view. If no layout parameters are already set on the child, the default parameters for this ViewGroup are set on the child.
Note: do not invoke this method from
draw(android.graphics.Canvas)
, onDraw(android.graphics.Canvas)
,
dispatchDraw(android.graphics.Canvas)
or any related method.
child | the child view to add |
---|---|
index | the position at which to add the child |
Adds a child view. If no layout parameters are already set on the child, the default parameters for this ViewGroup are set on the child.
Note: do not invoke this method from
draw(android.graphics.Canvas)
, onDraw(android.graphics.Canvas)
,
dispatchDraw(android.graphics.Canvas)
or any related method.
child | the child view to add |
---|
Adds a child view with this ViewGroup's default layout parameters and the specified width and height.
Note: do not invoke this method from
draw(android.graphics.Canvas)
, onDraw(android.graphics.Canvas)
,
dispatchDraw(android.graphics.Canvas)
or any related method.
child | the child view to add |
---|
Change the z order of the child so it's on top of all other children.
This ordering change may affect layout, if this container
uses an order-dependent layout scheme (e.g., LinearLayout). Prior
to KITKAT
this
method should be followed by calls to requestLayout()
and
invalidate()
on this parent to force the parent to redraw
with the new child ordering.
child | The child to bring to the top of the z order |
---|
If addStatesFromChildren()
is true, refreshes this group's
drawable state (to include the states from its children).
child | The child whose drawable state has changed. |
---|
Called when a child view has changed whether or not it is tracking transient state.
child | Child view whose state has changed |
---|---|
childHasTransientState | true if this child has transient state |
Called when a child of this parent is giving up focus
child | The view that is giving up focus |
---|
Removes any pending animations for views that have been removed. Call this if you don't want animations for exiting views to stack up.
Called when this view wants to give up focus. If focus is cleared
onFocusChanged(boolean, int, android.graphics.Rect)
is called.
Note: When a View clears focus the framework is trying to give focus to the first focusable View from the top. Hence, if this View is the first from the top that can take focus, then all callbacks related to clearing focus will be invoked after wich the framework will give focus to this view.
Dispatch a notification about a resource configuration change down the view hierarchy. ViewGroups should override to route to their children.
newConfig | The new resource configuration. |
---|
Dispatch a hint about whether this view is displayed. For instance, when a View moves out of the screen, it might receives a display hint indicating the view is not displayed. Applications should not rely on this hint as there is no guarantee that they will receive one.
Detects if this View is enabled and has a drag event listener.
If both are true, then it calls the drag event listener with the
DragEvent
it received. If the drag event listener returns
true
, then dispatchDragEvent() returns true
.
For all other cases, the method calls the
onDragEvent()
drag event handler
method and returns its result.
This ensures that a drag event is always consumed, even if the View does not have a drag event listener. However, if the View has a listener and the listener returns true, then onDragEvent() is not called.
Dispatch a key event to the next view on the focus path. This path runs from the top of the view tree down to the currently focused view. If this view has focus, it will dispatch to itself. Otherwise it will dispatch the next node down the focus path. This method also fires any key listeners.
event | The key event to be dispatched. |
---|
Dispatch a key event before it is processed by any input method associated with the view hierarchy. This can be used to intercept key events in special situations before the IME consumes them; a typical example would be handling the BACK key to update the application's UI instead of allowing the IME to see it and close itself.
event | The key event to be dispatched. |
---|
Dispatches a key shortcut event.
event | The key event to be dispatched. |
---|
Dispatch setActivated to all of this View's children.
activated | The new activated state |
---|
Dispatch setSelected to all of this View's children.
selected | The new selected state |
---|
Dispatch callbacks to setOnSystemUiVisibilityChangeListener(View.OnSystemUiVisibilityChangeListener)
down
the view hierarchy.
Pass the touch screen motion event down to the target view, or this view if it is the target.
ev | The motion event to be dispatched. |
---|
Pass a trackball motion event down to the focused view.
event | The motion event to be dispatched. |
---|
This method is the last chance for the focused view and its ancestors to respond to an arrow key. This is called when the focused view did not consume the key internally, nor could the view system find a new view in the requested direction to give focus to.
focused | The currently focused view. |
---|---|
direction | The direction focus wants to move. One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT. |
Called when the window containing this view gains or loses window focus. ViewGroups should override to route to their children.
hasFocus | True if the window containing this view now has focus, false otherwise. |
---|
Dispatch callbacks to onWindowSystemUiVisibilityChanged(int)
down
the view hierarchy.
Dispatch a window visibility change down the view hierarchy. ViewGroups should override to route to their children.
visibility | The new visibility of the window. |
---|
This method should always be called following an earlier call to
startViewTransition(View)
. The given View is finally removed from its parent
and will no longer be displayed. Note that this method does not perform the functionality
of removing a view from its parent; it just discontinues the display of a View that
has previously been removed.
startViewTransition(View)
.
Find the view in the hierarchy rooted at this view that currently has focus.
Finds the Views that contain given text. The containment is case insensitive.
The search is performed by either the text that the View renders or the content
description that describes the view for accessibility purposes and the view does
not render or both. Clients can specify how the search is to be performed via
passing the FIND_VIEWS_WITH_TEXT
and
FIND_VIEWS_WITH_CONTENT_DESCRIPTION
flags.
outViews | The output list of matching Views. |
---|---|
text | The text to match against. |
Find the nearest view in the specified direction that wants to take focus.
focused | The view that currently has focus |
---|---|
direction | One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT, or 0 for not applicable. |
Tells the parent that a new focusable view has become available. This is to handle transitions from the case where there are no focusable views to the case where the first focusable view appears.
v | The view that has become newly focusable |
---|
This is used by the RootView to perform an optimization when the view hierarchy contains one or several SurfaceView. SurfaceView is always considered transparent, but its children are not, therefore all View objects remove themselves from the global transparent region (passed as a parameter to this function).
region | The transparent region for this ViewAncestor (window). |
---|
Returns a new set of layout parameters based on the supplied attributes set.
attrs | the attributes to build the layout parameters from |
---|
ViewGroup.LayoutParams
or one
of its descendants
Returns the view at the specified position in the group.
index | the position at which to get the view from |
---|
Returns the number of children in the group.
Does the hard part of measureChildren: figuring out the MeasureSpec to pass to a particular child. This method figures out the right MeasureSpec for one dimension (height or width) of one child view. The goal is to combine information from our MeasureSpec with the LayoutParams of the child to get the best possible results. For example, if the this view knows its size (because its MeasureSpec has a mode of EXACTLY), and the child has indicated in its LayoutParams that it wants to be the same size as the parent, the parent should ask the child to layout given an exact size.
spec | The requirements for this view |
---|---|
padding | The padding of this view for the current dimension and margins, if applicable |
childDimension | How big the child wants to be in the current dimension |
Compute the visible part of a rectangular region defined in terms of a child view's coordinates.
Returns the clipped visible part of the rectangle r
, defined in the
child
's local coordinate system. r
is modified by this method to
contain the result, expressed in the global (root) coordinate system.
The resulting rectangle is always axis aligned. If a rotation is applied to a node in the View hierarchy, the result is the axis-aligned bounding box of the visible rectangle.
child | A child View, whose rectangular visible region we want to compute |
---|---|
r | The input rectangle, defined in the child coordinate system. Will be overwritten to contain the resulting visible rectangle, expressed in global (root) coordinates |
offset | The input coordinates of a point, defined in the child coordinate system.
As with the r parameter, this will be overwritten to contain the global (root)
coordinates of that point.
A null value is valid (in case you are not interested in this result) |
Returns whether ths group's children are clipped to their bounds before drawing. The default value is true.
Gets the descendant focusability of this view group. The descendant
focusability defines the relationship between this view group and its
descendants when looking for a view to take focus in
requestFocus(int, android.graphics.Rect)
.
Returns the focused child of this view, if any. The child may have focus or contain focus.
Returns the layout animation controller used to animate the group's children.
Returns the animation listener to which layout animation events are sent.
Returns the basis of alignment during layout operations on this ViewGroup:
either LAYOUT_MODE_CLIP_BOUNDS
or LAYOUT_MODE_OPTICAL_BOUNDS
.
If no layoutMode was explicitly set, either programmatically or in an XML resource,
the method returns the layoutMode of the view's parent ViewGroup if such a parent exists,
otherwise the method returns a default value of LAYOUT_MODE_CLIP_BOUNDS
.
Gets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is not null, changes in layout which occur because of children being added to or removed from the ViewGroup will be animated according to the animations defined in that LayoutTransition object. By default, the transition object is null (so layout changes are not animated).
null
means no transition will run on layout changes.
Returns the ViewGroupOverlay for this view group, creating it if it does
not yet exist. In addition to ViewOverlay
's support for drawables,
ViewGroupOverlay
allows views to be added to the overlay. These
views, like overlay drawables, are visual-only; they do not receive input
events and should not be used as anything other than a temporary
representation of a view in a parent container, such as might be used
by an animation effect.
Note: Overlays do not currently work correctly with SurfaceView
or TextureView
; contents in overlays for these
types of views may not display correctly.
Returns an integer indicating what types of drawing caches are kept in memory.
PERSISTENT_NO_CACHE
,
PERSISTENT_ANIMATION_CACHE
, PERSISTENT_SCROLLING_CACHE
and PERSISTENT_ALL_CACHES
Returns true if this view has or contains focus
Returns true if this view is focusable or if it contains a reachable View
for which hasFocusable()
returns true. A "reachable hasFocusable()"
is a View whose parents do not block descendants focus.
Only VISIBLE
views are considered focusable.
Indicates whether the view is currently tracking transient state that the app should not need to concern itself with saving and restoring, but that the framework should take special note to preserve when possible.
A view with transient state cannot be trivially rebound from an external data source, such as an adapter binding item views in a list. This may be because the view is performing an animation, tracking user selection of content, or similar.
Returns the position in the group of the specified child view.
child | the view for which to get the position |
---|
Don't call or override this method. It is used for the implementation of the view hierarchy.
child | The child which is dirty |
---|---|
dirty | The area within the child that is invalid |
Don't call or override this method. It is used for the implementation of the view hierarchy. This implementation returns null if this ViewGroup does not have a parent, if this ViewGroup is already fully invalidated or if the dirty rectangle does not intersect with this ViewGroup's bounds.
location | An array of 2 ints containing the left and top coordinates of the child to invalidate |
---|---|
dirty | The area within the child that is invalid |
Indicates whether this ViewGroup will always try to draw its children using their drawing cache. By default this property is enabled.
Indicates whether the children's drawing cache is used during a layout animation. By default, the drawing cache is enabled but this will prevent nested layout animations from working. To nest animations, you must disable the cache.
Returns true if MotionEvents dispatched to this ViewGroup can be split to multiple children.
Call Drawable.jumpToCurrentState()
on all Drawable objects associated with this view.
Assign a size and position to a view and all of its descendants
This is the second phase of the layout mechanism. (The first is measuring). In this phase, each parent calls layout on all of its children to position them. This is typically done using the child measurements that were stored in the measure pass().
Derived classes should not override this method. Derived classes with children should override onLayout. In that method, they should call layout on each of their children.
l | Left position, relative to parent |
---|---|
t | Top position, relative to parent |
r | Right position, relative to parent |
b | Bottom position, relative to parent |
Notifies a view parent that the accessibility state of one of its descendants has changed and that the structure of the subtree is different.
child | The direct child whose subtree has changed. |
---|---|
source | The descendant view that changed. |
changeType | A bit mask of the types of changes that occurred. One or more of: |
Offset a rectangle that is in a descendant's coordinate space into our coordinate space.
descendant | A descendant of this view |
---|---|
rect | A rectangle defined in descendant's coordinate space. |
Offset a rectangle that is in our coordinate space into an ancestor's coordinate space.
descendant | A descendant of this view |
---|---|
rect | A rectangle defined in descendant's coordinate space. |
Implement this method to intercept hover events before they are handled by child views.
This method is called before dispatching a hover event to a child of
the view group or to the view group's own onHoverEvent(MotionEvent)
to allow
the view group a chance to intercept the hover event.
This method can also be used to watch all pointer motions that occur within
the bounds of the view group even when the pointer is hovering over
a child of the view group rather than over the view group itself.
The view group can prevent its children from receiving hover events by
implementing this method and returning true
to indicate
that it would like to intercept hover events. The view group must
continuously return true
from onInterceptHoverEvent(MotionEvent)
for as long as it wishes to continue intercepting hover events from
its children.
Interception preserves the invariant that at most one view can be hovered at a time by transferring hover focus from the currently hovered child to the view group or vice-versa as needed.
If this method returns true
and a child is already hovered, then the
child view will first receive a hover exit event and then the view group
itself will receive a hover enter event in onHoverEvent(MotionEvent)
.
Likewise, if this method had previously returned true
to intercept hover
events and instead returns false
while the pointer is hovering
within the bounds of one of a child, then the view group will first receive a
hover exit event in onHoverEvent(MotionEvent)
and then the hovered child will
receive a hover enter event.
The default implementation always returns false.
event | The motion event that describes the hover. |
---|
Implement this method to intercept all touch screen motion events. This allows you to watch events as they are dispatched to your children, and take ownership of the current gesture at any point.
Using this function takes some care, as it has a fairly complicated
interaction with View.onTouchEvent(MotionEvent)
, and using it requires implementing
that method as well as this one in the correct way. Events will be
received in the following order:
ACTION_CANCEL
, and all further
events will be delivered to your onTouchEvent() method and no longer
appear here.
ev | The motion event being dispatched down the hierarchy. |
---|
Called when a child has requested sending an AccessibilityEvent
and
gives an opportunity to its parent to augment the event.
If an View.AccessibilityDelegate
has been specified via calling
setAccessibilityDelegate(android.view.View.AccessibilityDelegate)
its
onRequestSendAccessibilityEvent(ViewGroup, View, AccessibilityEvent)
is responsible for handling this call.
child | The child which requests sending the event. |
---|---|
event | The event to be sent. |
Tell view hierarchy that the global view attributes need to be re-evaluated.
child | View whose attributes have changed. |
---|
Call this method to remove all child views from the ViewGroup.
Note: do not invoke this method from
draw(android.graphics.Canvas)
, onDraw(android.graphics.Canvas)
,
dispatchDraw(android.graphics.Canvas)
or any related method.
Called by a ViewGroup subclass to remove child views from itself, when it must first know its size on screen before it can calculate how many child views it will render. An example is a Gallery or a ListView, which may "have" 50 children, but actually only render the number of children that can currently fit inside the object on screen. Do not call this method unless you are extending ViewGroup and understand the view measuring and layout pipeline.
Note: do not invoke this method from
draw(android.graphics.Canvas)
, onDraw(android.graphics.Canvas)
,
dispatchDraw(android.graphics.Canvas)
or any related method.
Note: do not invoke this method from
draw(android.graphics.Canvas)
, onDraw(android.graphics.Canvas)
,
dispatchDraw(android.graphics.Canvas)
or any related method.
Removes the view at the specified position in the group.
Note: do not invoke this method from
draw(android.graphics.Canvas)
, onDraw(android.graphics.Canvas)
,
dispatchDraw(android.graphics.Canvas)
or any related method.
index | the position in the group of the view to remove |
---|
Removes a view during layout. This is useful if in your onLayout() method, you need to remove more views.
Note: do not invoke this method from
draw(android.graphics.Canvas)
, onDraw(android.graphics.Canvas)
,
dispatchDraw(android.graphics.Canvas)
or any related method.
view | the view to remove from the group |
---|
Removes the specified range of views from the group.
Note: do not invoke this method from
draw(android.graphics.Canvas)
, onDraw(android.graphics.Canvas)
,
dispatchDraw(android.graphics.Canvas)
or any related method.
start | the first position in the group of the range of views to remove |
---|---|
count | the number of views to remove |
Removes a range of views during layout. This is useful if in your onLayout() method, you need to remove more views.
Note: do not invoke this method from
draw(android.graphics.Canvas)
, onDraw(android.graphics.Canvas)
,
dispatchDraw(android.graphics.Canvas)
or any related method.
start | the index of the first view to remove from the group |
---|---|
count | the number of views to remove from the group |
Called when a child of this parent wants focus
child | The child of this ViewParent that wants focus. This view will contain the focused view. It is not necessarily the view that actually has focus. |
---|---|
focused | The view that is a descendant of child that actually has focus |
Called when a child of this group wants a particular rectangle to be
positioned onto the screen. ViewGroup
s overriding this can trust
that:
ViewGroup
s overriding this should uphold the contract:
child | The direct child making the request. |
---|---|
rectangle | The rectangle in the child's coordinates the child wishes to be on the screen. |
immediate | True to forbid animated or delayed scrolling, false otherwise |
Called when a child does not want this parent and its ancestors to
intercept touch events with
onInterceptTouchEvent(MotionEvent)
.
This parent should pass this call onto its parents. This parent must obey this request for the duration of the touch (that is, only clear the flag after this parent has received an up or a cancel.
disallowIntercept | True if the child does not want the parent to intercept touch events. |
---|
Call this to try to give focus to a specific view or to one of its descendants
and give it hints about the direction and a specific rectangle that the focus
is coming from. The rectangle can help give larger views a finer grained hint
about where focus is coming from, and therefore, where to show selection, or
forward focus change internally.
A view will not actually take focus if it is not focusable (isFocusable()
returns
false), or if it is focusable and it is not focusable in touch mode
(isFocusableInTouchMode()
) while the device is in touch mode.
A View will not take focus if it is not visible.
A View will not take focus if one of its parents has
getDescendantFocusability()
equal to
FOCUS_BLOCK_DESCENDANTS
.
See also focusSearch(int)
, which is what you call to say that you
have focus, and you want your parent to look for the next one.
You may wish to override this method if your custom View
has an internal
View
that it wishes to forward the request to.
Looks for a view to give focus to respecting the setting specified by
getDescendantFocusability()
.
Uses onRequestFocusInDescendants(int, android.graphics.Rect)
to
find focus within the children of this group when appropriate.
direction | One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT |
---|---|
previouslyFocusedRect | The rectangle (in this View's coordinate system) to give a finer grained hint about where focus is coming from. May be null if there is no hint. |
Called by a child to request from its parent to send an AccessibilityEvent
.
The child has already populated a record for itself in the event and is delegating
to its parent to send the event. The parent can optionally add a record for itself.
Note: An accessibility event is fired by an individual view which populates the event with a record for its state and requests from its parent to perform the sending. The parent can optionally add a record for itself before dispatching the request to its parent. A parent can also choose not to respect the request for sending the event. The accessibility event is sent by the topmost view in the view tree.
child | The child which requests sending the event. |
---|---|
event | The event to be sent. |
Called when a child wants the view hierarchy to gather and report transparent regions to the window compositor. Views that "punch" holes in the view hierarchy, such as SurfaceView can use this API to improve performance of the system. When no such a view is present in the hierarchy, this optimization in unnecessary and might slightly reduce the view hierarchy performance.
child | the view requesting the transparent region computation |
---|
Schedules the layout animation to be played after the next layout pass of this view group. This can be used to restart the layout animation when the content of the view group changes or when the activity is paused and resumed.
Sets whether this ViewGroup's drawable states also include its children's drawable states. This is used, for example, to make a group appear to be focused when its child EditText or button is focused.
Indicates whether this ViewGroup will always try to draw its children using their drawing cache. This property can be set to true when the cache rendering is slightly different from the children's normal rendering. Renderings can be different, for instance, when the cache's quality is set to low. When this property is disabled, the ViewGroup will use the drawing cache of its children only when asked to. It's usually the task of subclasses to tell ViewGroup when to start using the drawing cache and when to stop using it.
always | true to always draw with the drawing cache, false otherwise |
---|
Enables or disables the children's drawing cache during a layout animation. By default, the drawing cache is enabled but this will prevent nested layout animations from working. To nest animations, you must disable the cache.
enabled | true to enable the animation cache, false otherwise |
---|
By default, children are clipped to their bounds before drawing. This allows view groups to override this behavior for animations, etc.
clipChildren | true to clip children to their bounds, false otherwise |
---|
By default, children are clipped to the padding of the ViewGroup. This allows view groups to override this behavior
clipToPadding | true to clip children to the padding of the group, false otherwise |
---|
Set the descendant focusability of this view group. This defines the relationship
between this view group and its descendants when looking for a view to
take focus in requestFocus(int, android.graphics.Rect)
.
focusability | one of FOCUS_BEFORE_DESCENDANTS , FOCUS_AFTER_DESCENDANTS ,
FOCUS_BLOCK_DESCENDANTS .
|
---|
Sets the layout animation controller used to animate the group's children after the first layout.
controller | the animation controller |
---|
Specifies the animation listener to which layout animation events must
be sent. Only
onAnimationStart(Animation)
and
onAnimationEnd(Animation)
are invoked.
animationListener | the layout animation listener |
---|
Sets the basis of alignment during the layout of this ViewGroup.
Valid values are either LAYOUT_MODE_CLIP_BOUNDS
or
LAYOUT_MODE_OPTICAL_BOUNDS
.
layoutMode | the layout mode to use during layout operations |
---|
Sets the LayoutTransition object for this ViewGroup. If the LayoutTransition object is not null, changes in layout which occur because of children being added to or removed from the ViewGroup will be animated according to the animations defined in that LayoutTransition object. By default, the transition object is null (so layout changes are not animated).
Replacing a non-null transition will cause that previous transition to be canceled, if it is currently running, to restore this container to its correct post-transition state.
transition | The LayoutTransition object that will animated changes in layout. A value
of null means no transition will run on layout changes. |
---|
Enable or disable the splitting of MotionEvents to multiple children during touch event
dispatch. This behavior is enabled by default for applications that target an
SDK version of HONEYCOMB
or newer.
When this option is enabled MotionEvents may be split and dispatched to different child views depending on where each pointer initially went down. This allows for user interactions such as scrolling two panes of content independently, chording of buttons, and performing independent gestures on different pieces of content.
split | true to allow MotionEvents to be split and dispatched to multiple
child views. false to only allow one child view to be the target of
any MotionEvent received by this ViewGroup. |
---|
Register a callback to be invoked when a child is added to or removed from this view.
listener | the callback to invoke on hierarchy change |
---|
Indicates what types of drawing caches should be kept in memory after they have been created.
drawingCacheToKeep | one or a combination of PERSISTENT_NO_CACHE ,
PERSISTENT_ANIMATION_CACHE , PERSISTENT_SCROLLING_CACHE
and PERSISTENT_ALL_CACHES
|
---|
Return true if the pressed state should be delayed for children or descendants of this ViewGroup. Generally, this should be done for containers that can scroll, such as a List. This prevents the pressed state from appearing when the user is actually trying to scroll the content. The default implementation returns true for compatibility reasons. Subclasses that do not scroll should generally override this method and return false.
Bring up a context menu for the specified view or its ancestors.
In most cases, a subclass does not need to override this. However, if
the subclass is added directly to the window manager (for example,
addView(View, android.view.ViewGroup.LayoutParams)
)
then it should override this and show the context menu.
originalView | The source view where the context menu was first invoked |
---|
Start an action mode for the specified view.
In most cases, a subclass does not need to override this. However, if the
subclass is added directly to the window manager (for example,
addView(View, android.view.ViewGroup.LayoutParams)
)
then it should override this and start the action mode.
originalView | The source view where the action mode was first invoked |
---|---|
callback | The callback that will handle lifecycle events for the action mode |
Runs the layout animation. Calling this method triggers a relayout of this view group.
This method tells the ViewGroup that the given View object, which should have this
ViewGroup as its parent,
should be kept around (re-displayed when the ViewGroup draws its children) even if it
is removed from its parent. This allows animations, such as those used by
Fragment
and LayoutTransition
to animate
the removal of views. A call to this method should always be accompanied by a later call
to endViewTransition(View)
, such as after an animation on the View has finished,
so that the View finally gets removed.
view | The View object to be kept visible even if it gets removed from its parent. |
---|
Adds a view during layout. This is useful if in your onLayout() method, you need to add more views (as does the list view for example). If index is negative, it means put it at the end of the list.
child | the view to add to the group |
---|---|
index | the index at which the child must be added |
params | the layout parameters to associate with the child |
preventRequestLayout | if true, calling this method will not trigger a layout request on child |
Adds a view during layout. This is useful if in your onLayout() method, you need to add more views (as does the list view for example). If index is negative, it means put it at the end of the list.
child | the view to add to the group |
---|---|
index | the index at which the child must be added |
params | the layout parameters to associate with the child |
Subclasses should override this method to set layout animation parameters on the supplied child.
child | the child to associate with animation parameters |
---|---|
params | the child's layout parameters which hold the animation parameters |
index | the index of the child in the view group |
count | the number of children in the view group |
Attaches a view to this view group. Attaching a view assigns this group as the parent,
sets the layout parameters and puts the view in the list of children so that
it can be retrieved by calling getChildAt(int)
.
This method is intended to be lightweight and makes no assumptions about whether the
parent or child should be redrawn. Proper use of this method will include also making
any appropriate requestLayout()
or invalidate()
calls.
For example, callers can post
a Runnable
which performs a requestLayout()
on the next frame, after all detach/attach
calls are finished, causing layout to be run prior to redrawing the view hierarchy.
This method should be called only for views which were detached from their parent.
child | the child to attach |
---|---|
index | the index at which the child should be attached |
params | the layout parameters of the child |
Indicates whether the view group has the ability to animate its children after the first layout.
Prevents the specified child to be laid out during the next layout pass.
child | the child on which to perform the cleanup |
---|
Prints information about this view in the log output, with the tag
VIEW_LOG_TAG
. Each line in the output is preceded with an
indentation defined by the depth
.
depth | the indentation level |
---|
Detaches all views from the parent. Detaching a view should be followed
either by a call to
attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
or a call to removeDetachedView(View, boolean)
. Detachment should only be
temporary; reattachment or removal should happen within the same drawing cycle as
detachment. When a view is detached, its parent is null and cannot be retrieved by a
call to getChildAt(int)
.
Detaches a view from its parent. Detaching a view should be followed
either by a call to
attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
or a call to removeDetachedView(View, boolean)
. Detachment should only be
temporary; reattachment or removal should happen within the same drawing cycle as
detachment. When a view is detached, its parent is null and cannot be retrieved by a
call to getChildAt(int)
.
child | the child to detach |
---|
Detaches a view from its parent. Detaching a view should be followed
either by a call to
attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
or a call to removeDetachedView(View, boolean)
. Detachment should only be
temporary; reattachment or removal should happen within the same drawing cycle as
detachment. When a view is detached, its parent is null and cannot be retrieved by a
call to getChildAt(int)
.
index | the index of the child to detach |
---|
Detaches a range of views from their parents. Detaching a view should be followed
either by a call to
attachViewToParent(View, int, android.view.ViewGroup.LayoutParams)
or a call to removeDetachedView(View, boolean)
. Detachment should only be
temporary; reattachment or removal should happen within the same drawing cycle as
detachment. When a view is detached, its parent is null and cannot be retrieved by a
call to getChildAt(int)
.
start | the first index of the childrend range to detach |
---|---|
count | the number of children to detach |
Called by draw to draw the child views. This may be overridden by derived classes to gain control just before its children are drawn (but after its own view has been drawn).
canvas | the canvas on which to draw the view |
---|
Perform dispatching of a saveHierarchyState(android.util.SparseArray)
freeze()}
to only this view, not to its children. For use when overriding
dispatchSaveInstanceState(android.util.SparseArray)
dispatchFreeze()} to allow
subclasses to freeze their own state but not the state of their children.
container | the container |
---|
Dispatch a generic motion event to the currently focused view.
Do not call this method directly.
Call dispatchGenericMotionEvent(MotionEvent)
instead.
event | The motion event to be dispatched. |
---|
Dispatch a generic motion event to the view under the first pointer.
Do not call this method directly.
Call dispatchGenericMotionEvent(MotionEvent)
instead.
event | The motion event to be dispatched. |
---|
Dispatch a hover event.
Do not call this method directly.
Call dispatchGenericMotionEvent(MotionEvent)
instead.
event | The motion event to be dispatched. |
---|
Called by restoreHierarchyState(android.util.SparseArray)
to retrieve the
state for this view and its children. May be overridden to modify how restoring
happens to a view's children; for example, some views may want to not store state
for their children.
container | The SparseArray which holds previously saved state. |
---|
Called by saveHierarchyState(android.util.SparseArray)
to store the state for
this view and its children. May be overridden to modify how freezing happens to a
view's children; for example, some views may want to not store state for their children.
container | The SparseArray in which to save the view's state. |
---|
Dispatch setPressed to all of this View's children.
pressed | The new pressed state |
---|
Perform dispatching of a restoreHierarchyState(android.util.SparseArray)
to only this view, not to its children. For use when overriding
dispatchRestoreInstanceState(android.util.SparseArray)
to allow
subclasses to thaw their own state but not the state of their children.
container | the container |
---|
Draw one child of this View Group. This method is responsible for getting the canvas in the right state. This includes clipping, translating so that the child's scrolled origin is at 0, 0, and applying any animation transformations.
canvas | The canvas on which to draw the child |
---|---|
child | Who to draw |
drawingTime | The time at which draw is occurring |
This function is called whenever the state of the view changes in such a way that it impacts the state of drawables being shown.
Be sure to call through to the superclass when overriding this function.
Called by the view hierarchy when the content insets for a window have changed, to allow it to adjust its content to fit within those windows. The content insets tell you the space that the status bar, input method, and other system windows infringe on the application's window.
You do not normally need to deal with this function, since the default
window decoration given to applications takes care of applying it to the
content of the window. If you use SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
or SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
this will not be the case,
and your content can be placed under those system elements. You can then
use this method within your view hierarchy if you have parts of your UI
which you would like to ensure are not being covered.
The default implementation of this method simply applies the content
insets to the view's padding, consuming that content (modifying the
insets to be 0), and returning true. This behavior is off by default, but can
be enabled through setFitsSystemWindows(boolean)
.
This function's traversal down the hierarchy is depth-first. The same content insets object is propagated down the hierarchy, so any changes made to it will be seen by all following views (including potentially ones above in the hierarchy since this is a depth-first traversal). The first view that returns true will abort the entire traversal.
The default implementation works well for a situation where it is used with a container that covers the entire window, allowing it to apply the appropriate insets to its content on all edges. If you need a more complicated layout (such as two different views fitting system windows, one on the top of the window, and one on the bottom), you can override the method and handle the insets however you would like. Note that the insets provided by the framework are always relative to the far edges of the window, not accounting for the location of the called view within that window. (In fact when this method is called you do not yet know where the layout will place the view, as it is done before layout happens.)
Note: unlike many View methods, there is no dispatch phase to this call. If you are overriding it in a ViewGroup and want to allow the call to continue to your children, you must be sure to call the super implementation.
Here is a sample layout that makes use of fitting system windows
to have controls for a video view placed inside of the window decorations
that it hides and shows. This can be used with code like the second
sample (video player) shown in setSystemUiVisibility(int)
.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <view class="com.example.android.apis.view.VideoPlayerActivity$Content" android:id="@+id/content" android:src="@drawable/frantic" android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="center" /> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" android:animateLayoutChanges="true" > <TextView android:id="@+id/title" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top|center_horizontal" android:textColor="#ffffffff" android:background="#a0000000" android:textAppearance="?android:attr/textAppearanceLarge" android:gravity="left" android:padding="16dp" android:text="A title goes here" /> <Button android:id="@+id/play" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:textSize="28dp" /> <SeekBar android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal" android:layout_marginBottom="16dp" /> </FrameLayout> </FrameLayout>
insets | Current content insets of the window. Prior to
JELLY_BEAN you must not modify
the insets or else you and Android will be unhappy. |
---|
true
if this view applied the insets and it should not
continue propagating further down the hierarchy, false
otherwise.Returns a set of default layout parameters. These parameters are requested
when the View passed to addView(View)
has no layout parameters
already set. If null is returned, an exception is thrown from addView.
Returns a safe set of layout parameters based on the supplied layout params.
When a ViewGroup is passed a View whose layout params do not pass the test of
checkLayoutParams(android.view.ViewGroup.LayoutParams)
, this method
is invoked. This method should return a new set of layout params suitable for
this ViewGroup, possibly by copying the appropriate attributes from the
specified set of layout params.
p | The layout parameters to convert into a suitable set of layout parameters for this ViewGroup. |
---|
ViewGroup.LayoutParams
or one
of its descendants
Returns the index of the child to draw for this iteration. Override this if you want to change the drawing order of children. By default, it returns i.
NOTE: In order for this method to be called, you must enable child ordering
first by calling setChildrenDrawingOrderEnabled(boolean)
.
i | The current iteration. |
---|
Sets t
to be the static transformation of the child, if set, returning a
boolean to indicate whether a static transform was set. The default implementation
simply returns false
; subclasses may override this method for different
behavior. setStaticTransformationsEnabled(boolean)
must be set to true
for this method to be called.
child | The child view whose static transform is being requested |
---|---|
t | The Transformation which will hold the result |
Indicates whether the ViewGroup is drawing its children in the order defined by
getChildDrawingOrder(int, int)
.
getChildDrawingOrder(int, int)
,
false otherwiseIndicates whether the ViewGroup is currently drawing its children using their drawing cache.
Ask one of the children of this view to measure itself, taking into account both the MeasureSpec requirements for this view and its padding. The heavy lifting is done in getChildMeasureSpec.
child | The child to measure |
---|---|
parentWidthMeasureSpec | The width requirements for this view |
parentHeightMeasureSpec | The height requirements for this view |
Ask one of the children of this view to measure itself, taking into account both the MeasureSpec requirements for this view and its padding and margins. The child must have MarginLayoutParams The heavy lifting is done in getChildMeasureSpec.
child | The child to measure |
---|---|
parentWidthMeasureSpec | The width requirements for this view |
widthUsed | Extra space that has been used up by the parent horizontally (possibly by other children of the parent) |
parentHeightMeasureSpec | The height requirements for this view |
heightUsed | Extra space that has been used up by the parent vertically (possibly by other children of the parent) |
Ask all of the children of this view to measure themselves, taking into account both the MeasureSpec requirements for this view and its padding. We skip children that are in the GONE state The heavy lifting is done in getChildMeasureSpec.
widthMeasureSpec | The width requirements for this view |
---|---|
heightMeasureSpec | The height requirements for this view |
Invoked by a parent ViewGroup to notify the end of the animation currently associated with this view. If you override this method, always call super.onAnimationEnd();
Invoked by a parent ViewGroup to notify the start of the animation currently associated with this view. If you override this method, always call super.onAnimationStart();
This is called when the view is attached to a window. At this point it
has a Surface and will start drawing. Note that this function is
guaranteed to be called before onDraw(android.graphics.Canvas)
,
however it may be called any time before the first onDraw -- including
before or after onMeasure(int, int)
.
Generate the new Drawable
state for
this view. This is called by the view
system when the cached Drawable state is determined to be invalid. To
retrieve the current state, you should use getDrawableState()
.
extraSpace | if non-zero, this is the number of extra entries you would like in the returned array in which you can place your own states. |
---|
Drawable
state of
the view.This is called when the view is detached from a window. At this point it no longer has a surface for drawing.
Called from layout when this view should assign a size and position to each of its children. Derived classes with children should override this method and call layout on each of their children.
changed | This is a new size or position for this view |
---|---|
l | Left position, relative to parent |
t | Top position, relative to parent |
r | Right position, relative to parent |
b | Bottom position, relative to parent |
Look for a descendant to call requestFocus()
on.
Called by requestFocus(int, android.graphics.Rect)
when it wants to request focus within its children. Override this to
customize how your ViewGroup
requests focus within its children.
direction | One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT |
---|---|
previouslyFocusedRect | The rectangle (in this View's coordinate system) to give a finer grained hint about where focus is coming from. May be null if there is no hint. |
Finishes the removal of a detached view. This method will dispatch the detached from window event and notify the hierarchy change listener.
This method is intended to be lightweight and makes no assumptions about whether the
parent or child should be redrawn. Proper use of this method will include also making
any appropriate requestLayout()
or invalidate()
calls.
For example, callers can post
a Runnable
which performs a requestLayout()
on the next frame, after all detach/remove
calls are finished, causing layout to be run prior to redrawing the view hierarchy.
child | the child to be definitely removed from the view hierarchy |
---|---|
animate | if true and the view has an animation, the view is placed in the disappearing views list, otherwise, it is detached from the window |
Enables or disables the drawing cache for each child of this view group.
enabled | true to enable the cache, false to dispose of it |
---|
Tells the ViewGroup whether to draw its children in the order defined by the method
getChildDrawingOrder(int, int)
.
enabled | true if the order of the children when drawing is determined by
getChildDrawingOrder(int, int) , false otherwise |
---|
Tells the ViewGroup to draw its children using their drawing cache. This property
is ignored when isAlwaysDrawnWithCacheEnabled()
is true. A child's drawing cache
will be used only if it has been enabled.
Subclasses should call this method to start and stop using the drawing cache when
they perform performance sensitive operations, like scrolling or animating.
enabled | true if children should be drawn with their cache, false otherwise |
---|
When this property is set to true, this ViewGroup supports static transformations on
children; this causes
getChildStaticTransformation(View, android.view.animation.Transformation)
to be
invoked when a child is drawn.
Any subclass overriding
getChildStaticTransformation(View, android.view.animation.Transformation)
should
set this property to true.
enabled | True to enable static transformations on children, false otherwise. |
---|