1

我有一个活动,我正在设置 viewPager 适配器,如下所示

public class ViewPagerAdapter extends FragmentPagerAdapter {
    private int pagerLength; 
    private HashMap<Integer, String> mFragmentTags;
    private FragmentManager mFragmentManager;
    String page = "Page";
    public ViewPagerAdapter(FragmentManager fm, int numPage) {
        super(fm);
        mFragmentManager = fm;
        pagerLength = numPage;
        mFragmentTags = new HashMap<Integer, String>();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        Object obj = super.instantiateItem(container, position);
        if(obj instanceof Fragment) {
            Fragment f = (Fragment) obj;
            String tag = f.getTag();
            mFragmentTags.put(position, tag);
        }
        return obj;
    }

    @Override
    public Fragment getItem(int index) {
        ImageFragment imageFragment = (ImageFragment)Fragment.instantiate(ImageActivity.this, imageFragment.class.getName(), null);
        return imageFragment;
    }

    @Override
    public int getCount() {
        return pagerLength;
    }

    public Fragment getFragment(int position) {
        String tag = mFragmentTags.get(position);
        if (tag == null) {
            return (Fragment)instantiateItem(mViewPager, position);
        }
        Fragment fragment = mFragmentManager.findFragmentByTag(tag);
        return fragment;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        mFragmentTags.remove(position);
        super.destroyItem(container, position, object);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // TODO Auto-generated method stub
        return  "TESTING";      //Remove later
    }

}

ImageFragment 什么都不是,它只是一个在其布局中带有 imageView 的片段

现在我想在活动片段的 imageView 中设置一个图像,我应该怎么做?

我尝试在我的活动中进行 fn 更新并将 imageUrl 传递给片段中称为更新的函数。问题是虽然片段中的 update() 被调用并接收到图像的 url,但片段的 onCreateView() 从未被调用

4

0 回答 0