Gallery is deprecated:Gallery 过期了怎么办?(一)

前段时间就发现Gallery这个控件被无情的打上了过期这个烙印。当时很好奇,谷歌为什么就让它过期了,不过那个时候项目忙就没有仔细去看了,反正过期了还能用,只是不维护了嘛。今天刚好有点时间就翻了翻android SDK文档:

This class was deprecated in API level 16.
This widget is no longer supported. Other horizontally scrolling widgets include HorizontalScrollView and ViewPager from the support library.

啊哦,在API level 16(Android 4.1)就过期了,也就是谷歌不在维护这个类了。谷歌还推荐了可以用HorizontalScrollView 或者ViewPager 来代替它。

首先,先给Gallery默哀一分钟吧,过期了,离死也就不远了。这期,我们先谈谈Gallery为什么会被谷歌判了死刑吧。

网上查了一圈,最后只在stackoverflow找到了一个类似的问题和一个疑似答案的答案,附上问题链接:

        http://stackoverflow.com/questions/11868503/the-type-gallery-is-deprecated-whats-the-best-alternative


简明说下大意:

问:很奇怪这个控件已经过期。
这个控件过期了,HorizontalScrollView 或者ViewPager该换用哪个?

答:我想HorizontalScrollView这个控件最符合你的要求。

我怀疑Gallery过期的原因是因为它的适配器不能合适的转换视图,它每次切换图片时都要新建视图造成浪费太多的资源。

另外你可以选择使用The third part created ecogallery,它克服了gallery不能回收视图的缺点;但不幸的是,通过网络只在pastebin上找到它的有关资料。(这句话无视吧,那个链接访问不了)

通过这段对话,可以推测出gallery的生理缺陷:它每次切换图片时都要新建视图造成浪费太多的资源。很有可能是因为这个原因被谷歌封杀的。对话中还提到了使用第三方gallery控件来解决这个问题。鉴于很多人还在使用gallery,我就把这个ecogallery这个项目地址也贴出来,供大家优化gallery使用。

EcoGallery
Replacement for Gallery widget in Android with View recycling. This originally made by Joseph Earl (details).

Usage:

1- Include 'EcoGallery' android library to your project. (Eclipse project: properties -> Android -> Library - Add)

2- You could declare EcoGallery in the layout xml file, Example:

android:id="@+id/images"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
/>
3- Use EcoGalleryAdapterView instead of AdapterView.

For a complete tutorial check the wiki.

Sample App

Click here to view the sample app

Change Log:

June 24, 2013

Compiler warnings fixed @mnowrot
Limitation:

The existing implementation assumed that each different position in the adapter resulted in a unique view. The changes are only good if your Gallery contains only one type of item, if not you'll need to add some sort of key based on item type and the amount of that type required.


EcoGallery项目github地址:https://github.com/falnatsheh/EcoGallery/

Gallery is deprecated:Gallery 过期了怎么办?(一)先讲这些了,接下来(二)(三)会详细讲该选用哪个控件来代替gallery和具体的代码。



你可能感兴趣的:(Android)