我使用viewpagerindicator并从服务器获得6个带自定义列表视图的片段。在启动时,此活动看起来所有片段都在加载。 我想要一个片段启动刚刚打开,点击或刷卡并进行加载。 并且在打开,点击或稍后滑动时将加载尚未打开的片段。如何?
这是我的代码适配器viewpager
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class ViewAdapter extends FragmentPagerAdapter{
private Context _context;
String[] page_titles;
public ViewAdapter(Context context, FragmentManager fm, String[] page_title){
super(fm);
_context = context;
this.page_titles = page_title;
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
Fragment f = new Fragment();
switch(position){
case 0:
f = ContentActivity.newInstance(_context);
break;
case 1:
f = ContentActivity.newInstance(_context);
break;
case 2:
f = ContentActivity.newInstance(_context);
break;
case 3:
f = ContentActivity.newInstance(_context);
break;
case 4:
f = ContentActivity.newInstance(_context);
break;
case 5:
f = ContentActivity.newInstance(_context);
break;
}
return f;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return page_titles.length;
}
public CharSequence getPageTitle(int position) {
return page_titles[position];
}
}
我已经设置了
pager = (ViewPager) findViewById(R.id.pager);
pager.setOffscreenPageLimit(1);
但是如果我从片段0滑动到片段索引1并且向后滑动到索引0或其他索引,总是重新加载内容,我想如果后面滑动到之前的片段不再重新加载?它怎么样
谢谢......对不起我的英文..
答案 0 :(得分:0)
在适配器构造函数中创建一个片段数组,并从getItem
返回已创建的片段 Fragment[] f=new Fragment[6];
public ViewAdapter(Context context, FragmentManager fm, String[] page_title){
super(fm);
_context = context;
this.page_titles = page_title;
f[0] = ContentActivity.newInstance(_context);
f[1] = ContentActivity.newInstance(_context);
f[2] = ContentActivity.newInstance(_context);
f[3] = ContentActivity.newInstance(_context);
f[4] = ContentActivity.newInstance(_context);
f[5] = ContentActivity.newInstance(_context);
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
return f[position];
}