ViewPager 片段随着时间的推移而被破坏?

我正在使用 Android 的 support.v4 包开发一个包含多个 Fragment 的 ViewPager.

I am using Android's support.v4 package to develop a ViewPager containing multiple Fragments.

我试图在用户更改页面时隐藏视图.因此,我实现了一个 OnPageChangeListener 来监听我的适配器,并在发生页面更改时在我的 Fragment 中调用 update() 方法.但是我得到了一个 NullPointerException,我不知道为什么.

I am trying to hide the Views when the user changes page. So, I implemented an OnPageChangeListener to listen to my adapter, and call an update() method in my Fragments when when a page change occurs. But I'm getting a NullPointerException, and I cannot figure out why.

代码如下:

public class MyActivity extends FragmentActivity implements
        OnPageChangeListener {

    private static final int NUM_ITEMS = 2;
    private ViewPager mPager;
    private static SpaceAdapter mAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_pager);

        mAdapter = new SpaceAdapter(getSupportFragmentManager());
        mPager = (ViewPager) findViewById(R.id.pager);
        mPager.setAdapter(mAdapter);

        mPager.setOnPageChangeListener(this);
}

    public static class SpaceAdapter extends FragmentPagerAdapter {

        public SpaceAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
            case 0:
            return new MyFragment();
            case 1:
            return new MyFragment();
            default:
            return new MyFragment();
            }
        }
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
        Log.i("pagination", "scroll state changed: " + arg0);
}

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // Log.i("pagination", "page scroll: " + arg0 + " with: " + arg1 + ", " + arg2);
        if (mAdapter.getItem(arg0) instanceof IUpdateOnPage) {
            ((IUpdateOnPage) mAdapter.getItem(arg0)).updateOnPage();
        }
    }

    @Override
    public void onPageSelected(int arg0) {
        Log.i("pagination", "page selected: " + arg0);
    }
}

public class MyFragment extends Fragment implements
        SurfaceHolder.Callback, IUpdateOnPage {

    private SurfaceView charts;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.crew, null);
        bindViews(v);
        return v;
    }

    private void bindViews(View v) {
        charts = (SurfaceView) v.findViewById(R.id.civ_charts);

        charts.getHolder().addCallback(this);
        Log.i("wtf", "huh " + String.valueOf(charts == null)); // logs false
    }

    @Override
    public void updateOnPage() {
        if (charts != null) {
            Log.i("crew surface", "hidden");
            charts.setVisibility(View.GONE);
        } else {
            Log.i("crew surface", "charts is null");  //THIS HERE gets logged
        }
    }
}

w当我复制/粘贴时,我删除了 SurfaceHolder.Callback 方法,但它们在那里.bindViews(v) 方法记录如果 charts == null,并且总是返回 false.当我页面时,updateOnPage 被调用,charts == null 返回 true.为什么会发生这种情况,我该如何解决?

wWhen I copy/pasted I removed the SurfaceHolder.Callback methods, but they are there. The bindViews(v) method logs if charts == null, and that always returns false. When I page, updateOnPage gets called, and charts == null returns true. Why does this happen and how do I get around it?

这是第 108 行:

charts.setVisibility(View.GONE);

推荐答案

在初始化你的ViewPager时添加以下内容,

Add the following when you initialize your ViewPager,

mPager.setOffscreenPageLimit(NUM_ITEMS-1);

在您的情况下,这会将屏幕外页面的数量设置为 1(即,当另一个页面当前处于焦点时,它将在内存中保留额外的屏幕外页面).这将强制您的 ViewPager 保留所有 Fragment,即使它们不在焦点上.

In your case, this will set the number of off-screen pages to 1 (i.e. it will keep the additional off-screen page in memory when the other page is currently in focus). This will force your ViewPager to keep all of the Fragments even when they are not in focus.

相关文章