本文介绍了当我单击NavigationView项时,如何在tablayout下更改viewpager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

当我单击NavigationView中的项目时,UI会通过调用以下"setUpTab"方法将viewpager中的片段替换为其他新片段.核心代码是:

When I click the item in NavigationView, the UI replace the fragment in viewpager with other new fragment by call the following "setUpTab" method. The core codes is :

public void setUpTab(int id){
    adapter.clear();
    switch (id){
        case R.id.nav_home:
            adapter.addFragment(new EventFragment(),"Events");
            adapter.addFragment(new OwnRepoFragment(),"Repository");
            adapter.addFragment(new ContributeRepoFragment(),"Contribute");
            break;
        case R.id.nav_friends:
            adapter.addFragment(new FollowerFragment(),"Follower");
            adapter.addFragment(new FollowingFragment(),"Following");
            break;
        case R.id.nav_gist:
            break;
        case R.id.nav_setting:
            break;
        case R.id.nav_about:
            break;
    }
    adapter.notifyDataSetChanged();
    tab.setupWithViewPager(viewpager);
}
 static class Adapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragments = new ArrayList<>();
    private final List<String> mFragmentTitles = new ArrayList<>();

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

    public void addFragment(Fragment fragment, String title) {
        mFragments.add(fragment);
        mFragmentTitles.add(title);
    }
    public void clear(){
        mFragments.clear();;
        mFragmentTitles.clear();;
    }


    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    @Override
    public int getCount() {
        return mFragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitles.get(position);
    }
}

问题

当片段再次插入到viewpager中时,我想加载数据(插入发生在我调用"adapter.addFragment()"时).并且我以生命周期方法启动了数据加载线程.但是似乎生命周期方法(onCreate,onResume)并不总是被调用.因此,我没有机会启动数据加载线程.

Question

I want to load data when a fragment is insert into the viewpager again(The insertion happens when i call "adapter.addFragment()").And I start the data-loading Thread in the life cycle method.But it seems that the life Cycle Methods(onCreate,onResume) are not always called.So I fail to get the chance to start the data-loading Thread.

如何替换viewpager中的片段并使片段经历正常的生命周期方法:onCreate,onCreateView,onResume?

How can I replace the fragment in viewpager and make the fragment go through the normal life cycle methods:onCreate,onCreateView,onResume ?

推荐答案

最后,我将其修复.

使用FragmentStatePagerAdapter而不是FragmentPagerAdapter

Use FragmentStatePagerAdapter instead of FragmentPagerAdapter

像这样实现getItemPosition:

Implement getItemPosition like this:

 @Override
    public int getItemPosition(Object object){
        return PagerAdapter.POSITION_NONE;
    }

步骤3:

调用Viewpager的setOffscreenPageLimit(int i)以保存状态.

Step 3:

Call the Viewpager's setOffscreenPageLimit(int i) to save state.

这篇关于当我单击NavigationView项时,如何在tablayout下更改viewpager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 18:53