我想从activity创建FragmentPagerAdapter适配器。我怎么能这样做?
当我从一个片段调用时,我已经使用过这种方式,但现在我想要一个活动。
MyActivity.java
// here error
adapter = new FragmentPagerAdapter(getFragmentManager(),
images.getImageItem());
viewPage.setAdapter(adapter);
viewPage.setOnPageChangeListener(this);
FragmentPagerAdapter.java
public class FragmentPagerAdapter extends FragmentStatePagerAdapter {
private ArrayList<String> itemData;
public FragmentPagerAdapter(FragmentManager fm,
ArrayList<String> itemData) {
super(fm);
this.itemData = itemData;
}
@Override
public int getCount() {
return itemData.size();
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
super.destroyItem(container, position, object);
}
@Override
public Fragment getItem(int position) {
FragmentImageView f = FragmentImageView.newInstance();
f.setImageList(itemData.get(position));
return f;
}
}
答案 0 :(得分:2)
替换
str('abc').upper() # returns 'ABC'
带
adapter = new FragmentPagerAdapter(getFragmentManager(),images.getImageItem());
adapter = new FragmentPagerAdapter(getSupportFragmentManager(),
images.getImageItem());
是getSupportFragmentManager
的方法,不属于FragmentActivity
,因此您的活动类必须实现Activity
。请注意,您应该导入包FragmentActivity
,不是com.support.v4.app.FragmentManager
。
答案 1 :(得分:1)
而不是getFragmentManager
来电getSupportFragmentManager
/**
* Return the FragmentManager for interacting with fragments associated
* with this activity.
*/
public FragmentManager getSupportFragmentManager() {
return mFragments.getSupportFragmentManager();
}
所以你的代码将是
adapter = new FragmentPagerAdapter(getSupportFragmentManager(),
images.getImageItem());
viewPage.setAdapter(adapter);
viewPage.setOnPageChangeListener(this);