我正在使用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]);
}
}
片段:
<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:detailfragment的
private ImageView mImageView;
(...)
public void SetPhotoView() {
mAttacher = new PhotoViewAttacher(mImageView);
}
佈局文件
<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片段=(ImageDetailFragment)getSupportFragmentManager()findFragmentById(mPager.getId()); fragment.SetPhotoView();
然後,它的工作,但與錯誤的片段... 我怎樣才能找到實際的片段的ID?
其實,我這裏有同樣的問題:Getting the current Fragment instance in the viewpager 但對我來說,findFragmentByTag( 「機器人:切換:」 + R.id.pager + 「:」 + mPager.getCurrentItem())返回任何結果:/
有人可以告訴我如何解決這個問題?
你必須創建片段 –
的一個實例,你怎麼實例化片段還是你? –
nr4bt>我用newInstance()實例化片段(我已經編輯了我的帖子的一些細節)。謝謝 :! – Jerry