简体   繁体   中英

Go to other fragment from a fragment

I'm really sorry for this stupid question but I've been reading various things but seems can't get through my thick skull. I'm really desperate here so please bear with me.

So I got one fragment full of text, say fragment_chapter_1.xml . One of the text need further explanation so I want to make it when the text ( Copyright Act (Amendment) 1997 ) got clicked (it is not set clickable in xml layout), it will go to fragment_explain_law.xml .

The fragment_explain_law.xml have a TextView explainLaw , which will get setText (in ExplainLawFragment.java ) to it's own explanation of the text clicked in fragment_chapter_1.xml . I use this way because if other text got clicked, it will reuse the same fragment_explain_law.xml but the explainLaw will set to different explanation.

Here's the code.

  1. In Chapter1Fragment.java , it will view fragment_chapter_one.xml . law1 is id for Copyright Act (Amendment) 1997 .

     @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.fragment_chapter_1, container, false); law1 = (TextView) rootView.findViewById(R.id.law1); law1.setOnClickListener(this); // Inflate the layout for this fragment return rootView; }
  2. When law1 got clicked, fragment_explain_law.xml will be shown. For that, I make it by go to ExplainLawFragment.java (am I doing it right?).

     @Override public void onClick(View v) { // when law1 till law11 clicked, go to ExplainLawFragment.java to view fragment_explain_law.xml Fragment fragment = null; String title = null; switch (v.getId()) { case R.id.law1: // view explainLaw fragment = new ExplainLawFragment(); title = "Copyright Act (Amendment) 1997"; break; }

    }

  3. In ExplainLawFragment.java

     @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_explain_law, container, false); explainLaw = (TextView) rootView.findViewById(R.id.explainLaw); explainLaw.setText("blahblahblah"); // Inflate the layout for this fragment return rootView; }

Result: Nothing happen.

Logcat when I click law1 : V/SettingsInterface: from settings cache , name = sound_effects_enabled , value = 1

Where did I go wrong? Please help.

You can call second fragment like this....

@Override
public void onClick(View v) {
// when law1 till law11 clicked, go to ExplainLawFragment.java to view fragment_explain_law.xml
Fragment fragment = null;
String title = null;

switch (v.getId()) {
case R.id.law1:
    // view explainLaw
    fragment = new ExplainLawFragment();
    title = "Copyright Act (Amendment) 1997";
 FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
    break;

}

Hope this will help you..

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