Domanda

I need some advice on this. I have a Fragment with a ViewPager. I use it as a gallery with a few images. The images are loaded from web and stored on a Bitmap array.

At first i used..

public class GalleryPageAdapter extends PagerAdapter {

public GalleryPageAdapter(){

}
  public Object instantiateItem(View collection, int position) {
  }
 ...
//all other methods
}

but, instatiateItem and other methods are now deprecated... I made some research an followed other posts Class extending Fragment - error says make sure class name exists, is public, and has an empty constructor that is public and Unable to instantiate fragment make sure class name exists, is public, and has an empty constructor that is public

Now it works without error

public class sEstablecimiento extends android.support.v4.app.FragmentActivity{
static BitmapDrawable iconos[];
//load bitmaps into iconos[] from web

static class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
    static int position;
    public ScreenSlidePagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        this.position=position;
        return new ScreenSlidePageFragment();
    }

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

}

public static class ScreenSlidePageFragment extends Fragment {
    private BitmapDrawable image;

    public ScreenSlidePageFragment() {
       image=iconos[ScreenSlidePagerAdapter.position];
    }

    @Override
    public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                             Bundle savedInstanceState) {
        ViewGroup rootView =(ViewGroup)inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
        ImageView icono=(ImageView)rootView.findViewById(R.id.imagen);
        icono.setImageDrawable(iconos[ScreenSlidePagerAdapter.position]);
        return rootView;
    }

  }

And, as always, here are my specific questions

  1. As the class and methods of the fragment are static, it needs the BitmapDrawable array to be static and it holds a few images.It is fine to make the BitmapDrawable array 'null' when the activity is destroyed to free the memory? (the fragment is refilled and used by other activities)
  2. My last code do not need static classes or static array. It is Fine to keep the code as it is altought it is deprecated?
  3. What implies keep the code in the deprecated version?

in advance, than you for your time and atention.

È stato utile?

Soluzione

I think you should go back to your original code for this use case - if you just want to show a gallery of images, it is much simpler to build a ViewPager based on Views rather than Fragments. You would typically use Fragments when you have more complicated screens for each page, e.g. if the ViewPager was the top-level navigation UI.

There is a pretty old but still relevant article on the problems you can run into with static Drawable references. Drawables have references to Views, which have references to an Activity. By using static Drawable references so you can easily leak the Activity context. If you support both vertical and portrait orientations, your Activity will be destroyed and recreated every time the device is rotated. You would need to be careful about how to keep your loaded bitmaps in this case (you only want to clear the array when the user is leaving the Activity, not when they are rotating).

The PagerAdapter class actually has two versions of the instantiateItem method:

  1. public Object instantiateItem (View container, int position)
  2. public Object instantiateItem (ViewGroup container, int position)

The original (#1) is deprecated in favor of the second method. When you override instantiateItem you generally need to add your View to the container. With the first method signature, you would first need to cast your container to a ViewGroup before adding. The second method signature avoids this by giving you a ViewGroup from the start. There are several similarly deprecated methods in PagerAdapter - all of them have equivalent versions that take a ViewGroup instead of a View.

Here is some (untested) sample code that implements a PagerAdapter to display images:

public class GalleryPagerAdapter extends PagerAdapter {

    private Context mContext;
    private BitmapDrawable[] mIcons;

    public GalleryPagerAdapter(Context context, BitmapDrawable[] icons) {
        mContext = context;
        mIcons = icons;
    }

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

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

    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        // You can inflate a layout here instead and apply styling to the ImageView
        ImageView imageView = new ImageView(mContext);

        BitmapDrawable icon = mIcons[position];
        imageView.setImageDrawable(icon);

        container.addView(imageView, 0);
        return imageView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top