IT. Expert System.

Android Reference


Class ViewPager

  • All Implemented Interfaces:
    Drawable.Callback, AccessibilityEventSource, KeyEvent.Callback, ViewManager, ViewParent

    public class ViewPager
    extends ViewGroup
    Layout manager that allows the user to flip left and right through pages of data. You supply an implementation of a PagerAdapter to generate the pages that the view shows.

    Note this class is currently under early design and development. The API will likely change in later updates of the compatibility library, requiring changes to the source code of apps when they are compiled against the newer version.

    ViewPager is most often used in conjunction with Fragment, which is a convenient way to supply and manage the lifecycle of each page. There are standard adapters implemented for using fragments with the ViewPager, which cover the most common use cases. These are FragmentPagerAdapter, FragmentStatePagerAdapter, FragmentPagerAdapter, and FragmentStatePagerAdapter; each of these classes have simple code showing how to build a full user interface with them.

    Here is a more complicated example of ViewPager, using it in conjuction with ActionBar tabs. You can find other examples of using ViewPager in the API 4+ Support Demos and API 13+ Support Demos sample code.

    • Field Detail


        public static final int SCROLL_STATE_IDLE
        Indicates that the pager is in an idle, settled state. The current page is fully in view and no animation is in progress.
        See Also:
        Constant Field Values

        public static final int SCROLL_STATE_DRAGGING
        Indicates that the pager is currently being dragged by the user.
        See Also:
        Constant Field Values

        public static final int SCROLL_STATE_SETTLING
        Indicates that the pager is in the process of settling to a final position.
        See Also:
        Constant Field Values
    • Method Detail

      • onDetachedFromWindow

        protected void onDetachedFromWindow()
        Description copied from class: View
        This is called when the view is detached from a window. At this point it no longer has a surface for drawing.
        onDetachedFromWindow in class View
        See Also:
      • setAdapter

        public void setAdapter(PagerAdapter adapter)
        Set a PagerAdapter that will supply views for this pager as needed.
        adapter - Adapter to use
      • getAdapter

        public PagerAdapter getAdapter()
        Retrieve the current adapter supplying pages.
        The currently registered PagerAdapter
      • setCurrentItem

        public void setCurrentItem(int item)
        Set the currently selected page. If the ViewPager has already been through its first layout with its current adapter there will be a smooth animated transition between the current item and the specified item.
        item - Item index to select
      • setCurrentItem

        public void setCurrentItem(int item,
                          boolean smoothScroll)
        Set the currently selected page.
        item - Item index to select
        smoothScroll - True to smoothly scroll to the new item, false to transition immediately
      • getCurrentItem

        public int getCurrentItem()
      • setPageTransformer

        public void setPageTransformer(boolean reverseDrawingOrder,
                              ViewPager.PageTransformer transformer)
        Set a ViewPager.PageTransformer that will be called for each attached page whenever the scroll position is changed. This allows the application to apply custom property transformations to each page, overriding the default sliding look and feel.

        Note: Prior to Android 3.0 the property animation APIs did not exist. As a result, setting a PageTransformer prior to Android 3.0 (API 11) will have no effect.

        reverseDrawingOrder - true if the supplied PageTransformer requires page views to be drawn from last to first instead of first to last.
        transformer - PageTransformer that will modify each page's animation properties
      • getOffscreenPageLimit

        public int getOffscreenPageLimit()
        Returns the number of pages that will be retained to either side of the current page in the view hierarchy in an idle state. Defaults to 1.
        How many pages will be kept offscreen on either side
        See Also:
      • setOffscreenPageLimit

        public void setOffscreenPageLimit(int limit)
        Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

        This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.

        You should keep this limit low, especially if your pages have complex layouts. This setting defaults to 1.

        limit - How many pages will be kept offscreen in an idle state.
      • getPageMargin

        public int getPageMargin()
        Return the margin between pages.
        The size of the margin in pixels
      • setPageMarginDrawable

        public void setPageMarginDrawable(Drawable d)
        Set a drawable that will be used to fill the margin between pages.
        d - Drawable to display between pages
      • setPageMarginDrawable

        public void setPageMarginDrawable(int resId)
        Set a drawable that will be used to fill the margin between pages.
        resId - Resource ID of a drawable to display between pages
      • verifyDrawable

        protected boolean verifyDrawable(Drawable who)
        Description copied from class: View
        If your view subclass is displaying its own Drawable objects, it should override this function and return true for any Drawable it is displaying. This allows animations for those drawables to be scheduled.

        Be sure to call through to the super class when overriding this function.

        verifyDrawable in class View
        who - The Drawable to verify. Return true if it is one you are displaying, else return the result of calling through to the super class.
        boolean If true than the Drawable is being displayed in the view; else false and it is not allowed to animate.
        See Also:
        View.unscheduleDrawable(, View.drawableStateChanged()
      • drawableStateChanged

        protected void drawableStateChanged()
        Description copied from class: View
        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.

        drawableStateChanged in class ViewGroup
        See Also:
      • onSaveInstanceState

        public Parcelable onSaveInstanceState()
        Description copied from class: View
        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.

        onSaveInstanceState in class View
        Returns a Parcelable object containing the view's current dynamic state, or null if there is nothing interesting to save. The default implementation returns null.
        See Also:
        View.onRestoreInstanceState(android.os.Parcelable), View.saveHierarchyState(android.util.SparseArray), View.dispatchSaveInstanceState(android.util.SparseArray), View.setSaveEnabled(boolean)
      • onSizeChanged

        protected void onSizeChanged(int w,
                         int h,
                         int oldw,
                         int oldh)
        Description copied from class: View
        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.
        onSizeChanged in class View
        w - Current width of this view.
        h - Current height of this view.
        oldw - Old width of this view.
        oldh - Old height of this view.
      • onLayout

        protected void onLayout(boolean changed,
                    int l,
                    int t,
                    int r,
                    int b)
        Description copied from class: ViewGroup
        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.
        Specified by:
        onLayout in class ViewGroup
        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
      • computeScroll

        public void computeScroll()
        Description copied from class: View
        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.
        computeScroll in class View
      • onPageScrolled

        protected void onPageScrolled(int position,
                          float offset,
                          int offsetPixels)
        This method will be invoked when the current page is scrolled, either as part of a programmatically initiated smooth scroll or a user initiated touch scroll. If you override this method you must call through to the superclass implementation (e.g. super.onPageScrolled(position, offset, offsetPixels)) before onPageScrolled returns.
        position - Position index of the first page currently being displayed. Page position+1 will be visible if positionOffset is nonzero.
        offset - Value from [0, 1) indicating the offset from the page at position.
        offsetPixels - Value in pixels indicating the offset from position.
      • onInterceptTouchEvent

        public boolean onInterceptTouchEvent(MotionEvent ev)
        Description copied from class: ViewGroup
        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:

        1. You will receive the down event here.
        2. 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.
        3. 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().
        4. 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.
        onInterceptTouchEvent in class ViewGroup
        ev - The motion event being dispatched down the hierarchy.
        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.
      • onTouchEvent

        public boolean onTouchEvent(MotionEvent ev)
        Description copied from class: View
        Implement this method to handle touch screen motion events.
        onTouchEvent in class View
        ev - The motion event.
        True if the event was handled, false otherwise.
      • draw

        public void draw(Canvas canvas)
        Description copied from class: View
        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 View.onDraw( instead of overriding this method. If you do need to override this method, call the superclass version.
        draw in class View
        canvas - The Canvas to which the View is rendered.
      • onDraw

        protected void onDraw(Canvas canvas)
        Description copied from class: View
        Implement this to do your drawing.
        onDraw in class View
        canvas - the canvas on which the background will be drawn
      • beginFakeDrag

        public boolean beginFakeDrag()
        Start a fake drag of the pager.

        A fake drag can be useful if you want to synchronize the motion of the ViewPager with the touch scrolling of another view, while still letting the ViewPager control the snapping motion and fling behavior. (e.g. parallax-scrolling tabs.) Call fakeDragBy(float) to simulate the actual drag motion. Call endFakeDrag() to complete the fake drag and fling as necessary.

        During a fake drag the ViewPager will ignore all touch events. If a real drag is already in progress, this method will return false.

        true if the fake drag began successfully, false if it could not be started.
        See Also:
        fakeDragBy(float), endFakeDrag()
      • fakeDragBy

        public void fakeDragBy(float xOffset)
        Fake drag by an offset in pixels. You must have called beginFakeDrag() first.
        xOffset - Offset in pixels to drag by.
        See Also:
        beginFakeDrag(), endFakeDrag()
      • canScroll

        protected boolean canScroll(View v,
                        boolean checkV,
                        int dx,
                        int x,
                        int y)
        Tests scrollability within child views of v given a delta of dx.
        v - View to test for horizontal scrollability
        checkV - Whether the view v passed should itself be checked for scrollability (true), or just its children (false).
        dx - Delta scrolled in pixels
        x - X coordinate of the active touch point
        y - Y coordinate of the active touch point
        true if child views of v can be scrolled by delta of dx.
      • dispatchKeyEvent

        public boolean dispatchKeyEvent(KeyEvent event)
        Description copied from class: ViewGroup
        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.
        dispatchKeyEvent in class ViewGroup
        event - The key event to be dispatched.
        True if the event was handled, false otherwise.
      • executeKeyEvent

        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.
        event - The key event to execute.
        Return true if the event was handled, else false.
      • arrowScroll

        public boolean arrowScroll(int direction)
      • addFocusables

        public void addFocusables(ArrayList<View> views,
                         int direction,
                         int focusableMode)
        We only want the current page that is being shown to be focusable.
        addFocusables in class ViewGroup
        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.
        See Also:
      • addTouchables

        public void addTouchables(ArrayList<View> views)
        We only want the current page that is being shown to be touchable.
        addTouchables in class ViewGroup
        views - Touchable views found so far
      • onRequestFocusInDescendants

        protected boolean onRequestFocusInDescendants(int direction,
                                          Rect previouslyFocusedRect)
        We only want the current page that is being shown to be focusable.
        onRequestFocusInDescendants in class ViewGroup
        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.
        Whether focus was taken.
      • generateDefaultLayoutParams

        protected ViewGroup.LayoutParams generateDefaultLayoutParams()
        Description copied from class: ViewGroup
        Returns a set of default layout parameters. These parameters are requested when the View passed to ViewGroup.addView(View) has no layout parameters already set. If null is returned, an exception is thrown from addView.
        generateDefaultLayoutParams in class ViewGroup
        a set of default layout parameters or null


Android Reference

Java basics

Java Enterprise Edition (EE)

Java Standard Edition (SE)





Java Script








Design patterns

RFC (standard status)

RFC (proposed standard status)

RFC (draft standard status)

RFC (informational status)

RFC (experimental status)

RFC (best current practice status)

RFC (historic status)

RFC (unknown status)

IT dictionary

All information of this service is derived from the free sources and is provided solely in the form of quotations. This service provides information and interfaces solely for the familiarization (not ownership) and under the "as is" condition.
Copyright 2016 © ELTASK.COM. All rights reserved.
Site is optimized for mobile devices.
Downloads: 35 / 159135757. Delta: 0.09212 с