【发布时间】:2014-04-28 21:00:08
【问题描述】:
我需要一些建议。 我有一个带有 ViewPager 的片段。我将它用作带有一些图像的画廊。图片从网络加载并存储在 Bitmap 数组中。
一开始我用..
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 public 和 Unable 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;
}
}
和往常一样,这是我的具体问题
- 由于fragment的类和方法是静态的,所以它需要BitmapDrawable数组是静态的,并且它可以保存一些图像。当Activity被销毁以释放内存时,可以将BitmapDrawable数组设为'null'吗? (片段被重新填充并被其他活动使用)
- 我的最后一个代码不需要静态类或静态数组。保留已弃用的代码是否可行?
- 将代码保留在已弃用的版本中意味着什么?
提前,比你花时间和精力。
【问题讨论】:
标签: android android-fragments static instantiation deprecated