How to migrate from Gallery to HorizontalScrollView & ViewPager?

后端 未结 2 468
夕颜
夕颜 2020-12-02 10:44

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 HorizontalScrollVie

相关标签:
2条回答
  • 2020-12-02 11:11

    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:

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

    I then specified this dimension in my 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:

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

    (Note that the viewpager_margin_fix dimension is half that of the absolute viewpager_margin dimension.)

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

    Phone example in De Telegraaf KrantTablet example

    0 讨论(0)
  • 2020-12-02 11:20

    This gist from Dave Smith shows a way to use ViewPager to have visual results very similar to a Gallery:

    Gallery-style ViewPager

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

    His container (com.example.pagercontainer.PagerContainer) wraps the ViewPager and calls setClipChildren(false); on itself, so even though the ViewPager is focused on one selected page, other pages that have coordinates beyond the ViewPager bounds are still visible, so long as they fit within the PagerContainer. By sizing the ViewPager to be smaller than the PagerContainer, the ViewPager 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, as ViewPager will only handle swipe events on its own visible bounds, ignoring any pages visible to the sides.

    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.

    0 讨论(0)
提交回复
热议问题