2016-12-15 121 views
0

我想在片段內使用imageviews創建菜單。每個人都得到了抽屜菜單,所以我想是不同的:使用ImageView從另一個片段打開新片段onClick

我做了一個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

這可能嗎? 我試過但沒有找到任何解決方案。

回答

0

你可以發佈你的佈局? 當使用菜單在片段之間導航時,您可以將菜單保留在主機活動中,並通過在該活動中切換片段進行導航,因此無需將菜單放入其中一個片段中。

相關問題