我正在开发一个由 3 个列表片段组成的 Android 应用程序。我正在使用一个ViewPager
对象让用户在片段之间滑动。我对片段使用了相同的代码。但是当我将它更改为使用列表片段时,我收到了一个错误
the return type is incompatible with FragmentPagerAdapter.getItem(int)
我被困在这里几个小时。请帮忙..
我的片段活动
public class PageViewActivity extends FragmentActivity {
MyPageAdapter pageAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page_view);
List<ListFragment> fragments = getFragments();
pageAdapter = new MyPageAdapter(getSupportFragmentManager(), fragments);
ViewPager pager = (ViewPager)findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
}
private List<ListFragment> getFragments(){
List<ListFragment> fList = new ArrayList<ListFragment>();
fList.add(AllMsgFragment.newInstance("Fragment 1"));
fList.add(ErrorMsgFragment.newInstance("Fragment 2"));
fList.add(SuccessMsgFragment.newInstance("Fragment 3"));
return fList;
}
和我的 FragmentPageAdapter
class MyPageAdapter extends FragmentPagerAdapter {
private List<ListFragment> fragments;
public MyPageAdapter(FragmentManager fm, List<ListFragment> fragments) {
super(fm);
this.fragments = fragments;
}
@Override
public Fragment getItem(int position) {//The return type is incompatible with FragmentPagerAdapter.getItem(int)
return this.fragments.get(position);
}
@Override
public int getCount() {
return this.fragments.size();
}
@Override
public CharSequence getPageTitle(int position) {
return "Page #" + ( position + 1 );
}
}
提前致谢