繁体   English   中英

从DialogFragment打开片段

[英]Opening Fragment from DialogFragment

我想从DialogFragment(CatalogueHatDialog.java)中打开Fragment(MirrorFragment.java),并在Fragment中显示我在DialogFragment中选择的图像。 我尝试从此处使用代码: 从DialogFragment打开Fragment(替换Dialogs父对象)以及关于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;
        }
    }
}

我发现了错误。 因此,这是需要编写的内容:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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