【发布时间】:2016-01-11 14:10:30
【问题描述】:
我想从 DialogFragment (CatalogueHatDialog.java) 打开 Fragment (MirrorFragment.java) 并在 Fragment 中显示我在 DialogFragment 中选择的图像。 我尝试使用此处的代码:Opening Fragment from a DialogFragment (replacing the Dialogs parent) 和有关 stackoverflow 的其他问题,但没有帮助。 图像未显示,也没有错误。 我认为我的错误在于 Fragment,但我不确定。
CatalogueHatDialog.java:
public class CatalogueHatDialog extends DialogFragment {
private static List<Hat> listHats;
private GridView gvMain;
public static CatalogueHatDialog newInstance(List<Hat> hats) {
listHats = hats;
Bundle args = new Bundle();
CatalogueHatDialog catalogueHatDialog = new CatalogueHatDialog();
catalogueHatDialog.setArguments(args);
return catalogueHatDialog;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.catalogue, null);
gvMain = (GridView) v.findViewById(R.id.gvCatalogue);
gvMain.setAdapter(new CatalogueGridAdapter(listHats, getActivity()));
gvMain.setOnItemClickListener(new GridView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
dismiss();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.btnCapture, MirrorFragment.PlaceholderFragment.newInstance(position));
ft.addToBackStack(null);
ft.commit();
}
});
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return v;
}
}
MirrorFragment.java(不相关方法除外):
public class MirrorFragment extends Fragment implements SurfaceHolder.Callback {
private static View viewMirrorLayout;
private static View cameraViewControl;
private SurfaceHolder cameraSurfaceHolder = null;
public ViewPager btnCapture;
private Activity activity;
private SectionsPagerAdapter mSectionsPagerAdapter;
private ImageView btnCatalogue;
private static int hatId;
public MirrorFragment() {
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
this.activity = (Activity) context;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
SurfaceView cameraSurfaceView;
viewMirrorLayout = inflater.inflate(R.layout.fragment_mirror, container, false);
activity.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
activity.getWindow().setFormat(PixelFormat.TRANSLUCENT);
activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
cameraSurfaceView = (SurfaceView) viewMirrorLayout.findViewById(R.id.cameraSurfaceViewFragment);
cameraSurfaceHolder = cameraSurfaceView.getHolder();
cameraSurfaceHolder.addCallback(this);
cameraSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
LayoutInflater layoutInflater = LayoutInflater.from(getActivity());
RelativeLayout.LayoutParams layoutParamsControl = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
cameraViewControl = layoutInflater.inflate(R.layout.cambutton, null);
btnCapture = (ViewPager) cameraViewControl.findViewById(R.id.btnCapture);
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
btnCapture.setAdapter(mSectionsPagerAdapter);
btnCapture.addOnPageChangeListener(new MyPageChangeListener());
((FrameLayout) viewMirrorLayout).addView(cameraViewControl, layoutParamsControl);
btnCatalogue = (ImageView) viewMirrorLayout.findViewById(R.id.ivCatalog);
btnCatalogue.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CatalogueHatDialog dialog = CatalogueHatDialog.newInstance(getAllHats());
dialog.show(getFragmentManager(), "catalogueDialog");
}
});
return viewMirrorLayout;
}
public static class PlaceholderFragment extends Fragment {
private static final String ARG_SECTION_NUMBER = "section_number";
private ImageView ivHat;
public PlaceholderFragment() {
}
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View slidingLayout = inflater.inflate(R.layout.sliding_fragment_hat, container, false);
ivHat = (ImageView) slidingLayout.findViewById(R.id.ivHat);
ivHat.setBackgroundResource(getAllHats().get(getArguments().getInt(ARG_SECTION_NUMBER) - 1).getPhoto());
return slidingLayout;
}
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
return getAllHats().size();
}
}
private class MyPageChangeListener extends ViewPager.SimpleOnPageChangeListener {
@Override
public void onPageSelected(int position) {
hatId = position;
}
}
}
【问题讨论】:
-
你的位置数为什么要减1?
标签: java android android-fragments android-dialogfragment