61

I am using Jake's ViewPageIndicator and want to display Images like a swipe gallery. Any refernce link where i can get started. I have implemented the basic viewpager and now want to implement image viewpaper as below

enter image description here

Is it possible to to achieve using ViewPageIndicator ?

Harsha M V
  • 54,075
  • 125
  • 354
  • 529

9 Answers9

72

In Jake's ViewPageIndicator he has implemented View pager to display a String array (i.e. ["this","is","a","text"]) which you pass from YourAdapter.java (that extends FragmentPagerAdapter) to the YourFragment.java which returns a View to the viewpager.

In order to display something different, you simply have to change the context type your passing. In this case you want to pass images instead of text, as shown in the sample below:

This is how you setup your Viewpager:

public class PlaceDetailsFragment extends SherlockFragment {
    PlaceSlidesFragmentAdapter mAdapter;
    ViewPager mPager;
    PageIndicator mIndicator;

    public static final String TAG = "detailsFragment";

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_place_details,
                container, false);

        mAdapter = new PlaceSlidesFragmentAdapter(getActivity()
                .getSupportFragmentManager());

        mPager = (ViewPager) view.findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mIndicator = (CirclePageIndicator) view.findViewById(R.id.indicator);
        mIndicator.setViewPager(mPager);
        ((CirclePageIndicator) mIndicator).setSnap(true);

        mIndicator
                .setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                    @Override
                    public void onPageSelected(int position) {
                        Toast.makeText(PlaceDetailsFragment.this.getActivity(),
                                "Changed to page " + position,
                                Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onPageScrolled(int position,
                            float positionOffset, int positionOffsetPixels) {
                    }

                    @Override
                    public void onPageScrollStateChanged(int state) {
                    }
                });
        return view;
    }

}

your_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >



    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <com.viewpagerindicator.CirclePageIndicator
        android:id="@+id/indicator"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="10dip" />

</LinearLayout>

YourAdapter.java

public class PlaceSlidesFragmentAdapter extends FragmentPagerAdapter implements
        IconPagerAdapter {

    private int[] Images = new int[] { R.drawable.photo1, R.drawable.photo2,
            R.drawable.photo3, R.drawable.photo4

    };

    protected static final int[] ICONS = new int[] { R.drawable.marker,
            R.drawable.marker, R.drawable.marker, R.drawable.marker };

    private int mCount = Images.length;

    public PlaceSlidesFragmentAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return new PlaceSlideFragment(Images[position]);
    }

    @Override
    public int getCount() {
        return mCount;
    }

    @Override
    public int getIconResId(int index) {
        return ICONS[index % ICONS.length];
    }

    public void setCount(int count) {
        if (count > 0 && count <= 10) {
            mCount = count;
            notifyDataSetChanged();
        }
    }
}

YourFragment.java

// you need to return image instaed of text from here.//

public final class PlaceSlideFragment extends Fragment {
    int imageResourceId;

    public PlaceSlideFragment(int i) {
        imageResourceId = i;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        ImageView image = new ImageView(getActivity());
        image.setImageResource(imageResourceId);

        LinearLayout layout = new LinearLayout(getActivity());
        layout.setLayoutParams(new LayoutParams());

        layout.setGravity(Gravity.CENTER);
        layout.addView(image);

        return layout;
    }
}

You should get a View pager like this from the above code. enter image description here

Gareth Jones
  • 1,241
  • 4
  • 17
  • 38
suresh cheemalamudi
  • 6,190
  • 11
  • 49
  • 67
  • I've tried your code but the image isn't show up. what wrong ?? – Tai Dao May 19 '13 at 19:23
  • hi i tried this code too, i am getting problem in swiping as the image pager not working at all, it swipes parent pager only and images too are not displayed, what am i doing wrong? – mayank_droid Jun 05 '13 at 07:16
  • @mak_just4anything It seems you are using a view pager inside a view pager. I tried that couple of times but couldn't make it work. What i did was to have tabs and not the view pager for fragments. – suresh cheemalamudi Jun 05 '13 at 10:18
  • thanks, but i tried other way, by separate viewpagers, which solved my problem but another problem it came with was old fragments stays in pager, even you change the main tab. do u know about removing fragments on refreshing viewpager adapter? – mayank_droid Jun 05 '13 at 11:45
  • @mak_just4anything check out these links http://stackoverflow.com/questions/7723964/replace-fragment-inside-a-viewpager – suresh cheemalamudi Jun 06 '13 at 05:44
  • http://stackoverflow.com/questions/10849552/android-viewpager-cant-update-dynamically – suresh cheemalamudi Jun 06 '13 at 05:46
  • Hey @sureshcheemalamudi ! nice example, i hope you have written this tutorial on your blog as well please share that link so i will download that zip to use. – Sun Aug 21 '14 at 04:38
  • How dynamically loading images and display to viewpager android like flipkart android – Harsha Oct 13 '15 at 12:57
44

I made a library named AndroidImageSlider, you can have a try.

enter image description here

daimajia
  • 967
  • 12
  • 15
  • @daimajia i want simple viewpager image changing style in SliderLayout, its decreasing size of image that is going out and increasing size of image that is coming in front. I don't want this increase and decrese in size, can you suggest how can I achieve this? – nawaab saab Apr 06 '15 at 12:09
  • @daimajia I am using AndroidImageSlider. It works great. But the problem is that i want to invalidate the cache. As i am updating the images that are beeing shown. Is there a way to do this? – Rishabh Lashkari May 23 '16 at 12:46
  • god bless you for making this library . best library ever see for making image slider – Erfan Jan 11 '17 at 17:52
  • 1
    @daimajia i have used your library, its very awesome. Can you let me know, if i can use number, instead of indicators like 1 of 12 , 2 of 12 – Abdul Aleem Feb 27 '17 at 15:58
12

Image Viewer with ViewPager Implementation, check this project https://github.com/chiuki/android-swipe-image-viewer

Refer this discussion also Swiping images (not layouts) with viewpager

Community
  • 1
  • 1
kumar_android
  • 2,273
  • 1
  • 21
  • 30
4

Just use this https://gist.github.com/8cbe094bb7a783e37ad1 for make surrounding pages visible and http://viewpagerindicator.com/ this, for indicator. That's pretty cool, i'm using it for a gallery.

sztembi
  • 223
  • 2
  • 10
  • Do you have some kinda sample app which is implementing it so that i can reference it ? – Harsha M V Dec 10 '12 at 19:53
  • 2
    I didn't implement indicator, but in `ViewPager` gallery implementation you have to copy whole code to your project, change content in `instantiateItem` section from TextView to your ImageView. – sztembi Dec 10 '12 at 20:11
3

Hoho.. It should be very easy to use Gallery to implement this, using ViewPager is much harder. But i still encourage to use ViewPager since Gallery really have many problems and is deprecated since android 4.2.

In PagerAdapter there is a method getPageWidth() to control page size, but only by this you cannot achieve your target.

Try to read the following 2 articles for help.

multiple-view-viewpager-options

viewpager-container

faylon
  • 7,360
  • 1
  • 30
  • 28
2

you can use custom gallery control.. check this https://github.com/kilaka/ImageViewZoom use galleryTouch class from this..

Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
1

Hi if your are looking for simple android image sliding with circle indicator you can download the complete code from here http://javaant.com/viewpager-with-circle-indicator-in-android/#.VysQQRV96Hs . please check the live demo which will give the clear idea.

Nirmal Dhara
  • 2,121
  • 18
  • 27
0
enter code here   public Timer timer;
public TimerTask task;
public ImageView slidingimage;

private int[] IMAGE_IDS = {
        R.drawable.home_banner1, R.drawable.home_banner2, R.drawable.home_banner3
    };

enter code here   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_screen);

     final Handler mHandler = new Handler();

        // Create runnable for posting
        final Runnable mUpdateResults = new Runnable() {
            public void run() {

                AnimateandSlideShow();

            }
        };
        int delay = 2000; // delay for 1 sec.

        int period = 2000; // repeat every 4 sec.

        Timer timer = new Timer();

        timer.scheduleAtFixedRate(new TimerTask() {

        public void run() {

             mHandler.post(mUpdateResults);

        }

        }, delay, period);

enter code here     private void AnimateandSlideShow() {

        slidingimage = (ImageView)findViewById(R.id.banner);
        slidingimage.setImageResource(IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);

        currentimageindex++;

        Animation rotateimage = AnimationUtils.loadAnimation(this, R.anim.custom_anim);

          slidingimage.startAnimation(rotateimage);

    }
patel135
  • 927
  • 13
  • 19
0

A great One Image slider : https://github.com/daimajia/AndroidImageSlider Check it

TooCool
  • 10,598
  • 15
  • 60
  • 85