android - 管理 fragment ,公共(public)静态 viewpager

标签 android android-fragments static instantiation deprecated

我需要一些这方面的建议。 我有一个带有 ViewPager 的 fragment 。我将它用作带有一些图像的画廊。图像从网络加载并存储在位图数组中。

一开始我用..

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”可以吗? (该 fragment 被其他 Activity 重新填充和使用)
  2. 我最后的代码不需要静态类或静态数组。保留代码是否可以,因为它被认为已被弃用?
  3. 什么意味着将代码保留在已弃用的版本中?

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

最佳答案

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

有一个相当古老但still relevant article关于静态 Drawable 引用可能遇到的问题。 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 时,您通常需要将 View 添加到容器中。使用第一个方法签名,您首先需要在添加之前将容器转换为 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);
    }
}

关于android - 管理 fragment ,公共(public)静态 viewpager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23351074/

相关文章:

android - 在 ViewPager 中旋转后选项卡内容消失

应用恢复时的 android.support.v4.app.Fragment.setUserVisibleHint 空指针

c++ - 通过引用传递指针 C++

Java更改另一个类中的静态变量不会影响对象值

redirect - Nginx 子域重定向到静态 URL

java - 具有两个索引的数据结构

android - Android 上的 DllNotFoundException

java - Android Fragment 实际上没有被替换

android - 双簧管/AAudio 播放流中的欠载

java - Android fragment 错误 : No view found for id 0x7f08009e (com. example.myapplication:id/preferenceFragment) fragment