TabLayout 预加载下一个选项卡
TabLayout Preload next tab
我有一个带 ViewPager 的 tablayout(tab1、tab2、tab3)。似乎在应用程序启动时预加载了 tab2,这很好。
但是,我需要在 tab1 上输入一些数据,我希望 tab2 根据从 tab1 输入的数据更新其片段并隐藏一些按钮。
怎么做?有没有办法不预加载选项卡,这样当我滑动到 tab2 时,它将获取最新数据而不是旧数据?
这是我的代码
private ViewPager viewPager;
private PagerAdapter adapter;
private TabLayout tabLayout;
在我的 activity 内的 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) {
}
});
这是我的页面适配器class
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;
}
}
在您的 detailfragment 片段上
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
//check values on tab 1 and try to update your tab 2 views accordingly
}
}
我有一个带 ViewPager 的 tablayout(tab1、tab2、tab3)。似乎在应用程序启动时预加载了 tab2,这很好。
但是,我需要在 tab1 上输入一些数据,我希望 tab2 根据从 tab1 输入的数据更新其片段并隐藏一些按钮。
怎么做?有没有办法不预加载选项卡,这样当我滑动到 tab2 时,它将获取最新数据而不是旧数据?
这是我的代码
private ViewPager viewPager;
private PagerAdapter adapter;
private TabLayout tabLayout;
在我的 activity 内的 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) {
}
});
这是我的页面适配器class
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;
}
}
在您的 detailfragment 片段上
@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);
if (isVisibleToUser) {
//check values on tab 1 and try to update your tab 2 views accordingly
}
}