ViewPager片段被破坏掉了一段时间

ViewPager片段被破坏掉了一段时间

本文介绍了ViewPager片段被破坏掉了一段时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Android的 support.v4 包来开发一个包含多个ViewPager 片段秒。

我试图隐藏视图,当用户更改页面。所以,我实现了一个 OnPageChangeListener 来听我的适配器,在我的<$ C叫更新()方法$ C>片段 S当一个网页发生变化的时候。但我发现了一个 NullPointerException异常,我想不通这是为什么。

下面是code:

 公共类MyActivity扩展FragmentActivity工具
        OnPageChangeListener {

    私有静态最终诠释NUM_ITEMS = 2;
    私人ViewPager mPager;
    私有静态SpaceAdapter mAdapter;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.fragment_pager);

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

        mPager.setOnPageChangeListener(本);
}

    公共静态类SpaceAdapter扩展FragmentPagerAdapter {

        公共SpaceAdapter(FragmentManager FM){
            超(FM);
        }

        @覆盖
        公众诠释getCount将(){
            返回NUM_ITEMS;
        }

        @覆盖
        公共片段的getItem(INT位置){
            开关(位置){
            情况下0:
            返回新MyFragment();
            情况1:
            返回新MyFragment();
            默认:
            返回新MyFragment();
            }
        }
    }

    @覆盖
    公共无效onPageScrollStateChanged(INT为arg0){
        Log.i(分页,滚动状态改为:+为arg0);
}

    @覆盖
    公共无效onPageScrolled(INT为arg0,浮动ARG1,ARG2 INT){
        // Log.i(分页,页面滚动:+为​​arg0 +与+ ARG1 +,+ ARG2);
        如果(mAdapter.getItem(arg0中)的instanceof IUpdateOnPage){
            ((IUpdateOnPage)mAdapter.getItem(arg0中))updateOnPage()。
        }
    }

    @覆盖
    公共无效onPageSelected(INT为arg0){
        (选定的页面:分页,+为arg0)Log.i;
    }
}
 

 公共类MyFragment扩展片段工具
        SurfaceHolder.Callback,IUpdateOnPage {

    私人SurfaceView图;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
    }

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        视图V = inflater.inflate(R.layout.crew,NULL);
        bindViews(五);
        返回伏;
    }

    私人无效bindViews(视图v){
        图表=(SurfaceView)v.findViewById(R.id.civ_charts);

        。charts.getHolder()的addCallback(本);
        Log.i(跆拳道,呵呵+将String.valueOf(图== NULL)); //日志假
    }

    @覆盖
    公共无效updateOnPage(){
        如果(图!= NULL){
            Log.i(剧组面,隐藏);
            charts.setVisibility(View.GONE);
        } 其他 {
            Log.i(剧组面,图表为空); //这个在这里得到记录
        }
    }
}
 

w当我复制/粘贴我删除了 SurfaceHolder.Callback 方法,但它们的存在。该 bindViews(V)方法日志,如果图表== NULL ,并且始终返回false。当我页, updateOnPage 被调用,而图表== NULL 返回true。为什么会这样?我如何围绕它得到什么呢?

这是行108:

  charts.setVisibility(View.GONE);
 

解决方案

添加以下时,初始化 ViewPager

  mPager.setOffscreenPageLimit(NUM_ITEMS-1);
 

在你的情况,这将设置的屏幕外的页面1(即它将保持额外的离屏页面的内存时,另一个页面当前处于焦点的)的数量。这将迫使你的 ViewPager 保留所有的片段第即使他们没有焦点。

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

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.

Here is the code:

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

and

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

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?

and this is line 108:

charts.setVisibility(View.GONE);
解决方案

Add the following when you initialize your ViewPager,

mPager.setOffscreenPageLimit(NUM_ITEMS-1);

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.

这篇关于ViewPager片段被破坏掉了一段时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-02 01:24