Android 如何从 Gallery 迁移到 Horizo​​ntalScrollView 和 ViewPager?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12239886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:08:49  来源:igfitidea点击:

How to migrate from Gallery to HorizontalScrollView & ViewPager?

androidgalleryandroid-viewpagerhorizontalscrollviewmigrate

提问by BArtWell

I need simple control for icon choosing on Android 2.2 and higher.
Gallery was a better solution for me, but it is deprecated and I have to use HorizontalScrollViewand ViewPagerinstead.
But how to migrate easy? How to use this classes and controls in this case? I've try to find complete example for this subject, but I can't find it.

我需要在 Android 2.2 及更高版本上进行图标选择的简单控制。
Gallery 对我来说是一个更好的解决方案,但它已被弃用,我必须使用HorizontalScrollViewandViewPager代替。
但是如何轻松迁移?在这种情况下如何使用这些类和控件?我试图为这个主题找到完整的例子,但我找不到它。

回答by CommonsWare

This gist from Dave Smithshows a way to use ViewPagerto have visual results very similar to a Gallery:

Dave Smith 的这个要点展示了一种用于获得ViewPager与以下非常相似的视觉效果的方法Gallery

Gallery-style ViewPager

画廊风格的 ViewPager

Quoting my blog post on the topicof showing multiple pages at a time in a ViewPager:

引用我的博客文章,主题是在 a 中一次显示多个页面ViewPager

His container (com.example.pagercontainer.PagerContainer) wraps the ViewPagerand calls setClipChildren(false);on itself, so even though the ViewPageris focused on one selected page, other pages that have coordinates beyond the ViewPagerbounds are still visible, so long as they fit within the PagerContainer. By sizing the ViewPagerto be smaller than the PagerContainer, the ViewPagercan size its pages to that size, leaving room for other pages to be seen. PagerContainer, though, needs to help out a bit with touch events, as ViewPagerwill only handle swipe events on its own visible bounds, ignoring any pages visible to the sides.

他的容器 ( com.example.pagercontainer.PagerContainer) 包装了ViewPager并调用setClipChildren(false);了自身,因此即使ViewPager聚焦在一个选定的页面上,坐标超出ViewPager边界的其他页面仍然可见,只要它们适合PagerContainer. 通过将 调整ViewPager为小于PagerContainerViewPager可以将其页面调整为该尺寸,从而为其他页面留出可见的空间。PagerContainer但是,需要对触摸事件有所帮助,因为它ViewPager只会在其自己的可见边界上处理滑动事件,而忽略侧面可见的任何页面。

You might also want to sift through this android-developers thread, where somebody pointed out an issue with this on newer Android versions. You need to disable hardware acceleration due to a bug in ViewPager.

您可能还想通过这个 android-developers thread进行筛选,其中有人指出了在较新的 Android 版本上存在的问题。您需要禁用硬件加速是由于一个错误ViewPager

回答by Paul Lammertsma

There are various problems with touch handling and hardware acceleration in CommonsWare's linked workaround. A simpler and more elegant solution, in my opinion, is to specify a negative margin for the ViewPager:

在 CommonsWare 的链接解决方法中,触摸处理和硬件加速存在各种问题。在我看来,一个更简单、更优雅的解决方案是为 ViewPager 指定负边距:

ViewPager.setPageMargin(
    getResources().getDimensionPixelOffset(R.dimen.viewpager_margin));

I then specified this dimension in my dimens.xml:

然后我在我的中指定了这个维度dimens.xml

<dimen name="viewpager_margin">-64dp</dimen>

To compensate for overlapping pages, each page's content view has the opposite margin:

为了补偿重叠的页面,每个页面的内容视图都有相反的边距:

android:layout_marginLeft="@dimen/viewpager_margin_fix"
android:layout_marginRight="@dimen/viewpager_margin_fix"

Again in dimens.xml:

再次在dimens.xml

<dimen name="viewpager_margin_fix">32dp</dimen>

(Note that the viewpager_margin_fixdimension is half that of the absolute viewpager_margindimension.)

(注意viewpager_margin_fix尺寸是绝对viewpager_margin尺寸的一半。)

We implemented this in the Dutch newspaper app De Telegraaf Krant:

我们在荷兰报纸应用De Telegraaf Krant 中实现了这一点

Phone example in De Telegraaf KrantTablet example

De Telegraaf Krant 中的电话示例平板电脑示例