Android 如何从 Gallery 迁移到 HorizontalScrollView 和 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
How to migrate from Gallery to HorizontalScrollView & ViewPager?
提问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 HorizontalScrollView
and ViewPager
instead.
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 对我来说是一个更好的解决方案,但它已被弃用,我必须使用HorizontalScrollView
andViewPager
代替。
但是如何轻松迁移?在这种情况下如何使用这些类和控件?我试图为这个主题找到完整的例子,但我找不到它。
回答by CommonsWare
This gist from Dave Smithshows a way to use ViewPager
to have visual results very similar to a Gallery
:
Dave Smith 的这个要点展示了一种用于获得ViewPager
与以下非常相似的视觉效果的方法Gallery
:
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 theViewPager
and callssetClipChildren(false);
on itself, so even though theViewPager
is focused on one selected page, other pages that have coordinates beyond theViewPager
bounds are still visible, so long as they fit within thePagerContainer
. By sizing theViewPager
to be smaller than thePagerContainer
, theViewPager
can 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, asViewPager
will 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
为小于PagerContainer
,ViewPager
可以将其页面调整为该尺寸,从而为其他页面留出可见的空间。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_fix
dimension is half that of the absolute viewpager_margin
dimension.)
(注意viewpager_margin_fix
尺寸是绝对viewpager_margin
尺寸的一半。)
We implemented this in the Dutch newspaper app De Telegraaf Krant:
我们在荷兰报纸应用De Telegraaf Krant 中实现了这一点: