Android Open Source - Joetz-Android-V2 Image Pager Fragment

From Project

Back to project page Joetz-Android-V2.


The source code is released under:

GNU General Public License

If you think the Android project Joetz-Android-V2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.jens.myapplication.camps;
// ww w  .  j a  va  2s .co  m
import android.os.Bundle;
import android.os.Parcelable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.example.jens.myapplication.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

 * Created by Jens on 19/11/2014.
public class ImagePagerFragment extends Fragment {

    public static final String IMAGE_POSITION ="image_postion" ;
    public static final String IMAGE_ARRAY ="image_array" ;

    int[] imageUrls;
    int position;

    DisplayImageOptions options;

    public void onCreate(Bundle savedInstanceState) {

        options = new DisplayImageOptions.Builder()
                .displayer(new FadeInBitmapDisplayer(300))

        imageUrls = getActivity().getIntent().getExtras().getIntArray(IMAGE_ARRAY);
        position = getActivity().getIntent().getExtras().getInt(IMAGE_POSITION);

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_image_pager, container, false);
        ViewPager pager = (ViewPager) rootView.findViewById(;
        pager.setAdapter(new ImageAdapter());

        return rootView;

    private class ImageAdapter extends PagerAdapter {

        private LayoutInflater inflater;

        ImageAdapter() {
            inflater = LayoutInflater.from(getActivity());

        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);

        public int getCount() {
            return imageUrls.length;

        public Object instantiateItem(ViewGroup view, int position) {
            View imageLayout = inflater.inflate(R.layout.item_pager_image, view, false);
            assert imageLayout != null;
            ImageView imageView = (ImageView) imageLayout.findViewById(;
            final ProgressBar spinner = (ProgressBar) imageLayout.findViewById(;

            ImageLoader.getInstance().displayImage(""+Integer.toString(imageUrls[position]), imageView, options, new SimpleImageLoadingListener() {
                public void onLoadingStarted(String imageUri, View view) {

                public void onLoadingFailed(String imageUri, View view, FailReason failReason) {
                    String message = null;
                    switch (failReason.getType()) {
                        case IO_ERROR:
                            message = "Input/Output error";
                        case DECODING_ERROR:
                            message = "Image can't be decoded";
                        case NETWORK_DENIED:
                            message = "Downloads are denied";
                        case OUT_OF_MEMORY:
                            message = "Out Of Memory error";
                        case UNKNOWN:
                            message = "Unknown error";
                    Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();


                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

            view.addView(imageLayout, 0);
            return imageLayout;

        public boolean isViewFromObject(View view, Object object) {
            return view.equals(object);

        public void restoreState(Parcelable state, ClassLoader loader) {

        public Parcelable saveState() {
            return null;

Java Source Code List