In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects.
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. 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 and FragmentStatePagerAdapter; each of these classes have simple code showing how to build a full user interface with them.
/** * Set a listener that will be invoked whenever the page changes or is incrementally * scrolled. See OnPageChangeListener. * * @param listener Listener to set * * @deprecated Use addOnPageChangeListener(OnPageChangeListener) * and removeOnPageChangeListener(OnPageChangeListener) instead. */ @Deprecated publicvoidsetOnPageChangeListener(OnPageChangeListener listener){ mOnPageChangeListener = listener; }
/** * Callback interface for responding to changing state of the selected page. */ publicinterfaceOnPageChangeListener{
/** * 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. * * @param position Position index of the first page currently being displayed. * Page position+1 will be visible if positionOffset is nonzero. * @param positionOffset Value from [0, 1) indicating the offset from the page at position. * @param positionOffsetPixels Value in pixels indicating the offset from position. */ publicvoidonPageScrolled(int position, float positionOffset, int positionOffsetPixels);
/** * This method will be invoked when a new page becomes selected. Animation is not * necessarily complete. * * @param position Position index of the new selected page. */ publicvoidonPageSelected(int position);
/** * Called when the scroll state changes. Useful for discovering when the user * begins dragging, when the pager is automatically settling to the current page, * or when it is fully stopped/idle. * * @param state The new scroll state. * @see ViewPager#SCROLL_STATE_IDLE * @see ViewPager#SCROLL_STATE_DRAGGING * @see ViewPager#SCROLL_STATE_SETTLING */ publicvoidonPageScrollStateChanged(int state); }
从源码可以看出该接口是当选中页面状态改变时做出响应的,需要实现依次三个方法,三个方法分别对应着onPageScrolled(int position, float positionOffset, int positionOffsetPixels)(当页面滚动的时候),onPageSelected(int position)(当页面被选的时候),onPageScrollStateChanged(int state)(当页面滚动状态改变的时候)
PagerAdapter
官方api的说明:
Base class providing the adapter to populate pages inside of a ViewPager.You will most likely want to use a more specific implementation of this, such as android.support.v4.app.FragmentPagerAdapter or android.support.v4.app.FragmentStatePagerAdapter.
/** * Create the page for the given position. The adapter is responsible * for adding the view to the container given here, although it only * must ensure this is done by the time it returns from finishUpdate(ViewGroup). * * @param container The containing View in which the page will be shown. * @param position The page position to be instantiated. * @return Returns an Object representing the new page. This does not * need to be a View, but can be some other container of the page. * * @deprecated Use instantiateItem(ViewGroup, int) */ @Deprecated public Object instantiateItem(View container, int position){ thrownew UnsupportedOperationException( "Required method instantiateItem was not overridden"); }
/** * Remove a page for the given position. The adapter is responsible * for removing the view from its container, although it only must ensure * this is done by the time it returns from finishUpdate(View). * * @param container The containing View from which the page will be removed. * @param position The page position to be removed. * @param object The same object that was returned by * instantiateItem(View, int). * * @deprecated Use destroyItem(ViewGroup, int, Object) */ @Deprecated publicvoiddestroyItem(View container, int position, Object object){ thrownew UnsupportedOperationException("Required method destroyItem was not overridden"); }
/** * Return the number of views available. */ publicabstractintgetCount();
/** * Determines whether a page View is associated with a specific key object * as returned by instantiateItem(ViewGroup, int). This method is * required for a PagerAdapter to function properly. * * @param view Page View to check for association with object * @param object Object to check for association with view * @return true if view is associated with the key object object */ publicabstractbooleanisViewFromObject(View view, Object object);
根据源码说的注释说明,可以得知:
**instantiateItem(View container, int position)*该方法是在指定位置创建相应的页面,其中参数container*是展示页面中的view,参数position页面被实例化的位置,不过该方法现在已经被弃用,改用为instantiateItem(ViewGroup container, int position)
**destroyItem(View container, int position, Object object)*该方法是移除指定位置的页面,其中参数container*是将要被移走页面的view,参数position是将要被移除页面的位置,参数object是由instantiateItem(View, int)返回的对象,该方法也被弃用,改用为destroyItem(ViewGroup, int, Object)
其返回值均为view,但是值得注意的是:inflater 是用来找 res/layout 下的 xml 布局文件,并且实例化;findViewById() 是找具体 xml 布局文件中的具体 widget 控件(如:Button、TextView 等
ShareActionProvider
官网api说明:
Provides a share action, which is suitable for an activity’s app bar. Creates views that enable data sharing. If the provider appears in the overflow menu, it creates a submenu with the appropriate sharing actions.