简体   繁体   中英

Opening Fragment from DialogFragment

I want to open Fragment (MirrorFragment.java) from DialogFragment (CatalogueHatDialog.java) and show in Fragment the image that I have chosen in DialogFragment. I tried to use the code from here: Opening Fragment from a DialogFragment (replacing the Dialogs parent) and other questions on stackoverflow, but it didn't help. The image isn't shown and there are no errors. I think my mistake is in Fragment, but I'm not sure.

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 (not relevant methods are excluded):

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;
        }
    }
}

I found the mistake. So here is what is needed to be written:

CatalogueHatDialog.java:

public class CatalogueHatDialog extends DialogFragment {

    ...

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                dismiss();
                getTargetFragment().onActivityResult(position, Activity.RESULT_OK, getActivity().getIntent());


            }
        });
        ...
    }

}

MirrorFragment.java:

…
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    btnCapture.setCurrentItem(requestCode);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM