使用ImageView onClick从另一个Fragment打开新片段

时间:2016-12-15 14:34:50

标签: java android android-fragments

我想在片段中创建一个带有图像视图的菜单。每个人都有抽屉菜单,所以我想要与众不同:

我制作了OneFragment.java,这是我的oncreateview:

   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_one, container, false);

        super.onViewCreated(view, savedInstanceState);
        img = (ImageView)view.findViewById(R.id.imgRandom);
        imageView9 = (ImageView)view.findViewById(R.id.imageView3);
        imageView4 = (ImageView)view.findViewById(R.id.imageView4);
        imageView5 = (ImageView)view.findViewById(R.id.imageView5);
        imageView6 = (ImageView)view.findViewById(R.id.imageView6);
        imageView7 = (ImageView)view.findViewById(R.id.imageView7);
        imageView8 = (ImageView)view.findViewById(R.id.imageView8);

        firstName = (TextView)view.findViewById(R.id.imie);


        textView13 = (TextView)view.findViewById(R.id.textView13);
        textView14 = (TextView)view.findViewById(R.id.textView15);
        textView15 = (TextView)view.findViewById(R.id.textView16);
        textView16 = (TextView)view.findViewById(R.id.textView17);
        textView17 = (TextView)view.findViewById(R.id.textView18);
        textView18 = (TextView)view.findViewById(R.id.textView19);

        final Animation wbijam = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
        textView13.startAnimation(wbijam);
        final Animation wbijam1 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
        textView14.startAnimation(wbijam1);
        final Animation wbijam2 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
        textView15.startAnimation(wbijam2);
        final Animation wbijam3 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
        textView16.startAnimation(wbijam3);
        final Animation wbijam4 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
        textView17.startAnimation(wbijam4);
        final Animation wbijam5 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.wbijam);
        textView18.startAnimation(wbijam5);






        final Animation animShake = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.shak);
        img.startAnimation(animShake);


        final Animation load1 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load1);
        imageView9.startAnimation(load1);
        final Animation load2 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load2);
        imageView4.startAnimation(load2);
        final Animation load3 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load3);
        imageView5.startAnimation(load3);
        final Animation load4 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load4);
        imageView6.startAnimation(load4);
        final Animation load5 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load5);
        imageView7.startAnimation(load5);
        final Animation load6 = AnimationUtils.loadAnimation(getActivity().getApplicationContext(), R.anim.load6);
        imageView8.startAnimation(load6);


        firstName.setText("Hey,"+" " +PreferenceConnector.readString(getActivity().getApplicationContext(), PreferenceConnector.FIRST_NAME, ""));

        // Inflate the layout for this fragment

        final ImageView img = (ImageView) view.findViewById(R.id.imgRandom);
//  return view;
//  ^^^^  error remove it
        Resources res = getResources();

        myString = res.getStringArray(R.array.myArray);

        String q = myString[rgenerator.nextInt(myString.length)];

        TextView tv = (TextView) view.findViewById(R.id.tv);
        tv.setText(q);
        // I have 3 images named img_0 to img_2, so...
        final String str = "img_" + rnd.nextInt(9);
        img.setImageDrawable
                (
                        getResources().getDrawable(getResourceID(str, "drawable",
                                getActivity().getApplicationContext())
                ));
        return view;
        // ^^^ move it here
    }

        // I have 3 images named img_0 to img_2, so...



    protected final static int getResourceID
            (final String resName, final String resType, final Context ctx) {
        final int ResourceID =
                ctx.getResources().getIdentifier(resName, resType,
                        ctx.getApplicationInfo().packageName);
        if (ResourceID == 0) {
            throw new IllegalArgumentException
                    (
                            "No resource string found with name " + resName
                    );
        } else {
            return ResourceID;
        }

    }

}

我想点击一下

imageView9 = (ImageView)view.findViewById(R.id.imageView3);

将打开片段SecondFragment

有可能吗? 我尝试过,但没有找到任何解决方案。

1 个答案:

答案 0 :(得分:0)

你可以发布你的布局吗? 当使用菜单在片段之间导航时,您将菜单保留在托管活动中,并通过切换该活动内的片段进行导航,无需将菜单放在其中一个片段中。