2014-04-28 75 views
3

我需要一些建议。 我有一个ViewPager的片段。我用它作为一个有几张图片的画廊。图像从网站加载并存储在位图阵列中。管理片段,公共静态viewpager

起初我用..

public class GalleryPageAdapter extends PagerAdapter { 

public GalleryPageAdapter(){ 

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

但是,instatiateItem等方法现在已过时...... 我做了一些研究的跟随其他职位 Class extending Fragment - error says make sure class name exists, is public, and has an empty constructor that is publicUnable to instantiate fragment make sure class name exists, is public, and has an empty constructor that is public

现在,它的工作原理没有错误

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; 
    } 

    } 

而且,一如既往,这里是我的具体问题

  1. 由于类和片段的方法是静态的,它需要BitmapDrawable数组是静态的,它拥有几images.It是罚款,使BitmapDrawable阵列“空”时活动被破坏以释放内存? (片段被重新填充并被其他活动使用)
  2. 我最后的代码不需要静态类或静态数组。保持代码不变,它被弃用是好的吗?
  3. 这意味着将代码保留在不推荐的版本中?

提前,比你的时间和注意。

回答

1

我认为你应该回到这个用例的原始代码 - 如果你只是想展示一个图片库,那么基于Views而不是Fragments来构建ViewPager会简单得多。当您为每个页面设置更复杂的屏幕时,您通常会使用Fragments。如果ViewPager是顶级导航用户界面。

对于静态可绘制引用可能会遇到的问题,存在一个相当古老但still relevant article。 Drawables引用了Views,它引用了一个Activity。通过使用静态Drawable引用,您可以轻松地泄漏Activity上下文。如果您同时支持纵向和纵向方向,则每次旋转设备时,您的活动都会被破坏并重新创建。在这种情况下,您需要小心如何保留加载的位图(您只需要在用户离开活动时清除数组,而不是在旋转时清除数组)。

的PagerAdapter类实际上具有instantiateItem方法的两个版本:

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

原始的(#1)有利于所述第二方法被弃用。当你重写instantiateItem时,你通常需要将你的View添加到容器中。使用第一个方法签名,首先需要在添加之前将您的容器转换为ViewGroup。第二个方法签名通过从一开始就给你一个ViewGroup来避免这种情况。 PagerAdapter中有几个类似的不推荐使用的方法 - 它们都具有取代View的ViewGroup的等效版本。

这里是实现PagerAdapter显示图像的一些(未经测试)的示例代码:

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); 
    } 
} 
+0

我原来的代码是完全按照你贴一个。 感谢您的文章。 我会保留这种情况下的旧代码,并在主导航菜单中使用新方法进行一些实验。 谢谢! – UrielUVD