GetItem(int)返回FragmentPagerAdapter中的Listfragment

时间:2013-08-27 03:00:50

标签: android android-fragments android-listfragment fragmentpageradapter

我正在开发一个由3个List片段组成的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 );
}

}

提前致谢

1 个答案:

答案 0 :(得分:6)

最后,谢谢@hieuxit。改变

import android.app.ListFragment;

import android.support.v4.app.ListFragment;

解决了我的问题...