androidandroid-viewpagerandroid-pageradaptertouchimageview

PagerAdapter with TouchImageView or GestureImageView not loading images


When i am using simple imageview or AspectRatioImageView I am able to load images fetched from server. But if I am using TouchImageView or GestureImageView it is not able to load images. I see a blank screen.

Apart from using UIL,I have tried some other ways of loading images also but same result.

However when i have opened a view in viewpager and its not showing image..then if i switch to landscape mode and then again to portrait mode then it is able to load image..If i call notifyDataSetChanged() anywhere inside adapter it crashes showing InflateException

xml

<com.polites.android.GestureImageView
        android:id="@+id/imagefullscreen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerVertical="true"
        android:layout_gravity="center"/>

Activity

pager.setAdapter(imagePagerAdapter = new ImagePagerAdapter(ImagePagerActivity.this, posts));
pager.setCurrentItem(pos);
PageListener pageListener = new PageListener();
pager.setOnPageChangeListener(pageListener);

imagePagerAdapter.notifyDataSetChanged();

Adapter

public ImagePagerAdapter(Activity activity,
                         List<Post> object) {
    this._activity = activity;
    posts = object;

    inflater = (LayoutInflater) _activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    options = new DisplayImageOptions.Builder()
            .showImageOnLoading(android.R.color.transparent)
            .showImageForEmptyUri(android.R.color.transparent)
            .showImageOnFail(android.R.color.transparent)
            .cacheInMemory(true)
            .cacheOnDisk(true)
            .considerExifParams(true)
            .bitmapConfig(Bitmap.Config.RGB_565)
            .build();
    imageLoader = ImageLoader.getInstance();
    imageLoader.init(ImageLoaderConfiguration.createDefault(_activity));

}

@Override
public int getCount() {
    return posts.size();
}

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

@Override
public Object instantiateItem(ViewGroup container, final int position) {
    final Post post = posts.get(position);
    View viewLayout = inflater.inflate(R.layout.item_pager_image, container,
            false);
    visualizerView = (VisualizerView) viewLayout.findViewById(R.id.visualizerView);


    image = (GestureImageView) viewLayout.findViewById(R.id.imagefullscreen);
    mGDescription = (TextView) viewLayout.findViewById(R.id.guggu_pager_description);
    image.setVisibility(View.VISIBLE);

    mGDescription.setVisibility(View.VISIBLE);

    play = (ImageView) viewLayout.findViewById(R.id.play);
    pause = (ImageView) viewLayout.findViewById(R.id.pause);
imageLoader.displayImage(url,images, options);
        pause.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //TODO:pausing audio/video here
            }
        });
    }
    container.addView(viewLayout);
    viewLayout.setTag("my" + position);
    return viewLayout;
}

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

@Override
public int getItemPosition(Object object) {
    return POSITION_NONE;
}

Solution

  • The issue was because TouchImageView or GestureImageView is taking time to load image from server and view was not updated after image is being loaded.

    I added-

    private class PageListener extends ViewPager.SimpleOnPageChangeListener
        {
            public void onPageSelected(int position)
            {
    
    
                imagePagerAdapter.notifyDataSetChanged();
            }
        }
    

    inside activity class so that view is updated whenever user swipe to new page.But with this the first view that I was opening was still showing blank..so i added

    h.postDelayed(r=new Runnable()
        {
            @Override
            public void run() {
                if (mPosition == position)
                {
                    notifyDataSetChanged();
                    h.removeCallbacks(r);
    
    
                }
            }
        }, 1000);
    

    inside instantiateItem of adapter so that first view is updated after 1 second when view is created as by that time image is fetched from server.mPosition is the position of first view that is being opened.

    This completely solves my issue.