ITKeyword,专注技术干货聚合推荐

注册 | 登录

解决android - The fragments in the viewpager tablayout keep reinstanting every time I reselect the tab

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

推荐:Android TabLayout、ViewPager实现顶部和底部Tab导航 点击滑动切换Tab页面

Android TabLayout、ViewPager实现顶部和底部Tab导航 点击滑动切换Tab页面 android design library提供的TabLayout android studio 依赖 compile 'com.android

I have three tabs and when the user goes to the third tab and then back to the first tab - the things in the first tab are not reset and the form keeps reloading.

This is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.scorespagemain);
    viewPager = (ViewPager) findViewById(R.id.viewPager);
    CAdapter cAdapter = new Adapter(getSupportFragmentManager(), ScoresPageMain.this);
    viewPager.setAdapter(cAdapter);
    tabLayout = (TabLayout) findViewById(R.id.tabsstuff);
    tabLayout.setupWithViewPager(viewPager);
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }
    });
}

}

CAdapter class:

public class CAdapter extends FragmentPagerAdapter{
    String names[] = new String[] {"Top Teams", "Top Scores", "Top Players"};
    Context context;

    public CAdapter(FragmentManager fragmentManager, Context context){
        super(fragmentManager);
        this.context = context;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case 0:
                return new TopTeams();
            case 1:
                return new TopScores();
            case 2:
                return new TopPlayers();
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return names.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return names[position];
    }
}
android android-fragments android-viewpager android-tablayout
|
  this question
asked Feb 29 '16 at 13:05 Lukazs Pioetrszci 80 7 2   Use - mViewPager.setOffscreenPageLimit(int pages); where the parameter int refers to howmany pages you want to keep in memory –  Skynet Feb 29 '16 at 13:08      No need to do anything in the tabUnSelected() and tabReSelected(). –  user4584887 Feb 29 '16 at 13:09

 | 

1 Answers
1

解决方法

Try to use setOffscreenPageLimit() in your code.

推荐:android material design之Tablayout,Recyclerview,Fragment,Viewpager搭配使用(四)

我的项目源码托管地址:点击打开我的项目源码地址 相信大家都用过fragment+viepager的组合,也都比较熟悉,现在说一下美拍,暴风,爱奇艺的高仿实现,这个实用性

Hope this will help you.


|
  this answer
edited Feb 29 '16 at 13:10 Rohit5k2 10.7k 4 15 39 answered Feb 29 '16 at 13:09 Silvans Solanki 874 1 8 21      Where do I put that? Like in what class or method? –  Lukazs Pioetrszci Feb 29 '16 at 13:10 2   After viewPager.setAdapter(cAdapter);. use viewPager.setOffscreenPageLimit(2) –  Rohit5k2 Feb 29 '16 at 13:10      In your activity class where you are initialize the tab adapter. –  Silvans Solanki Feb 29 '16 at 13:11      Wow, it works - thank you. Could please explain what that line setOffscreenPageLimit() does? –  Lukazs Pioetrszci Feb 29 '16 at 13:14      @LukazsPioetrszci Read this developer.android.com/reference/android/support/v4/view/… –  Rohit5k2 Feb 29 '16 at 13:15  |  show more comments

推荐:Android TabLayout定制CustomView与ViewPager交互双向联动

 Android TabLayout定制CustomView与ViewPager交互双向联动 我之前写了一些关于Android Material Design TabLayout的文章, (1)《Android L、M扩展支持包des


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。