从父活动调用片段public void

时间:2014-01-27 19:35:27

标签: android android-fragments

我正在研究BitMapFun项目。 我试图从片段的父活动中调用片段public void。

一些源代码:

活动:

public void onCreate(Bundle savedInstanceState) {
    if (BuildConfig.DEBUG) {
        Utils.enableStrictMode();
    }
    super.onCreate(savedInstanceState);
    setContentView(R.layout.image_detail_pager);

    // Fetch screen height and width, to use as our max size when loading images as this
    // activity runs full screen
    final DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    final int height = displayMetrics.heightPixels;
    final int width = displayMetrics.widthPixels;

    ImageCache.ImageCacheParams cacheParams =
            new ImageCache.ImageCacheParams(this, IMAGE_CACHE_DIR);
    cacheParams.setMemCacheSizePercent(0.25f); // Set memory cache to 25% of app memory

    // The ImageFetcher takes care of loading images into our ImageView children asynchronously
    mImageFetcher = new ImageFetcher(this, longest);
    mImageFetcher.addImageCache(getSupportFragmentManager(), cacheParams);
    mImageFetcher.setImageFadeIn(false);

    // Set up ViewPager and backing adapter
    mAdapter = new ImagePagerAdapter(getSupportFragmentManager(), Images.imageUrls.length);
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdapter);
    mPager.setOffscreenPageLimit(2);

    // Set up activity to go full screen
    getWindow().addFlags(LayoutParams.FLAG_FULLSCREEN);

    // Enable some additional newer visibility and ActionBar features to create a more immersive photo viewing experience
    if (Utils.hasHoneycomb()) {
        final ActionBar actionBar = getActionBar();

        // Set home as up
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setTitle("");

        // Start low profile mode
        mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
        // actionBar.hide();
    }

    // Set the current item based on the extra passed in to this activity
    final int extraCurrentItem = getIntent().getIntExtra(EXTRA_IMAGE, -1);
    if (extraCurrentItem != -1) {
        mPager.setCurrentItem(extraCurrentItem);
    }

    // Événement changement d'image
    mPager.setOnPageChangeListener(new OnPageChangeListener() {
        @Override
        public void onPageSelected(int arg0) {
            // cache le bouton de retour situé dans le coin supérieur gauche si l'utilisateur change d'image
            mPager.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);
        }

        @Override
        public void onPageScrollStateChanged(int arg0) { }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) { }
     });
}

(...)

ImageDetailFragment.SetPhotoView();

(...)


private class ImagePagerAdapter extends FragmentStatePagerAdapter {
    private final int mSize;

    public ImagePagerAdapter(FragmentManager fm, int size) {
        super(fm);
        mSize = size;
    }

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

    @Override
    public Fragment getItem(int position) {
        return ImageDetailFragment.newInstance(Images.imageUrls[position]);
    }
}

片段:

private ImageView mImageView;

(...)

    public void SetPhotoView() {
        mAttacher = new PhotoViewAttacher(mImageView);
    }

detailfragment的布局文件:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />

    <biz.jchambon.gedm.bitmapfun.ui.RecyclingImageView
        android:id="@+id/imageView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:contentDescription="@string/imageview_description" />

</FrameLayout>

和layout_detail_pager.xml:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

</android.support.v4.view.ViewPager>

问题是我无法对非静态方法SetPhotoView()进行静态引用... 如果我将SetPhotoView更改为静态方法,我必须将mImageView更改为静态,并且我的片段不再起作用。

编辑:

如果我使用 ImageDetailFragment fragment =(ImageDetailFragment)getSupportFragmentManager()。findFragmentById(mPager.getId()); fragment.SetPhotoView();

然后它工作但错误的片段...... 如何找到实际片段的ID?

事实上,我遇到的问题与此处相同:Getting the current Fragment instance in the viewpager 但对我来说,findFragmentByTag(“android:switcher:”+ R.id.pager +“:”+ mPager.getCurrentItem())什么都不返回:/

有人可以告诉我怎样才能解决这个问题?

1 个答案:

答案 0 :(得分:2)

从您的活动中,您可以获得这样的片段对象。

ImageDetailFragment fragment = (ImageDetailFragment) getSupportFragmentManager().findFragmentById(android.R.id.content);

如果你的片段占用整个活动,那么使用android.R.id.content,否则将布局的id放在片段所在的位置。

然后你可以打电话

fragment.SetPhotoView();