简体   繁体   English

ViewPager没有显示正确的页面

[英]ViewPager does not show correct page

I have a viewPager and I add fragments inside it. 我有一个viewPager,并在其中添加了片段。 At first I have 4 fragments, showing the last one. 首先,我有4个片段,显示最后一个片段。 5, 6, 7 ,8 When I reach the first fragment. 5、6、7、8当我到达第一个片段时。 I make a Api Call, get the data for another 4 earlier fragments: 1,2, 3, 4. And I set the last one as vizible. 我进行了一次Api呼叫,获取了另外4个早期片段的数据:1、2、3、4。我将最后一个片段设置为可见。 My issue is that going through them. 我的问题是经历它们。 I get this: 8, 7 ,6 ,5, 8, 7, 6, 5. If I exit and reenter the fragment, it works fine. 我得到了:8、7、6、5、8、7、6、5。如果我退出并重新输入片段,它可以正常工作。 but always the first time, it seems to have the other fragments cached somehow? 但总是第一次,似乎其他片段以某种方式被缓存了?

This is how I create the fragments: 这就是我创建片段的方式:

 private List<PSActivityFragment> getFragments(){
    UserRecord profile = UserRecord.getMyUser(PSProfileActivity.this);
    CrudStateCallback crudStateCallback = new CrudStateCallback() {
        @Override
        public void onResponse(String string) {
        }
    };
    List<PSActivityFragment> fList = new ArrayList<PSActivityFragment>();
    if(profile != null) {
        loader.setVisibility(View.GONE);
        if(profile.getMonths().size() > 0) {
            for (PSUserActivityMonth month : profile.getMonths()){
                Log.i("","month is fragments created are:" + month);
                PSActivityFragment psaf1 = new PSActivityFragment();
                psaf1.init(month, crudStateCallback);
                fList.add(psaf1);
            }
        }
    }else{
        loader.setVisibility(View.VISIBLE);
    }
    return fList;

}

Each time I get new data. 每次我获得新数据。 I recreate the adapter: 我重新创建适配器:

  pager.removeAllViews();
    pager = null;
    pager = (ViewPager) findViewById(R.id.viewpager);
    final List<PSActivityFragment> fragments = getFragments();
    myPagerAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
    pager.setAdapter(myPagerAdapter);

I tried instead of recreating and setting the adapter again to just set the fragments to the adapter again, and call notifyDataSetChanged. 我尝试过重新创建并重新设置适配器,而只是重新将片段设置为适配器,然后调用notifyDataSetChanged。 But still, I get the same issue. 但是,我仍然遇到同样的问题。 Are the fragments cached somewhere? 碎片是否缓存在某个地方? why does this happen? 为什么会这样? I checked the log and the fragments are created in the normal order: 我检查了日志,并以正常顺序创建了片段:

 06-02 14:16:21.514: I/(14192): month is fragments created are:PSUserActivityMonth{category='u/23', month=11, year=2015, weeks=1}
 06-02 14:16:21.515: I/(14192): month is fragments created are:PSUserActivityMonth{category='u/23', month=12, year=2015, weeks=5}
 06-02 14:16:21.515: I/(14192): month is fragments created are:PSUserActivityMonth{category='u/23', month=1, year=2016, weeks=5}
 06-02 14:16:21.515: I/(14192): month is fragments created are:PSUserActivityMonth{category='u/23', month=2, year=2016, weeks=5}
 06-02 14:16:21.515: I/(14192): month is fragments created are:PSUserActivityMonth{category='u/23', month=3, year=2016, weeks=5}
 06-02 14:16:21.516: I/(14192): month is fragments created are:PSUserActivityMonth{category='u/23', month=4, year=2016, weeks=5}
 06-02 14:16:21.516: I/(14192): month is fragments created are:PSUserActivityMonth{category='u/23', month=5, year=2016, weeks=6}
 06-02 14:16:21.516: I/(14192): month is fragments created are:PSUserActivityMonth{category='u/23', month=6, year=2016, weeks=1}

These are months: I should see after cycling through the pager this: june 16, may 16 , april 16 , march 16 (first for) and then feb 16, jan 16, dec 15, nov15. 这些是几个月:在浏览完寻呼机后,我应该看到:6月16日,5月16日,4月16日,3月16日(首先),然后是2月16日,1月16日,12月15日,11月15日。 Instead I see: june 16, may 16 , april 16 , march 16 (first for) and then june 16, may 16 , april 16 , march 16 again 相反,我看到:6月16日,5月16日,4月16日,3月16日(首先),然后又是6月16日,5月16日,4月16日,3月16日

Adapter code: 适配器代码:

public class MyPageAdapter extends FragmentPagerAdapter {

private List<PSActivityFragment> fragments;


public MyPageAdapter(FragmentManager fm, List<PSActivityFragment> fragments) {
    super(fm);
    this.fragments = fragments;
}

@Override

public Fragment getItem(int position) {
    return this.fragments.get(position);
}


@Override

public int getCount() {
    return this.fragments.size();
}

}

PS: My activity extends fragmentActivity, and I ma using a getSupportFragmentManager as you can see when I create the adapter. PS:我的活动扩展了fragmentActivity,并且我在创建适配器时使用了getSupportFragmentManager。 Could this be the issue? 这可能是问题吗?

using a Activity with FragmentActivity and getSupportFragmentManager. 使用带有FragmentActivity和getSupportFragmentManager的Activity。 I thought that that might be the issue so I did this: 我认为可能是问题所在,所以我这样做了:

 if(getSupportFragmentManager() != null && getSupportFragmentManager().getFragments() != null){
        getSupportFragmentManager().getFragments().clear();
    }

And now it works!!! 现在就可以了!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM