android - 安卓PagerAdapter中的isViewFromObject()方法有什么用?
ringa_lee
ringa_lee 2017-04-17 11:36:44
0
1
640

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

api这么写,判断view是不是和object相关,还是不大懂要干嘛?

ringa_lee
ringa_lee

ringa_lee

reply all(1)
刘奇

One method of PagerAdapter is

public Object instantiateItem (ViewGroup container, int 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.

This method declares that the return value is not necessarily a view, but can be any object. You must know that the view is added inside this method through the container, so this method does not necessarily need to return the view.

The isViewFromObject method is used to determine whether a view of the pager is related to the object returned by the instantiateItem method. If there is a correlation, what should be done? Go look at the code
ViewPager source code. If you look at the addNewItem method, you will find how to use instantiateItem. Pay attention to the mItems variable here. Then if you search for isViewFromObject, you will find that it is called by the infoForChild method, and the return value is ItemInfo. Let's take a look at the structure of ItemInfo. There is an object object in it, and this value is returned by instantiateItem.

In other words, ViewPager uses an mItems (ArrayList) to store the information (ItemInfo) of each page. When the interface is to be displayed or changes, it needs to be adjusted based on the current information of the page, but at this time it can only Search through view, so you can only traverse mItems and find the corresponding ItemInfo by comparing view and object.

It’s a bit confusing, just read the source code and you’ll understand!

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template