我的应用程序中有一个slipTab,它有4个孩子,每个孩子连接到一个碎片并从json获取数据。
当我从标签滚动到标签时,它从互联网上获取数据并显示给我,没关系但是如何保存下载的数据并在我返回相同标签时再次显示?
问题是,每当我切换标签时,它都会从互联网上调用并获取数据,而且非常麻烦。
如何保存标签内容并在加载后将其显示为脱机状态?
这是我用于创建标签的代码:
class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
final int PAGE_COUNT = 3;
private String tabTitles[] = new String[]{"tab", "tab","tab","tab"};
private Context context;
public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public int getCount() {
return PAGE_COUNT;
}
@Override
public android.support.v4.app.Fragment getItem(int position) {
android.support.v4.app.Fragment fragment = null;
Bundle bundle = new Bundle();
if (position == 0) {
fragment = new Fragments();
bundle.putString("for", "basteVije");
fragment.setArguments(bundle);
}
if (position == 1) {
fragment = new Fragments();
bundle.putString("for", "topSold");
fragment.setArguments(bundle);
}
if (position == 2) {
fragment = new Fragments();
bundle.putString("for", "jadidtarin");
fragment.setArguments(bundle);
}
if (position == 3) {
fragment = new Fragments();
bundle.putString("for", "Pishnahad");
fragment.setArguments(bundle);
}
return fragment;
}
@Override
public CharSequence getPageTitle(int position) {
// Generate title based on item position
return tabTitles[position];
}
}
private void makeTabs() {
viewpager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(), FistActiivty.this));
viewpager.setCurrentItem(4);
// Give the SlidingTabLayout the ViewPager
SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
// Center the tabs in the layout
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setViewPager(viewpager);
slidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
@Override
public int getIndicatorColor(int position) {
return Color.RED;
}
});
}
答案 0 :(得分:0)
试试这个:
viewpager.setOffScreenPageLimit(4);
该数字取决于您要设置的标签数量。
答案 1 :(得分:0)
您可以将网络请求的结果保存到SharedPreferences
写下数据:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(FRAGMENT_ID, result);
editor.commit();
阅读数据:
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
String result = sharedPref.getString(FRAGMENT_ID);
通过这种方式,您可以决定是从区域设置还是从网络
获取数据