FragmentPagerAdapter不会更新项目

时间:2015-02-15 13:30:59

标签: android android-viewpager

我有FragmentPager适配器,如下所示:

public class ImageAdapter extends FragmentPagerAdapter{
    private List<ImageItem> items;

    public ImageAdapter(List<ImageItem> items, FragmentManager manager) {
        super(manager);
        this.items = items;
    }

    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Fragment getItem(int position) {
        ImageFragment fragment = new ImageFragment();
        fragment.setItem(items.get(position));
        return fragment;
    }

    @Override
    public int getItemPosition(Object object) {
        ImageFragment fragment = (ImageFragment)object;
        int position = items.indexOf(fragment.getItem());

        if(position >= 0) {
            fragment.pudDataChanges();
            return position;
        } else return POSITION_NONE;
    }

}

因此片段本身是空的,所有自定义都来自getItem和getItemsPoisition,然后我有一个加载器,它创建了这个适配器,将它放到ViewPager中:

 @Override
    public void onLoadFinished(Loader<ArrayList<ImageItem>> loader, ArrayList<ImageItem> data) {
        adapter = new ImageAdapter(data, getChildFragmentManager());
        pager.setAdapter(adapter);
        pager.setCurrentItem(0, true);
        adapter.notifyDataSetChanged();
        hideProgressDialog();

        getLoaderManager().initLoader(SearchHelper.INSERT_ITEMS_TO_DATABASE_ID, null, new InsertItemsToDatabaseHandler(data)).forceLoad();

    }

它第一次设置一切都没问题,但第二次和以后它只是没有调用getItem()而我有空片段

1 个答案:

答案 0 :(得分:0)

我相信你需要添加

@Override
public void onLoaderReset(Loader<ArrayList<ImageItem>> loader) {
    pager.setAdapter(null);
}

到实现LoaderManager.LoaderCallbacks<>的类。