如何在Tablayout

时间:2017-02-23 16:19:16

标签: android android-fragments android-viewpager android-adapter android-tablayout

我有一个带有ViewPager的tablayout(tab1,tab2,tab3)。我想在tab1中输入内容,并且取决于输入,tab2和tab3将相应地显示数据。

每次应用程序启动时,tab1(片段)和tab2(片段)都会加载正确的数据。我正在使用静态变量将数据从tab1传递到tab2。但是,在我更改tab1中的某些数据后,tab2不会根据tab1中的数据更新自身,它会显示旧数据。这是我的代码

private ViewPager viewPager;
private PagerAdapter adapter;
private TabLayout tabLayout;

在活动中的onCreate方法中。

    tabLayout.addTab(tabLayout.newTab().setText("Input"));
    tabLayout.addTab(tabLayout.newTab().setText("Detail"));
    tabLayout.addTab(tabLayout.newTab().setText("Chart"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    viewPager = (ViewPager) findViewById(R.id.pager);
    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) {

        }
    });

这是我的pageadapater类

public class PagerAdapter extends FragmentPagerAdapter {
int mNumOfTabs;
private InputFragment inputFragment;
private DetailFragment detailFragment;
private ChartFragment chartFragment;

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

@Override
public int getItemPosition(Object object) {
    // TODO Auto-generated method stub
    return POSITION_NONE;
}


@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            inputFragment = new InputFragment();
            return inputFragment;
        case 1:
            detailFragment = new detailFragment();
            return detailFragment;
        case 2:
            chartFragment = new ChartFragment();
            return chartFragment;
        default:
            return null;
    }
}

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

0 个答案:

没有答案