我希望能够在Android上预先加载动态内容的下一个ViewPager
页面。在创建视图后,我将视图添加到视图中。
@Override
public Object instantiateItem(ViewGroup collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.test_layout, null);
view.setTag("rel" + position);
collection.addView(view, 0);
return view;
}
如果我尝试使用findViewWithTag("rel" + (view1.getCurrentItem() + 1));
获取下一个视图,则会返回null
。那么,我怎样才能获得下一个视图,或者手动充气呢?
答案 0 :(得分:0)
尝试使用setOffscreenPageLimit(int limit)
类的ViewPager
方法。
答案 1 :(得分:0)
在进入下一个位置之前调用getItem方法,这样你就可以在getInstance中传递你想要的任何动态数据
私有类MyPagerAdapter扩展了FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return FirstFragment.newInstance("FirstFragment, Instance 1");
case 1: return SecondFragment.newInstance("SecondFragment, Instance 1");
case 2: return ThirdFragment.newInstance("ThirdFragment, Instance 1");
case 3: return ThirdFragment.newInstance("ThirdFragment, Instance 2");
case 4: return ThirdFragment.newInstance("ThirdFragment, Instance 3");
default: return ThirdFragment.newInstance("ThirdFragment, Default");
}
}
@Override
public int getCount() {
return 5;
}
}
公共类FirstFragment扩展了Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_frag, container, false);
TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
tv.setText(getArguments().getString("msg"));
return v;
}
public static FirstFragment newInstance(String text) {
FirstFragment f = new FirstFragment();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}