在TabLayout

时间:2016-04-23 06:41:50

标签: java android

我们正在开发一个项目,其中tablayout中有四个选项卡。四个选项卡中的每一个都是片段,并且在加载时调用了Web服务。我们需要再次调用Web服务以便我们可以刷新刷子上的碎片。但是当加载第一个片段时,当我滑动标签并返回到第一个片段时,它不会令人耳目一新。以下是我们迄今为止所尝试过的:

tabLayout = (TabLayout) findViewById(R.id.tablayout);

  tabLayout.addTab(tabLayout.newTab().setCustomView(tabView));
            tabLayout.addTab(tabLayout.newTab().setText("TAB 1"));
            tabLayout.addTab(tabLayout.newTab().setText("TAB 2"));
            tabLayout.addTab(tabLayout.newTab().setText("TAB 3"));
            tabLayout.addTab(tabLayout.newTab().setText("TAB 4"));
            tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
            final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
            final PagerAdapter adapter = new PagerAdapter
                    (getSupportFragmentManager(), tabLayout.getTabCount());
            viewPager.setAdapter(adapter);
            viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
            tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
                @Override
                public void onTabSelected(TabLayout.Tab tab) {
                    viewPager.setCurrentItem(tab.getPosition());
                }

                @Override
                public void onTabUnselected(TabLayout.Tab tab) {

                }

                @Override
                public void onTabReselected(TabLayout.Tab tab) {

                }
            });

The adapter class:

public class PagerAdapter extends FragmentStatePagerAdapter {
    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                TabFragment2015 tab1 = new TabFragment2015();
                return tab1;
            case 1:
                TabFragmentXchanges tab2 = new TabFragmentXchanges();
                return tab2;
            case 2:
                TabFragmentToday tab3 = new TabFragmentToday();
                return tab3;
            case 3:
                TabFragment2016 tab4=new TabFragment2016();
                return tab4;
            default:
                return null;
        }
    }

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

使用了四个标签。我使用以下代码首先加载第三个标签:

tabLayout.setScrollPosition(2, 0f, true);
            viewPager.setCurrentItem(2);

当我从thrid选项卡滑动到第二个选项卡然后再次打开第三个选项卡时,片段不会重新加载。但是如果我滑动到第一个选项卡然后打开第三个选项卡它会刷新。我希望选项卡刷新在每次刷卡。请帮助..

1 个答案:

答案 0 :(得分:2)

只需要jsut必须添加以下代码:

tabLayout =(TabLayout)findViewById(R.id.tablayout);

tabLayout.addTab(tabLayout.newTab().setCustomView(tabView));
        tabLayout.addTab(tabLayout.newTab().setText("TAB 1"));
        tabLayout.addTab(tabLayout.newTab().setText("TAB 2"));
        tabLayout.addTab(tabLayout.newTab().setText("TAB 3"));
        tabLayout.addTab(tabLayout.newTab().setText("TAB 4"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
        final ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        final PagerAdapter adapter = new PagerAdapter
                (getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
                adapter.refreshFragment(tab.getPosition())

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

适配器类:

public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
TabFragment2015 tab1;
TabFragmentXchanges tab2;
TabFragmentToday tab3;
TabFragment2016 tab4;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0:
            tab1 = new TabFragment2015();
            return tab1;
        case 1:
             tab2 = new TabFragmentXchanges();
            return tab2;
        case 2:
             tab3 = new TabFragmentToday();
            return tab3;
        case 3:
             tab4=new TabFragment2016();
            return tab4;
        default:
            return null;
    }
}

public void refreshFragment(int position) {
    switch (position) {
            case 1:
            tab1.refreshApi();
            break;
        case 2:
            tab2.refreshApi();
            break;
        case 3:
            tab3.refreshApi();
            break;
        case 4:
            tab4.refreshApi();
            break;
    }
  }

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

在所有四个片段中添加以下方法:

public void refreshApi(){
    //write the code here to refresh your Api
}