Search code examples
androidandroid-viewpagerandroid-galleryandroid-volley

Android View Pager Gallery doesn't display data at the first time


I'm creating a simple ViewPager gallery for my App. here's the code that I've implemented so far.

this is my xml file

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:orientation="vertical"
    tools:context="com.example.newgallery.MainActivity" >

     <android.support.v4.view.ViewPager
         android:id="@+id/view_pager"
         android:layout_width="match_parent"
         android:layout_height="240dp" />

</LinearLayout>

and this is my java file

    public class MainActivity extends Activity {

    private Bitmap[] IMAGES;
    Bitmap bit1,bit2,bit3,bit4;
    String getid, url1,url2,url3,url4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        getid = "14";
        url1 = "http://example.com/dom/uploads/"+getid+"/img1.jpg";
        url2 = "http://example.com/dom/uploads/"+getid+"/img2.jpg";
        url3 = "http://example.com/dom/uploads/"+getid+"/img3.jpg";
        url4 = "http://example.com/dom/uploads/"+getid+"/img4.jpg";

        bit1 = LoadImages(url1);
        bit2 = LoadImages(url2);
        bit3 = LoadImages(url3);
        bit4 = LoadImages(url4);

        IMAGES = new Bitmap[]{bit1,bit2,bit3,bit4};


        ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
        ImageAdapter adapter = new ImageAdapter();
        viewPager.setAdapter(adapter);




    }

    class ImageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return IMAGES.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == ((ImageView) object);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView imageView = new ImageView(getApplicationContext());
            imageView.setImageBitmap(IMAGES[position]);
            ((ViewPager) container).addView(imageView, 0);
            return imageView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((ImageView) object);
        }
    }

    Bitmap result;
    public Bitmap LoadImages(String url){
        ImageLoader imageLoader = AppController.getInstance().getImageLoader();
        imageLoader.get(url, new ImageListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Bitmap drawimg = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
                        result = drawimg;
                    }

                    @Override
                    public void onResponse(ImageContainer response, boolean arg1) {
                        if (response.getBitmap() != null) {
                            result = response.getBitmap();
                        }
                    }
                });
        return result;
    }
}

and here's my Logcat. All the links are working fine. here I changed the original domain to example.com

09-02 19:36:35.568: D/Volley(630): [76] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://example.com/dom/uploads/14/img2.jpg 0x90e75755 LOW 2> [lifetime=3027], [size=107746], [rc=200], [retryCount=0]
09-02 19:36:35.568: D/Volley(630): [74] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://example.com/dom/uploads/14/img1.jpg 0x90e75755 LOW 1> [lifetime=3075], [size=89388], [rc=200], [retryCount=0]
09-02 19:36:35.798: D/dalvikvm(630): GC_FOR_ALLOC freed 281K, 4% free 19781K/20487K, paused 84ms, total 85ms
09-02 19:36:35.808: I/dalvikvm-heap(630): Grow heap (frag case) to 20.002MB for 544016-byte allocation
09-02 19:36:35.918: D/dalvikvm(630): GC_FOR_ALLOC freed <1K, 4% free 20312K/21063K, paused 101ms, total 101ms
09-02 19:36:36.028: D/dalvikvm(630): GC_CONCURRENT freed <1K, 4% free 20312K/21063K, paused 30ms+22ms, total 100ms
09-02 19:36:36.108: D/Volley(630): [1] Request.finish: 4019 ms: [ ] http://example.com/dom/uploads/14/img3.jpg 0x90e75755 LOW 3
09-02 19:36:36.218: D/dalvikvm(630): GC_FOR_ALLOC freed 18K, 4% free 20313K/21063K, paused 69ms, total 69ms
09-02 19:36:36.228: I/dalvikvm-heap(630): Grow heap (frag case) to 20.522MB for 544016-byte allocation
09-02 19:36:36.318: D/dalvikvm(630): GC_FOR_ALLOC freed 0K, 4% free 20845K/21639K, paused 84ms, total 84ms
09-02 19:36:36.428: D/dalvikvm(630): GC_CONCURRENT freed 1K, 4% free 20845K/21639K, paused 30ms+9ms, total 98ms
09-02 19:36:36.568: D/Volley(630): [1] Request.finish: 4472 ms: [ ] http://example.com/dom/uploads/14/img4.jpg 0x90e75755 LOW 4
09-02 19:36:36.638: D/dalvikvm(630): GC_FOR_ALLOC freed 20K, 4% free 20845K/21639K, paused 63ms, total 64ms
09-02 19:36:36.648: I/dalvikvm-heap(630): Grow heap (frag case) to 21.041MB for 544016-byte allocation
09-02 19:36:36.728: D/dalvikvm(630): GC_FOR_ALLOC freed 0K, 4% free 21376K/22215K, paused 77ms, total 77ms
09-02 19:36:36.908: D/Volley(630): [1] Request.finish: 4813 ms: [ ] http://example.com/dom/uploads/14/img2.jpg 0x90e75755 LOW 2
09-02 19:36:36.978: D/Volley(630): [1] Request.finish: 4897 ms: [ ] http://example.com/dom/uploads/14/img1.jpg 0x90e75755 LOW 1

So my problem is when I'm trying to run my App for the first time it doesn't load images. i get 4 white pages. but if I exit the program and run it again, I can see all the images are loaded just fine.

enter image description here

After that i just press back button and start the app again by clicking on the launcher icon.

enter image description here

What did I miss here? Can anyone please explain me the reason for this?


Solution

  • Well this is not the exact answer for the above question. But I found an alternative. it's called Universal Image Loader for Android. Using this library I managed to survive the situation.

    enter image description here