来源:https://developer.android.com/reference/android/widget/ScrollView
ScrollView
public class ScrollView
extends FrameLayout
java.lang.Object | ||||
↳ | android.view.View | |||
↳ | android.view.ViewGroup | |||
↳ | android.widget.FrameLayout | |||
↳ | android.widget.ScrollView |
A view group that allows the view hierarchy placed within it to be scrolled. Scroll view may have only one direct child placed within it. To add multiple views within the scroll view, make the direct child you add a view group, for example LinearLayout
, and place additional views within that LinearLayout.
Scroll view supports vertical scrolling only. For horizontal scrolling, use HorizontalScrollView
instead.
Never add a RecyclerView
or ListView
to a scroll view. Doing so results in poor user interface performance and a poor user experience.
For vertical scrolling, consider NestedScrollView
instead of scroll view which offers greater user interface flexibility and support for the material design scrolling patterns.
To learn more about material design patterns for handling scrolling, see Scrolling techniques.
Summary
XML attributes |
|
---|---|
android:fillViewport |
Defines whether the scrollview should stretch its content to fill the viewport. |
Inherited XML attributes |
|
---|---|
From class |
|
From class |
|
From class |
Inherited constants |
---|
From class |
From class |
Inherited fields |
---|
From class |
Public constructors |
|
---|---|
ScrollView(Context context) |
|
ScrollView(Context context, AttributeSet attrs) |
|
ScrollView(Context context, AttributeSet attrs, int defStyleAttr) |
|
ScrollView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) |
Public methods |
|
---|---|
void |
addView(View child, int index) Adds a child view. |
void |
addView(View child, ViewGroup.LayoutParams params) Adds a child view with the specified layout parameters. |
void |
addView(View child, int index, ViewGroup.LayoutParams params) Adds a child view with the specified layout parameters. |
void |
addView(View child) Adds a child view. |
boolean |
arrowScroll(int direction) Handle scrolling in response to an up or down arrow click. |
void |
computeScroll() Called by a parent to request that a child update its values for mScrollX and mScrollY if necessary. |
boolean |
dispatchKeyEvent(KeyEvent event) Dispatch a key event to the next view on the focus path. |
void |
draw(Canvas canvas) Manually render this view (and all of its children) to the given Canvas. |
boolean |
executeKeyEvent(KeyEvent event) You can call this function yourself to have the scroll view perform scrolling from a key event, just as if the event had been dispatched to it by the view hierarchy. |
void |
fling(int velocityY) Fling the scroll view |
boolean |
fullScroll(int direction) Handles scrolling in response to a "home/end" shortcut press. |
CharSequence |
getAccessibilityClassName() Return the class name of this object to be used for accessibility purposes. |
int |
getMaxScrollAmount() |
boolean |
isFillViewport() Indicates whether this ScrollView's content is stretched to fill the viewport. |
boolean |
isSmoothScrollingEnabled() |
boolean |
onGenericMotionEvent(MotionEvent event) Implement this method to handle generic motion events. |
boolean |
onInterceptTouchEvent(MotionEvent ev) Implement this method to intercept all touch screen motion events. |
boolean |
onNestedFling(View target, float velocityX, float velocityY, boolean consumed) Request a fling from a nested scroll. |
void |
onNestedScroll(View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) React to a nested scroll in progress. |
void |
onNestedScrollAccepted(View child, View target, int axes) React to the successful claiming of a nested scroll operation. |
boolean |
onStartNestedScroll(View child, View target, int nestedScrollAxes) React to a descendant view initiating a nestable scroll operation, claiming the nested scroll operation if appropriate. |
void |
onStopNestedScroll(View target) React to a nested scroll operation ending. |
boolean |
onTouchEvent(MotionEvent ev) Implement this method to handle touch screen motion events. |
boolean |
pageScroll(int direction) Handles scrolling in response to a "page up/down" shortcut press. |
void |
requestChildFocus(View child, View focused) Called when a child of this parent wants focus |
boolean |
requestChildRectangleOnScreen(View child, Rect rectangle, boolean immediate) Called when a child of this group wants a particular rectangle to be positioned onto the screen. |
void |
requestDisallowInterceptTouchEvent(boolean disallowIntercept) Called when a child does not want this parent and its ancestors to intercept touch events with |
void |
requestLayout() Call this when something has changed which has invalidated the layout of this view. |
void |
scrollTo(int x, int y) Set the scrolled position of your view. This version also clamps the scrolling to the bounds of our child. |
void |
setFillViewport(boolean fillViewport) Indicates this ScrollView whether it should stretch its content height to fill the viewport or not. |
void |
setOverScrollMode(int mode) Set the over-scroll mode for this view. |
void |
setSmoothScrollingEnabled(boolean smoothScrollingEnabled) Set whether arrow scrolling will animate its transition. |
boolean |
shouldDelayChildPressedState() Return true if the pressed state should be delayed for children or descendants of this ViewGroup. |
final void |
smoothScrollBy(int dx, int dy) Like |
final void |
smoothScrollTo(int x, int y) Like |
Protected methods |
|
---|---|
int |
computeScrollDeltaToGetChildRectOnScreen(Rect rect) Compute the amount to scroll in the Y direction in order to get a rectangle completely on the screen (or, if taller than the screen, at least the first screen size chunk of it). |
int |
computeVerticalScrollOffset() Compute the vertical offset of the vertical scrollbar's thumb within the horizontal range. |
int |
computeVerticalScrollRange() The scroll range of a scroll view is the overall height of all of its children. |
float |
getBottomFadingEdgeStrength() Returns the strength, or intensity, of the bottom faded edge. |
float |
getTopFadingEdgeStrength() Returns the strength, or intensity, of the top faded edge. |
void |
measureChild(View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec) Ask one of the children of this view to measure itself, taking into account both the MeasureSpec requirements for this view and its padding. |
void |
measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed) 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. |
void |
onDetachedFromWindow() This is called when the view is detached from a window. |
void |
onLayout(boolean changed, int l, int t, int r, int b) Called from layout when this view should assign a size and position to each of its children. |
void |
onMeasure(int widthMeasureSpec, int heightMeasureSpec) Measure the view and its content to determine the measured width and the measured height. |
void |
onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) Called by |
boolean |
onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) When looking for focus in children of a scroll view, need to be a little more careful not to give focus to something that is scrolled off screen. |
void |
onRestoreInstanceState(Parcelable state) Hook allowing a view to re-apply a representation of its internal state that had previously been generated by |
Parcelable |
onSaveInstanceState() Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state. |
void |
onSizeChanged(int w, int h, int oldw, int oldh) This is called during layout when the size of this view has changed. |
Inherited methods |
|
---|---|
From class |
|
From class |
|
From class |
|
From class |
|
From interface |
|
From interface |
|
From interface |
|
From interface |
|
From interface |
XML attributes
android:fillViewport
Defines whether the scrollview should stretch its content to fill the viewport.
May be a boolean value, such as "true
" or "false
".
Related methods:
Public constructors
ScrollView
added in API level 1
public ScrollView (Context context)
Parameters | |
---|---|
context |
Context |
ScrollView
added in API level 1
public ScrollView (Context context, AttributeSet attrs)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
ScrollView
added in API level 1
public ScrollView (Context context, AttributeSet attrs, int defStyleAttr)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
ScrollView
added in API level 21
public ScrollView (Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
Parameters | |
---|---|
context |
Context |
attrs |
AttributeSet |
defStyleAttr |
int |
defStyleRes |
int |
Public methods
addView
added in API level 1
public void addView (View child, int index)
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 View.draw(android.graphics.Canvas)
, View.onDraw(android.graphics.Canvas)
, dispatchDraw(android.graphics.Canvas)
or any related method.
Parameters | |
---|---|
child |
View : the child view to add |
index |
int : the position at which to add the child |
addView
added in API level 1
public void addView (View child, ViewGroup.LayoutParams params)
Adds a child view with the specified layout parameters.
Note: do not invoke this method from View.draw(android.graphics.Canvas)
, View.onDraw(android.graphics.Canvas)
, dispatchDraw(android.graphics.Canvas)
or any related method.
Parameters | |
---|---|
child |
View : the child view to add |
params |
ViewGroup.LayoutParams : the layout parameters to set on the child |
addView
added in API level 1
public void addView (View child, int index, ViewGroup.LayoutParams params)
Adds a child view with the specified layout parameters.
Note: do not invoke this method from View.draw(android.graphics.Canvas)
, View.onDraw(android.graphics.Canvas)
, dispatchDraw(android.graphics.Canvas)
or any related method.
Parameters | |
---|---|
child |
View : the child view to add |
index |
int : the position at which to add the child or -1 to add last |
params |
ViewGroup.LayoutParams : the layout parameters to set on the child |
addView
added in API level 1
public void addView (View 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 View.draw(android.graphics.Canvas)
, View.onDraw(android.graphics.Canvas)
, dispatchDraw(android.graphics.Canvas)
or any related method.
Parameters | |
---|---|
child |
View : the child view to add |
arrowScroll
added in API level 1
public boolean arrowScroll (int direction)
Handle scrolling in response to an up or down arrow click.
Parameters | |
---|---|
direction |
int : The direction corresponding to the arrow key that was pressed |
Returns | |
---|---|
boolean |
True if we consumed the event, false otherwise |
computeScroll
added in API level 1
public void computeScroll ()
Called by a parent to request that a child update its values for mScrollX and mScrollY if necessary. This will typically be done if the child is animating a scroll using a Scroller
object.
dispatchKeyEvent
added in API level 1
public boolean dispatchKeyEvent (KeyEvent event)
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.
Parameters | |
---|---|
event |
KeyEvent : The key event to be dispatched. |
Returns | |
---|---|
boolean |
True if the event was handled, false otherwise. |
draw
added in API level 1
public void draw (Canvas canvas)
Manually render this view (and all of its children) to the given Canvas. The view must have already done a full layout before this function is called. When implementing a view, implement onDraw(android.graphics.Canvas)
instead of overriding this method. If you do need to override this method, call the superclass version.
Parameters | |
---|---|
canvas |
Canvas : The Canvas to which the View is rendered. |
executeKeyEvent
added in API level 1
public boolean executeKeyEvent (KeyEvent event)
You can call this function yourself to have the scroll view perform scrolling from a key event, just as if the event had been dispatched to it by the view hierarchy.
Parameters | |
---|---|
event |
KeyEvent : The key event to execute. |
Returns | |
---|---|
boolean |
Return true if the event was handled, else false. |
fling
added in API level 1
public void fling (int velocityY)
Fling the scroll view
Parameters | |
---|---|
velocityY |
int : The initial velocity in the Y direction. Positive numbers mean that the finger/cursor is moving down the screen, which means we want to scroll towards the top. |
fullScroll
added in API level 1
public boolean fullScroll (int direction)
Handles scrolling in response to a "home/end" shortcut press. This method will scroll the view to the top or bottom and give the focus to the topmost/bottommost component in the new visible area. If no component is a good candidate for focus, this scrollview reclaims the focus.
Parameters | |
---|---|
direction |
int : the scroll direction: View.FOCUS_UP to go the top of the view or View.FOCUS_DOWN to go the bottom |
Returns | |
---|---|
boolean |
true if the key event is consumed by this method, false otherwise |
getAccessibilityClassName
added in API level 23
public CharSequence getAccessibilityClassName ()
Return the class name of this object to be used for accessibility purposes. Subclasses should only override this if they are implementing something that should be seen as a completely new class of view when used by accessibility, unrelated to the class it is deriving from. This is used to fill in AccessibilityNodeInfo.setClassName
.
Returns | |
---|---|
CharSequence |
getMaxScrollAmount
added in API level 1
public int getMaxScrollAmount ()
Returns | |
---|---|
int |
The maximum amount this scroll view will scroll in response to an arrow event. |
isFillViewport
added in API level 1
public boolean isFillViewport ()
Indicates whether this ScrollView's content is stretched to fill the viewport.
Related XML Attributes:
Returns | |
---|---|
boolean |
True if the content fills the viewport, false otherwise. |
isSmoothScrollingEnabled
added in API level 1
public boolean isSmoothScrollingEnabled ()
Returns | |
---|---|
boolean |
Whether arrow scrolling will animate its transition. |
onGenericMotionEvent
added in API level 12
public boolean onGenericMotionEvent (MotionEvent event)
Implement this method to handle generic motion events.
Generic motion events describe joystick movements, mouse hovers, track pad touches, scroll wheel movements and other input events. The source
of the motion event specifies the class of input that was received. Implementations of this method must examine the bits in the source before processing the event. The following code example shows how this is done.
Generic motion events with source class InputDevice.SOURCE_CLASS_POINTER
are delivered to the view under the pointer. All other generic motion events are delivered to the focused view.
public boolean onGenericMotionEvent(MotionEvent event) { if (event.isFromSource(InputDevice.SOURCE_CLASS_JOYSTICK)) { if (event.getAction() == MotionEvent.ACTION_MOVE) { // process the joystick movement... return true; } } if (event.isFromSource(InputDevice.SOURCE_CLASS_POINTER)) { switch (event.getAction()) { case MotionEvent.ACTION_HOVER_MOVE: // process the mouse hover movement... return true; case MotionEvent.ACTION_SCROLL: // process the scroll wheel movement... return true; } } return super.onGenericMotionEvent(event); }
Parameters | |
---|---|
event |
MotionEvent : The generic motion event being processed. |
Returns | |
---|---|
boolean |
True if the event was handled, false otherwise. |
onInterceptTouchEvent
added in API level 1
public boolean onInterceptTouchEvent (MotionEvent ev)
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:
- You will receive the down event here.
- The down event will be handled either by a child of this view group, or given to your own onTouchEvent() method to handle; this means you should implement onTouchEvent() to return true, so you will continue to see the rest of the gesture (instead of looking for a parent view to handle it). Also, by returning true from onTouchEvent(), you will not receive any following events in onInterceptTouchEvent() and all touch processing must happen in onTouchEvent() like normal.
- For as long as you return false from this function, each following event (up to and including the final up) will be delivered first here and then to the target's onTouchEvent().
- If you return true from here, you will not receive any following events: the target view will receive the same event but with the action
MotionEvent.ACTION_CANCEL
, and all further events will be delivered to your onTouchEvent() method and no longer appear here.
Parameters | |
---|---|
ev |
MotionEvent : The motion event being dispatched down the hierarchy. |
Returns | |
---|---|
boolean |
Return true to steal motion events from the children and have them dispatched to this ViewGroup through onTouchEvent(). The current target will receive an ACTION_CANCEL event, and no further messages will be delivered here. |
onNestedFling
added in API level 21
public boolean onNestedFling (View target, float velocityX, float velocityY, boolean consumed)
Request a fling from a nested scroll.
This method signifies that a nested scrolling child has detected suitable conditions for a fling. Generally this means that a touch scroll has ended with a velocity
in the direction of scrolling that meets or exceeds the minimum fling velocity
along a scrollable axis.
If a nested scrolling child view would normally fling but it is at the edge of its own content, it can use this method to delegate the fling to its nested scrolling parent instead. The parent may optionally consume the fling or observe a child fling.
Parameters | |
---|---|
target |
View : View that initiated the nested scroll |
velocityX |
float : Horizontal velocity in pixels per second |
velocityY |
float : Vertical velocity in pixels per second |
consumed |
boolean : true if the child consumed the fling, false otherwise |
Returns | |
---|---|
boolean |
true if this parent consumed or otherwise reacted to the fling |
onNestedScroll
added in API level 21
public void onNestedScroll (View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed)
React to a nested scroll in progress.
This method will be called when the ViewParent's current nested scrolling child view dispatches a nested scroll event. To receive calls to this method the ViewParent must have previously returned true
for a call toonStartNestedScroll(View, View, int)
.
Both the consumed and unconsumed portions of the scroll distance are reported to the ViewParent. An implementation may choose to use the consumed portion to match or chase scroll position of multiple child elements, for example. The unconsumed portion may be used to allow continuous dragging of multiple scrolling or draggable elements, such as scrolling a list within a vertical drawer where the drawer begins dragging once the edge of inner scrolling content is reached.
Parameters | |
---|---|
target |
View : The descendent view controlling the nested scroll |
dxConsumed |
int : Horizontal scroll distance in pixels already consumed by target |
dyConsumed |
int : Vertical scroll distance in pixels already consumed by target |
dxUnconsumed |
int : Horizontal scroll distance in pixels not consumed by target |
dyUnconsumed |
int : Vertical scroll distance in pixels not consumed by target |
onNestedScrollAccepted
added in API level 21
public void onNestedScrollAccepted (View child, View target, int axes)
React to the successful claiming of a nested scroll operation.
This method will be called after onStartNestedScroll
returns true. It offers an opportunity for the view and its superclasses to perform initial configuration for the nested scroll. Implementations of this method should always call their superclass's implementation of this method if one is present.
Parameters | |
---|---|
child |
View : Direct child of this ViewParent containing target |
target |
View : View that initiated the nested scroll |
axes |
int : Flags consisting of View.SCROLL_AXIS_HORIZONTAL , View.SCROLL_AXIS_VERTICAL or both |
onStartNestedScroll
added in API level 21
public boolean onStartNestedScroll (View child, View target, int nestedScrollAxes)
React to a descendant view initiating a nestable scroll operation, claiming the nested scroll operation if appropriate.
This method will be called in response to a descendant view invoking View.startNestedScroll(int)
. Each parent up the view hierarchy will be given an opportunity to respond and claim the nested scrolling operation by returning true
.
This method may be overridden by ViewParent implementations to indicate when the view is willing to support a nested scrolling operation that is about to begin. If it returns true, this ViewParent will become the target view's nested scrolling parent for the duration of the scroll operation in progress. When the nested scroll is finished this ViewParent will receive a call to onStopNestedScroll(View)
.
Parameters | |
---|---|
child |
View : Direct child of this ViewParent containing target |
target |
View : View that initiated the nested scroll |
nestedScrollAxes |
int : Flags consisting of View.SCROLL_AXIS_HORIZONTAL , View.SCROLL_AXIS_VERTICAL or both |
Returns | |
---|---|
boolean |
true if this ViewParent accepts the nested scroll operation |
onStopNestedScroll
added in API level 21
public void onStopNestedScroll (View target)
React to a nested scroll operation ending.
Perform cleanup after a nested scrolling operation. This method will be called when a nested scroll stops, for example when a nested touch scroll ends with a MotionEvent.ACTION_UP
or MotionEvent.ACTION_CANCEL
event. Implementations of this method should always call their superclass's implementation of this method if one is present.
Parameters | |
---|---|
target |
View : View that initiated the nested scroll |
onTouchEvent
added in API level 1
public boolean onTouchEvent (MotionEvent ev)
Implement this method to handle touch screen motion events.
If this method is used to detect click actions, it is recommended that the actions be performed by implementing and calling performClick()
. This will ensure consistent system behavior, including:
- obeying click sound preferences
- dispatching OnClickListener calls
- handling
ACTION_CLICK
when accessibility features are enabled
Parameters | |
---|---|
ev |
MotionEvent : The motion event. |
Returns | |
---|---|
boolean |
True if the event was handled, false otherwise. |
pageScroll
added in API level 1
public boolean pageScroll (int direction)
Handles scrolling in response to a "page up/down" shortcut press. This method will scroll the view by one page up or down and give the focus to the topmost/bottommost component in the new visible area. If no component is a good candidate for focus, this scrollview reclaims the focus.
Parameters | |
---|---|
direction |
int : the scroll direction: View.FOCUS_UP to go one page up or View.FOCUS_DOWN to go one page down |
Returns | |
---|---|
boolean |
true if the key event is consumed by this method, false otherwise |
requestChildFocus
added in API level 1
public void requestChildFocus (View child, View focused)
Called when a child of this parent wants focus
Parameters | |
---|---|
child |
View : 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 |
View : The view that is a descendant of child that actually has focus |
requestChildRectangleOnScreen
added in API level 1
public boolean requestChildRectangleOnScreen (View child, Rect rectangle, boolean immediate)
Called when a child of this group wants a particular rectangle to be positioned onto the screen. ViewGroup
s overriding this can trust that:
- child will be a direct child of this group
- rectangle will be in the child's content coordinates
ViewGroup
s overriding this should uphold the contract:
- nothing will change if the rectangle is already visible
- the view port will be scrolled only just enough to make the rectangle visible
Parameters child
View
: The direct child making the request.rectangle
Rect
: The rectangle in the child's coordinates the child wishes to be on the screen.immediate
boolean
: True to forbid animated or delayed scrolling, false otherwiseReturns boolean
Whether the group scrolled to handle the operation
requestDisallowInterceptTouchEvent
added in API level 1
public void requestDisallowInterceptTouchEvent (boolean disallowIntercept)
Called when a child does not want this parent and its ancestors to intercept touch events withViewGroup.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.
Parameters | |
---|---|
disallowIntercept |
boolean : True if the child does not want the parent to intercept touch events. |
requestLayout
added in API level 1
public void requestLayout ()
Call this when something has changed which has invalidated the layout of this view. This will schedule a layout pass of the view tree. This should not be called while the view hierarchy is currently in a layout pass (isInLayout()
. If layout is happening, the request may be honored at the end of the current layout pass (and then layout will run again) or after the current frame is drawn and the next layout occurs.
Subclasses which override this method should call the superclass method to handle possible request-during-layout errors correctly.
scrollTo
added in API level 1
public void scrollTo (int x, int y)
Set the scrolled position of your view. This will cause a call to onScrollChanged(int, int, int, int)
and the view will be invalidated.
This version also clamps the scrolling to the bounds of our child.
Parameters | |
---|---|
x |
int : the x position to scroll to |
y |
int : the y position to scroll to |
setFillViewport
added in API level 1
public void setFillViewport (boolean fillViewport)
Indicates this ScrollView whether it should stretch its content height to fill the viewport or not.
Related XML Attributes:
Parameters | |
---|---|
fillViewport |
boolean : True to stretch the content's height to the viewport's boundaries, false otherwise. |
setOverScrollMode
added in API level 9
public void setOverScrollMode (int mode)
Set the over-scroll mode for this view. Valid over-scroll modes are OVER_SCROLL_ALWAYS
(default), OVER_SCROLL_IF_CONTENT_SCROLLS
(allow over-scrolling only if the view content is larger than the container), or OVER_SCROLL_NEVER
. Setting the over-scroll mode of a view will have an effect only if the view is capable of scrolling.
Parameters | |
---|---|
mode |
int : The new over-scroll mode for this view. |
setSmoothScrollingEnabled
added in API level 1
public void setSmoothScrollingEnabled (boolean smoothScrollingEnabled)
Set whether arrow scrolling will animate its transition.
Parameters | |
---|---|
smoothScrollingEnabled |
boolean : whether arrow scrolling will animate its transition |
shouldDelayChildPressedState
added in API level 14
public boolean shouldDelayChildPressedState ()
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.
Returns | |
---|---|
boolean |
smoothScrollBy
added in API level 1
public final void smoothScrollBy (int dx, int dy)
Like View.scrollBy(int, int)
, but scroll smoothly instead of immediately.
Parameters | |
---|---|
dx |
int : the number of pixels to scroll by on the X axis |
dy |
int : the number of pixels to scroll by on the Y axis |
smoothScrollTo
added in API level 1
public final void smoothScrollTo (int x, int y)
Like scrollTo(int, int)
, but scroll smoothly instead of immediately.
Parameters | |
---|---|
x |
int : the position where to scroll on the X axis |
y |
int : the position where to scroll on the Y axis |
Protected methods
computeScrollDeltaToGetChildRectOnScreen
added in API level 1
protected int computeScrollDeltaToGetChildRectOnScreen (Rect rect)
Compute the amount to scroll in the Y direction in order to get a rectangle completely on the screen (or, if taller than the screen, at least the first screen size chunk of it).
Parameters | |
---|---|
rect |
Rect : The rect. |
Returns | |
---|---|
int |
The scroll delta. |
computeVerticalScrollOffset
added in API level 1
protected int computeVerticalScrollOffset ()
Compute the vertical offset of the vertical scrollbar's thumb within the horizontal range. This value is used to compute the position of the thumb within the scrollbar's track.
The range is expressed in arbitrary units that must be the same as the units used by computeVerticalScrollRange()
and computeVerticalScrollExtent()
.
The default offset is the scroll offset of this view.
Returns | |
---|---|
int |
the vertical offset of the scrollbar's thumb |
computeVerticalScrollRange
added in API level 1
protected int computeVerticalScrollRange ()
The scroll range of a scroll view is the overall height of all of its children.
Returns | |
---|---|
int |
the total vertical range represented by the vertical scrollbar The default range is the drawing height of this view. |
getBottomFadingEdgeStrength
added in API level 1
protected float getBottomFadingEdgeStrength ()
Returns the strength, or intensity, of the bottom faded edge. The strength is a value between 0.0 (no fade) and 1.0 (full fade). The default implementation returns 0.0 or 1.0 but no value in between. Subclasses should override this method to provide a smoother fade transition when scrolling occurs.
Returns | |
---|---|
float |
the intensity of the bottom fade as a float between 0.0f and 1.0f |
getTopFadingEdgeStrength
added in API level 1
protected float getTopFadingEdgeStrength ()
Returns the strength, or intensity, of the top faded edge. The strength is a value between 0.0 (no fade) and 1.0 (full fade). The default implementation returns 0.0 or 1.0 but no value in between. Subclasses should override this method to provide a smoother fade transition when scrolling occurs.
Returns | |
---|---|
float |
the intensity of the top fade as a float between 0.0f and 1.0f |
measureChild
added in API level 1
protected void measureChild (View child, int parentWidthMeasureSpec, int parentHeightMeasureSpec)
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.
Parameters | |
---|---|
child |
View : The child to measure |
parentWidthMeasureSpec |
int : The width requirements for this view |
parentHeightMeasureSpec |
int : The height requirements for this view |
measureChildWithMargins
added in API level 1
protected void measureChildWithMargins (View child, int parentWidthMeasureSpec, int widthUsed, int parentHeightMeasureSpec, int heightUsed)
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.
Parameters | |
---|---|
child |
View : The child to measure |
parentWidthMeasureSpec |
int : The width requirements for this view |
widthUsed |
int : Extra space that has been used up by the parent horizontally (possibly by other children of the parent) |
parentHeightMeasureSpec |
int : The height requirements for this view |
heightUsed |
int : Extra space that has been used up by the parent vertically (possibly by other children of the parent) |
onDetachedFromWindow
added in API level 1
protected void onDetachedFromWindow ()
This is called when the view is detached from a window. At this point it no longer has a surface for drawing.
onLayout
added in API level 1
protected void onLayout (boolean changed, int l, int t, int r, int b)
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.
Parameters | |
---|---|
changed |
boolean : This is a new size or position for this view |
l |
int : Left position, relative to parent |
t |
int : Top position, relative to parent |
r |
int : Right position, relative to parent |
b |
int : Bottom position, relative to parent |
onMeasure
added in API level 1
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)
Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int)
and should be overridden by subclasses to provide accurate and efficient measurement of their contents.
CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int)
to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException
, thrown by measure(int, int)
. Calling the superclass' onMeasure(int, int)
is a valid use.
The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override onMeasure(int, int)
to provide better measurements of their content.
If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight()
and getSuggestedMinimumWidth()
).
Parameters | |
---|---|
widthMeasureSpec |
int : horizontal space requirements as imposed by the parent. The requirements are encoded withView.MeasureSpec . |
heightMeasureSpec |
int : vertical space requirements as imposed by the parent. The requirements are encoded withView.MeasureSpec . |
onOverScrolled
added in API level 9
protected void onOverScrolled (int scrollX, int scrollY, boolean clampedX, boolean clampedY)
Called by overScrollBy(int, int, int, int, int, int, int, int, boolean)
to respond to the results of an over-scroll operation.
Parameters | |
---|---|
scrollX |
int : New X scroll value in pixels |
scrollY |
int : New Y scroll value in pixels |
clampedX |
boolean : True if scrollX was clamped to an over-scroll boundary |
clampedY |
boolean : True if scrollY was clamped to an over-scroll boundary |
onRequestFocusInDescendants
added in API level 1
protected boolean onRequestFocusInDescendants (int direction, Rect previouslyFocusedRect)
When looking for focus in children of a scroll view, need to be a little more careful not to give focus to something that is scrolled off screen. This is more expensive than the default ViewGroup
implementation, otherwise this behavior might have been made the default.
Parameters | |
---|---|
direction |
int : One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT |
previouslyFocusedRect |
Rect : 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. |
Returns | |
---|---|
boolean |
Whether focus was taken. |
onRestoreInstanceState
added in API level 1
protected void onRestoreInstanceState (Parcelable state)
Hook allowing a view to re-apply a representation of its internal state that had previously been generated by onSaveInstanceState()
. This function will never be called with a null state.
Parameters | |
---|---|
state |
Parcelable : The frozen state that had previously been returned by onSaveInstanceState() . |
onSaveInstanceState
added in API level 1
protected Parcelable onSaveInstanceState ()
Hook allowing a view to generate a representation of its internal state that can later be used to create a new instance with that same state. This state should only contain information that is not persistent or can not be reconstructed later. For example, you will never store your current position on screen because that will be computed again when a new instance of the view is placed in its view hierarchy.
Some examples of things you may store here: the current cursor position in a text view (but usually not the text itself since that is stored in a content provider or other persistent storage), the currently selected item in a list view.
Returns | |
---|---|
Parcelable |
Returns a Parcelable object containing the view's current dynamic state, or null if there is nothing interesting to save. |
onSizeChanged
added in API level 1
protected void onSizeChanged (int w, int h, int oldw, int oldh)
This is called during layout when the size of this view has changed. If you were just added to the view hierarchy, you're called with the old values of 0.
Parameters | |
---|---|
w |
int : Current width of this view. |
h |
int : Current height of this view. |
oldw |
int : Old width of this view. |
oldh |
int : Old height of this view. |