Я реализую вид масштабирования фотографий, расположенный сверху сетки эскизов, используя DialogFragment. Он содержит ViewPager, позволяющий пользователям переключаться с фотографий. Ниже представлена подробная информация о DialogFragment:
public class DialogContainer extends PhotoViewerDialogFragment{
public DialogContainer () {
super();
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
// request a window without the title
dialog.setContentView(R.layout.fragment_photo_viewer);
dialog.getWindow().setLayout(AppDevice.getDeviceWidth(), AppDevice.getDeviceHeight());
return dialog;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
viewPager = (CustomViewPager) view.findViewById(R.id.photo_viewer_view_pager);
photoAdapter = new PhotoViewerAdapter(getChildFragmentManager(), photoSupporter, null, null);
viewPager.setAdapter(photoAdapter);
super.onViewCreated(view, savedInstanceState);
}
}
Вот диалог:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="@+id/photo_viewer_view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/dark_background" >
</android.support.v4.view.ViewPager>
И вот как я реализую адаптер:
public static class PhotoViewerAdapter extends FragmentStatePagerAdapter {
private PhotoSupporter photoSupporter;
public PhotoViewerAdapter(FragmentManager fm, PhotoSupporter photoSupporter) {
super(fm);
this.photoSupporter = photoSupporter;
}
@Override
public Fragment getItem(int position) {
String src = null;
// get photo src
return PhotoViewFragment.newInstance(src);
}
@Override
public int getCount() {
Log.w(TAG, "size in adapter: " + photoSupporter.getSize());
return photoSupporter.getSize();
}
}
Вот фрагмент для каждой фотографии:
public static class PhotoViewFragment extends Fragment {
private String imagePath;
public PhotoViewFragment() {
}
public static PhotoViewFragment newInstance(String imagePath) {
PhotoViewFragment previewFragment = new PhotoViewFragment();
// put data
Bundle bundle = new Bundle();
bundle.putString("IMAGE_PATH",
imagePath);
previewFragment.setArguments(bundle);
return previewFragment;
}
/**
* When creating, retrieve this instance number from its arguments.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
imagePath = getArguments() != null ? getArguments().getString(
"IMAGE_PATH") : "";
Log.d("onCreate", imagePath);
}
/**
* The Fragment UI is just a simple text view showing its instance
* number.
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ImageView imageView = new ImageView(getActivity());
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
imageView.setLayoutParams(lp);
// set image by Picasso
Picasso picassoInstance = Picasso.with(getActivity());
picassoInstance.setIndicatorsEnabled(true);
picassoInstance.load(new File(imagePath))
.error(R.drawable.ic_picture_placeholder_light2)
.fit()
.centerInside()
.into(imageView);
return imageView;
}
}
}
При нажатии на миниатюру:
dialogContainer = new DialogContainer();
dialogContainer.show(((HomeActivity)parentActivity).getSupportFragmentManager(), "photo_viewer_dialog");
И я получил эту ошибку:
01-16 15:46:28.916: E/AndroidRuntime(18338): FATAL EXCEPTION: main
01-16 15:46:28.916: E/AndroidRuntime(18338): java.lang.IllegalStateException: No activity
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1070)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:1861)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1474)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.os.Handler.handleCallback(Handler.java:615)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.os.Handler.dispatchMessage(Handler.java:92)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.os.Looper.loop(Looper.java:137)
01-16 15:46:28.916: E/AndroidRuntime(18338): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-16 15:46:28.916: E/AndroidRuntime(18338): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 15:46:28.916: E/AndroidRuntime(18338): at java.lang.reflect.Method.invoke(Method.java:511)
01-16 15:46:28.916: E/AndroidRuntime(18338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-16 15:46:28.916: E/AndroidRuntime(18338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-16 15:46:28.916: E/AndroidRuntime(18338): at dalvik.system.NativeStart.main(Native Method)
Спасибо, ребята, за какие-либо предложения, и, пожалуйста, дайте мне знать все, что пока не ясно.
ps: Я попробовал этот, этот и getChildFragmentManager
, но ничего не изменилось.