【问题标题】:Manage Fragment , public static viewpager管理片段,公共静态浏览器
【发布时间】: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 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. 由于fragment的类和方法是静态的,所以它需要BitmapDrawable数组是静态的,并且它可以保存一些图像。当Activity被销毁以释放内存时,可以将BitmapDrawable数组设为'null'吗? (片段被重新填充并被其他活动使用)
  2. 我的最后一个代码不需要静态类或静态数组。保留已弃用的代码是否可行?
  3. 将代码保留在已弃用的版本中意味着什么?

提前,比你花时间和精力。

【问题讨论】:

    标签: android android-fragments static instantiation deprecated


    【解决方案1】:

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

    关于静态 Drawable 引用可能遇到的问题,有一个相当古老但 still relevant article 的内容。 Drawables 有对 Views 的引用,Views 有对 Activity 的引用。通过使用静态 Drawable 引用,您可以轻松地泄漏 Activity 上下文。如果您同时支持垂直和纵向,您的 Activity 将在每次设备旋转时被销毁并重新创建。在这种情况下,您需要注意如何保留已加载的位图(您只想在用户离开 Activity 时清除数组,而不是在他们旋转时)。

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

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

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

    下面是一些(未经测试的)示例代码,它们实现了 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);
        }
    }
    

    【讨论】:

    • 我的原始代码与您发布的完全相同。谢谢你的文章。我将在这个案例中保留旧代码,并在主导航菜单中使用新方法进行一些实验。谢谢!
    猜你喜欢
    • 2017-11-15
    • 2017-12-04
    • 2010-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多