我很好奇为什么 instantiateItem
被弃用,取而代之的是它的新版本。变化在于它现在接收 ViewGroup
而不是更通用的 View
。
弃用的方法
public Object instantiateItem (View container, int position)
新方法
public Object instantiateItem (ViewGroup container, int position)
注意:此更改也发生在 destroyItem
、startUpdate
、finishUpdate
和 setPrimaryItem
中。
最佳答案
我的猜测是这样做是因为这些方法总是使用 ViewGroup
而不是更通用的 View
调用。因此,将参数作为 ViewGroup
提供是一种便利,允许开发人员避免总是检查和转换输入。因此,不要一遍又一遍地看到这段代码:
ViewGroup parent;
if (container instanceof ViewGroup) {
parent = (ViewGroup) container;
}
else {
throw new IllegalArgumentException("container must be a ViewGroup");
}
实现者可以直接使用container
。
事实上,您可以看到这正是 commit message from Adam Powell 中的原因。 :
Bug 5327146 - ViewPager API tweaks and docs
PagerAdapter previously took View instances as parameters to several of its methods leading to lots of casting to ViewGroup in adapter implementations.
Change these to take ViewGroups. Default implementation calls through to deprecated stubs with the existing signatures, allowing current adapters to keep working unmodified.
关于android - 弃用的 PagerAdapter.instantiateItem() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20548422/