ViewPager 的 PagerAdapter的官方的说明 摘录

注意:

public abstract boolean isViewFromObject (View view, Object object)

判定一个页面的View是否已经分配了特殊的key object,这个key object 是通过instantiateItem返回来的。

Determines whether a page View is associated with a specific key object as returned by instantiateItem(ViewGroup, int).

------isViewFromObject(View view, Object object) :
这个方法,代表检查一下 view 参数代表的对象,是否是利用 object 来创建的,也就是view 是否 和 object 有关系;
这个方法的应用场景,主要就是  Fragment 与 Fragment 的view显示;
------Object instantiateItem(ViewGroup container, int position) instantiate的翻译是实例化
/**
     * 当ViewPager 需要滑动到指定位置的时候, 那么这个方法instantiateItem会被调用;
     * 这个方法内部需要进行控件的添加操作;
     * 返回值用于判断view与返回值之间的关系;=
     * 可以认为这个方法就是 BaseAdapter 中的 getView,

还必须要重写这两个方法:
public Object instantiateItem(ViewGroup container, int position) 
public void destroyItem(ViewGroup container, int position, Object object) {
// !!!
// 必须重写的方法, 不重写抛异常; 并且这两个方法,不允许调用 super的同名方法

PagerAdapter extends Object

java.lang.Object
   ↳ android.support.v4.view.PagerAdapter
Known Direct Subclasses

Class Overview


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 FragmentPagerAdapter or FragmentStatePagerAdapter.

When you implement a PagerAdapter, you must override the following methods at minimum:

  • instantiateItem(ViewGroup, int)
  • destroyItem(ViewGroup, int, Object)
  • getCount()
  • isViewFromObject(View, Object)

PagerAdapter 是一个比给AdapterViews做的adapter 更常用的。ViewPager不再提供一个View复用机制了,而是使用回调去指示那些在更新时候的发生的步骤。一个PagerAdapter可能实现一个表的View的复用,如果请求或使用一个复杂的管理page View的方法例如Fragment 的事务,在这里每一个page 都会被自己的Fragment 所代替。

PagerAdapter is more general than the adapters used for AdapterViews. Instead of providing a View recycling mechanism directly ViewPager uses callbacks to indicate the steps taken during an update. A PagerAdapter may implement a form of View recycling if desired or use a more sophisticated method of managing page Views such as Fragment transactions where each page is represented by its own Fragment.

ViewPage 给每一个页面都有一个key Object而不是直接操作View。这个key 被用来跟踪并且表示一个已提供的页面,这个页面是独立与adapter的的position的。调用PagerAdapter的方法startUpdate(ViewGroup) 指示了ViewPage的将要变化的内容。一个或者多个调用instantiateItem(ViewGroup,int) 或者 destroyItem(ViewGroup,int,Object)将会执行,并且值更新的最后会调用finishUpdate(ViewGroup)。

当finishUpdate 方法执行后,那么那些通过instantiateItem方法返回的被分配额了key 对象的views 应该被添加到父类的ViewGroup里面去,这个ViewGroup 已经在方法里面传递了。还有那些分类了keys的被传递给destroyItem的view是应该被移除。

方法isViewFromObject(View,Object)判断了一个page View 是否已经被分配给了一个已提供的key 对象。

ViewPager associates each page with a key Object instead of working with Views directly. This key is used to track and uniquely identify a given page independent of its position in the adapter. A call to the PagerAdapter method startUpdate(ViewGroup) indicates that the contents of the ViewPager are about to change. One or more calls to instantiateItem(ViewGroup, int) and/or destroyItem(ViewGroup, int, Object) will follow, and the end of an update will be signaled by a call to finishUpdate(ViewGroup). By the time finishUpdate returns the views associated with the key objects returned by instantiateItem should be added to the parent ViewGroup passed to these methods and the views associated with the keys passed to destroyItem should be removed. The method isViewFromObject(View, Object) identifies whether a page View is associated with a given key object.

一个简单的PagerAdapter 可能会自己选择去用页面 Views作为key 对象,通过instantiateItem(ViewGroup,int) 在创建和添加他们到父类的ViewGroup后来返回他们。一个匹配destroyItem(ViewGroup,int,Object) 实现会移除那些从父类的ViewGroup来的View,isViewFromObject(View,Object)可以实现如果返回的view和object 相等。

A very simple PagerAdapter may choose to use the page Views themselves as key objects, returning them from instantiateItem(ViewGroup, int)after creation and adding them to the parent ViewGroup. A matching destroyItem(ViewGroup, int, Object) implementation would remove the View from the parent ViewGroup and isViewFromObject(View, Object) could be implemented as return view == object;.

PagerAdapter 支持数据集变化。数据集变化必须发生在主线程并且必须用使用调用notifyDataSetChanged()类似结束,这个类似于AdapterView 的adapters源自于BaseAdapter。一个数据集的变化可能包含,页面的添加,移除或者变化位置。ViewPager将会保持当前的页面活动,这个adapter 实现了getItemPosition(Object)的方法

PagerAdapter supports data set changes. Data set changes must occur on the main thread and must end with a call to notifyDataSetChanged()similar to AdapterView adapters derived from BaseAdapter. A data set change may involve pages being added, removed, or changing position. The ViewPager will keep the current page active provided the adapter implements the method getItemPosition(Object).

Summary

Constants
int POSITION_NONE  
int POSITION_UNCHANGED  
Public Constructors
PagerAdapter()
Public Methods
void destroyItem( ViewGroup container, int position,  Object object)
Remove a page for the given position.
void destroyItem( View container, int position,  Object object)
This method is deprecated. Use destroyItem(ViewGroup, int, Object)
void finishUpdate( View container)
This method is deprecated. Use finishUpdate(ViewGroup)
void finishUpdate( ViewGroup container)
Called when the a change in the shown pages has been completed.
abstract int getCount()
Return the number of views available.
int getItemPosition( Object object)
Called when the host view is attempting to determine if an item's position has changed.
CharSequence getPageTitle(int position)
This method may be called by the ViewPager to obtain a title string to describe the specified page.
float getPageWidth(int position)
Returns the proportional width of a given page as a percentage of the ViewPager's measured width from (0.f-1.f]
Object instantiateItem( ViewGroup container, int position)
Create the page for the given position.
Object instantiateItem( View container, int position)
This method is deprecated. Use instantiateItem(ViewGroup, int)
abstract boolean isViewFromObject( View view,  Object object)
Determines whether a page View is associated with a specific key object as returned by  instantiateItem(ViewGroup, int).
void notifyDataSetChanged()
This method should be called by the application if the data backing this adapter has changed and associated views should update.
void registerDataSetObserver( DataSetObserver observer)
Register an observer to receive callbacks related to the adapter's data changing.
void restoreState( Parcelable state,  ClassLoader loader)
Restore any instance state associated with this adapter and its pages that was previously saved by  saveState().
Parcelable saveState()
Save any instance state associated with this adapter and its pages that should be restored if the current UI state needs to be reconstructed.
void setPrimaryItem( View container, int position,  Object object)
This method is deprecated. Use setPrimaryItem(ViewGroup, int, Object)
void setPrimaryItem( ViewGroup container, int position,  Object object)
Called to inform the adapter of which item is currently considered to be the "primary", that is the one show to the user as the current page.
void startUpdate( View container)
This method is deprecated. Use startUpdate(ViewGroup)
void startUpdate( ViewGroup container)
Called when a change in the shown pages is going to start being made.
void unregisterDataSetObserver( DataSetObserver observer)
Unregister an observer from callbacks related to the adapter's data changing.
[Expand]
Inherited Methods
 From class java.lang.Object

Constants

public static final int POSITION_NONE

Constant Value: -2 (0xfffffffe)

public static final int POSITION_UNCHANGED

Constant Value: -1 (0xffffffff)

Public Constructors

public PagerAdapter ()

Public Methods

public void destroyItem (ViewGroup container, int position, Object object)

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(ViewGroup).

Parameters
container The containing View from which the page will be removed.
position The page position to be removed.
object The same object that was returned by instantiateItem(View, int).

public void destroyItem (View container, int position, Object object)

This method is deprecated.
Use destroyItem(ViewGroup, int, Object)

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).

Parameters
container The containing View from which the page will be removed.
position The page position to be removed.
object The same object that was returned by instantiateItem(View, int).

public void finishUpdate (View container)

This method is deprecated.
Use finishUpdate(ViewGroup)

Called when the a change in the shown pages has been completed. At this point you must ensure that all of the pages have actually been added or removed from the container as appropriate.

Parameters
container The containing View which is displaying this adapter's page views.

public void finishUpdate (ViewGroup container)

Called when the a change in the shown pages has been completed. At this point you must ensure that all of the pages have actually been added or removed from the container as appropriate.

Parameters
container The containing View which is displaying this adapter's page views.

public abstract int getCount ()

Return the number of views available.

public int getItemPosition (Object object)

Called when the host view is attempting to determine if an item's position has changed. Returns POSITION_UNCHANGED if the position of the given item has not changed or POSITION_NONE if the item is no longer present in the adapter.

The default implementation assumes that items will never change position and always returns POSITION_UNCHANGED.

Parameters
object Object representing an item, previously returned by a call to instantiateItem(View, int).
Returns
  • object's new position index from [0, getCount()), POSITION_UNCHANGED if the object's position has not changed, or POSITION_NONE if the item is no longer present.

public CharSequence getPageTitle (int position)

This method may be called by the ViewPager to obtain a title string to describe the specified page. This method may return null indicating no title for this page. The default implementation returns null.

Parameters
position The position of the title requested
Returns
  • A title for the requested page

public float getPageWidth (int position)

Returns the proportional width of a given page as a percentage of the ViewPager's measured width from (0.f-1.f]

Parameters
position The position of the page requested
Returns
  • Proportional width for the given page position

public Object instantiateItem (ViewGroup container, int position)

给提供的position创建一个页面。

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).

Parameters
container The containing View in which the page will be shown.
position The page position to be instantiated.
Returns
  • Returns an Object representing the new page. This does not need to be a View, but can be some other container of the page.

public Object instantiateItem (View container, int position)

This method is deprecated.
Use instantiateItem(ViewGroup, int)

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).

Parameters
container The containing View in which the page will be shown.
position The page position to be instantiated.
Returns
  • Returns an Object representing the new page. This does not need to be a View, but can be some other container of the page.

public abstract boolean isViewFromObject (View view, Object object)

判定一个页面的View是否已经分配了特殊的key object,这个key object 是通过instantiateItem返回来的。

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.

Parameters
view Page View to check for association with object
object Object to check for association with view
Returns
  • true if view is associated with the key object object

public void notifyDataSetChanged ()

This method should be called by the application if the data backing this adapter has changed and associated views should update.

public void registerDataSetObserver (DataSetObserver observer)

Register an observer to receive callbacks related to the adapter's data changing.

Parameters
observer The DataSetObserver which will receive callbacks.

public void restoreState (Parcelable state, ClassLoader loader)

Restore any instance state associated with this adapter and its pages that was previously saved by saveState().

Parameters
state State previously saved by a call to saveState()
loader A ClassLoader that should be used to instantiate any restored objects

public Parcelable saveState ()

Save any instance state associated with this adapter and its pages that should be restored if the current UI state needs to be reconstructed.

Returns
  • Saved state for this adapter

public void setPrimaryItem (View container, int position, Object object)

This method is deprecated.
Use setPrimaryItem(ViewGroup, int, Object)

Called to inform the adapter of which item is currently considered to be the "primary", that is the one show to the user as the current page.

Parameters
container The containing View from which the page will be removed.
position The page position that is now the primary.
object The same object that was returned by instantiateItem(View, int).

public void setPrimaryItem (ViewGroup container, int position, Object object)

Called to inform the adapter of which item is currently considered to be the "primary", that is the one show to the user as the current page.

Parameters
container The containing View from which the page will be removed.
position The page position that is now the primary.
object The same object that was returned by instantiateItem(View, int).

public void startUpdate (View container)

This method is deprecated.
Use startUpdate(ViewGroup)

Called when a change in the shown pages is going to start being made.

Parameters
container The containing View which is displaying this adapter's page views.

public void startUpdate (ViewGroup container)

Called when a change in the shown pages is going to start being made.

Parameters
container The containing View which is displaying this adapter's page views.

public void unregisterDataSetObserver (DataSetObserver observer)

Unregister an observer from callbacks related to the adapter's data changing.

Parameters
observer The DataSetObserver which will be unregistered.

你可能感兴趣的:(ViewPager 的 PagerAdapter的官方的说明 摘录)