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.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...