Make carousel with ViewFlipper or ViewPager

后端 未结 3 1495
挽巷
挽巷 2021-02-02 02:53

Since GalleryView deprecated we should immigrate to some alternative widgets, In my case ViewFlipper is the best but I have faced with several issues,

相关标签:
3条回答
  • 2021-02-02 03:09

    after going through your code i found that CarouselAdapter is behaving in circular manner(infinite scrolling) part of code is which causes this is:-

    @Override
        public int getCount() 
        {
            return Integer.MAX_VALUE;
        }
    

    and

    @Override
        public Object instantiateItem(ViewGroup container, int position)
        {
            if (position >= bannerUri.length) 
                position %= bannerUri.length;
           .
           . 
           .
        }
    

    However you are doing

            ImageView i = new ImageView(mContext);
            displayImage(i, bannerUri[position]);
            i.setScaleType(ScaleType.FIT_XY); 
            container.addView(i);
            return i;
    

    in instantiateItem, so for each page in your ViewPager, you are allocating memory for ImageView and ViewPager is getting run out of memory because of this. ViewPager has method public void setOffscreenPageLimit (int limit) which will limit the memory allocated by destroying idle pages in view hierarchy.. See below from the android documentation

    Set the number of pages that should be retained to either side of the current page in the view hierarchy in an idle state. Pages beyond this limit will be recreated from the adapter when needed.

    This is offered as an optimization. If you know in advance the number of pages you will need to support or have lazy-loading mechanisms in place on your pages, tweaking this setting can have benefits in perceived smoothness of paging animations and interaction. If you have a small number of pages (3-4) that you can keep active all at once, less time will be spent in layout for newly created view subtrees as the user pages back and forth.

    You should keep this limit low, especially if your pages have complex layouts. This setting defaults to 1.

    As mentioned above, If do not specify any value to it , it will use default value 1 and ViewPager will destroy idle pages from view hierarchy. So you need to pass optimised value to it, according to your memory requirements(number of images and average image size), that will solve your Out of memory issue..

    0 讨论(0)
  • 2021-02-02 03:10

    Ok, here's one thing you can do:

    1. Use a horizontal list view solution (example here)

    2. Make sure each item takes about 1/3 of the width of the screen. Maybe a bit more.

    3. For the stickiness of the center item, handle touch events so that it will smooth scroll to the middle item, for example as shown on samsung's sample "circle launcher" .

    4. For more fancy effects, make the size/location properties of the views change similar to the sample i've written about on #3 .

    0 讨论(0)
  • 2021-02-02 03:17

    Consider using a ViewPager with multiple pages:

    http://commonsware.com/blog/2012/08/20/multiple-view-viewpager-options.html

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