21

I am currently using the Gallery widget to display a sliding list of thumbnails. Each thumbnail has a background colour and a text overlay. (It's a colour chooser).

However as of API version 16, the gallery is deprecated.. As I understand it, phones with API versions greater than 16 aren't guaranteed to have the gallery widget.

I would use a viewpager, but that only shows one view at a time, and I want to show adjacent views too. A horizontal scroll view may do it, but it won't snap to the nearest option like a gallery will.

I've looked for existing widgets, and can't find any. Do you have any suggestions as to what widget I should choose?

Todd Davies
  • 5,484
  • 8
  • 47
  • 71

7 Answers7

15

A good option to replace the Gallery is a ViewPager, works as a listview or gridview, you have to made your own adapter that extends PagerAdater and a layout item. I already use it with this code i attach below, and it works perfectly. good touch response, i hope this can help you out!!!

LAYOUT

<android.support.v4.view.ViewPager
    android:id="@+id/gallery_item"
    android:layout_width="fill_parent"
    android:layout_height="709dp"
    android:layout_below="@+id/relative_headerBar"
    >

</android.support.v4.view.ViewPager> 

CLASS CODE

private ViewPager gallery;
gallery = (ViewPager) findViewById(R.id.gallery_item);

gallery = (ViewPager) findViewById(R.id.gallery_item);
    lista_galeria = new ArrayList<ObjectVerGaleria>();

    int i=0;
    for(i=0;i<listImages.length;i++)
    {       
        ObjectVerGaleria objV = new ObjectVerGaleria();
        objV.setUrlImg(listImages[i]);  
        lista_galeria.add(objV);
    }
    gallery.setAdapter(new AdapterVerGaleria(ctx, lista_galeria));

    gallery.setOnPageChangeListener(new OnPageChangeListener()
    {

        public void onPageSelected(int pos)
        {
            String pathImage = listImages[pos].toString();

            currentPosFront = pos;
            Log.d("setOnItemSelectedListener>>","path:"+pathImage);

        }

        public void onPageScrolled(int arg0, float arg1, int arg2)
        {
            // TODO Auto-generated method stub

        }

        public void onPageScrollStateChanged(int arg0)
        {
            // TODO Auto-generated method stub

        }
    });

ADAPTER

public class AdapterVerGaleria extends PagerAdapter {

private Activity ctx;
private ArrayList<ObjectVerGaleria> dataList;

public AdapterVerGaleria(Activity act, ArrayList<ObjectVerGaleria> lista) {

    ctx = act;
    dataList = lista;
}

public int getCount() {
    return dataList.size();
}

public Object getItem(int pos) {
    return pos;
}

@Override
public Object instantiateItem(View collection, int pos)
{
    ImageView foto = new ImageView(ctx);



        //foto.setLayoutParams(new ViewPager.LayoutParams(Gallery.LayoutParams.FILL_PARENT, Gallery.LayoutParams.FILL_PARENT));
        foto.setScaleType(ImageView.ScaleType.FIT_XY);
        Utils.fetchDrawableOnThread(Utils.getPath(ctx)+"/"+dataList.get(pos).getUrlImg(), foto, true);
        ((ViewPager)collection).addView(foto);


    return foto;
}

@Override
public void destroyItem(View collection, int position, Object view)
{
    ((ViewPager)collection).removeView((ImageView)view);

}

public long getItemId(int pos) {
    return pos;
}



@Override
public boolean isViewFromObject(View view, Object object)
{
    // TODO Auto-generated method stub
    return view == ((ImageView)object);
}

}

user3050757
  • 272
  • 2
  • 3
  • Thanks, works wonderfully. Great smooth touch response, as you wrote. Only the method public AdapterVerGaleria.getItem(int) was something I plain removed as it doesn't seem to serve any purpose. – Karl May 14 '14 at 12:25
  • 6
    what is ObjectVerGaleria? – djdance Jul 04 '14 at 18:26
  • How does that == comparison work in isViewFromObject? I guess it never gets called because you'd have to use equals() to compare – Rarw Jan 31 '16 at 22:07
  • 1
    There is a lot of gaps in this code: lista_galeria not defined, class ObjectVerGaleria not defined, listImages not defined. Make sure to post a working solution. – Totalys Feb 12 '16 at 15:11
9

I used a ViewPager with the it's clipToPadding set to false and equal padding values on the left and the right. This makes a page smaller than the viewpager and center it inside it.

Then I set the viewPager.setPageMargin to a negative value to allow the pages on either side to become visible. This way you have a centered page with others showing.

Then you can also add some fancy animation by setting a PageTransformer on the ViewPager (viewPager.setPageTransformer). I did a rotation and scale using the provided float in the PageTransformer to emulate a carousel like effect.

I hope this is helpful for somebody. Also I think the Gallery was deprecated because the flinging just doesn't feel right. You have no idea which item will be selected after a fling.

Pepijn
  • 1,439
  • 17
  • 16
  • where i have to start. Need a tuts/link @Pepjin – Shihab Uddin Nov 19 '13 at 10:05
  • 1
    More detailed explanation on how to do this : http://stackoverflow.com/questions/13914609/viewpager-with-previous-and-next-page-boundaries – Taiko Apr 03 '15 at 06:21
  • Direct link to a [github project](https://gist.github.com/devunwired/8cbe094bb7a783e37ad1) with working code mentioned in the answer @Taiko shared. – Lamorak May 10 '15 at 11:29
2

Someone wrote a replacement for gallery that recycles it's views. It's based on the original gallery code, so it should be an improvement. Here's the link: https://github.com/falnatsheh/EcoGallery

mtbomb
  • 1,107
  • 1
  • 13
  • 16
  • Imported this into my workspace and played with it. Simple, clean, appears to work well. Thanks for this answer! – slogan621 Nov 19 '14 at 21:43
2

Current best solution is RecyclerView

1

There is a way to customize ViewPager for showing adjacent views but it not very simple. Check this - http://commonsware.com/blog/2012/08/20/multiple-view-viewpager-options.html.

If it will not help you may try HorizontallScrollView.

QArea
  • 4,955
  • 1
  • 12
  • 22
0

Have you tried coverflow? You can use this. https://github.com/Polidea/android-coverflow Hope this might help you.

or

You can also use page curl for swipping. https://github.com/moritz-wundke/android-page-curl

ByteWelder
  • 5,464
  • 1
  • 38
  • 45
Shadow
  • 6,864
  • 6
  • 44
  • 93
0

I think be a good idea uses the View Pager to replace the gallery.

ademar111190
  • 14,215
  • 14
  • 85
  • 114