Android viewPager - 片段

时间:2013-02-25 15:37:27

标签: android android-viewpager

我有一个活动,我正在设置一个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 in fragment的函数。问题是虽然片段中的update()被调用并接收到image的url,但是片段的onCreateView()从不被调用

0 个答案:

没有答案