微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

android – 已弃用的PagerAdapter.instantiateItem()方法

我很好奇为什么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");
}

实施者可以直接使用容器.

而事实上,你可以看到这正是070​​00的原因:

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.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐