我有处理ScreenSlider的PagerAdapter类。我想要的是添加实例(Fragment类的对象)而不是添加多个片段
调用PagerAdapterClass的类:
List<Fragment> fragments = new Vector<Fragment>();
fragments.add(Fragment.instantiate(this, Fragment1.class.getName()));// Here i want to add instance of class
fragments.add(Fragment.instantiate(this, Fragment2.class.getName()));
this.mPagerAdapter = new PagerAdapter2(super.getSupportFragmentManager(), fragments); //Constructor
ViewPager pager = (ViewPager)findViewById(R.id.view_pager);
pager.setAdapter(this.mPagerAdapter);
pager.setCurrentItem(Proba.broj);
这是PagerAdapter类:
public class PagerAdapter2 extends FragmentPagerAdapter
{
private List<Fragment> fragments;
/**
* @param fm
* @param fragments
*/
public PagerAdapter2(FragmentManager fm, List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
/* (non-Javadoc)
* @see android.support.v4.app.FragmentPagerAdapter#getItem(int)
*/
@Override
public Fragment getItem(int position)
{
return this.fragments.get(position);
}
/* (non-Javadoc)
* @see android.support.v4.view.PagerAdapter#getCount()
*/
@Override
public int getCount() {
return this.fragments.size();
}
}
答案 0 :(得分:0)
然后添加一个实例。
fragments.add(new Fragment1()) ;
或者如果你需要参数
Fragment f = new Fragment1();
Bundle args1 = new Bundle() ;
// put into args1
f.setArguments(args1);
fragments.add(f) ;
相关:When Fragment.instantiate is preferable over MyFragment.newInstance or new MyFragment()
而不是添加多个片段
如果您想要多个页面,则必须添加多个片段