android - 弃用的 PagerAdapter.instantiateItem() 方法

标签 android android-pageradapter

我很好奇为什么 instantiateItem 被弃用,取而代之的是它的新版本。变化在于它现在接收 ViewGroup 而不是更通用的 View

弃用的方法

public Object instantiateItem (View container, int position)

新方法

public Object instantiateItem (ViewGroup container, int position)

注意:此更改也发生在 destroyItemstartUpdatefinishUpdatesetPrimaryItem 中。

最佳答案

我的猜测是这样做是因为这些方法总是使用 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/

相关文章:

android - PageTabStrip 类未找到异常

java - 将图像从 url 添加到 viewpager 的第二和第三位置

android - 删除任何 View 时,不会调用 PageAdapter 的 destroyItem 函数

android - ViewPager.setCurrentItem 在 PagerAdapter 中返回错误的位置

c# - 在 list 中发现意外元素 <uses-library>

android - 无法为 opencv android 加载信息库

android - Firebase 使用 Google 帐户覆盖登录

android - 如何处理 Android 中的 OutOfMemoryError?

Android - 来自连接的联系人照片