2016-06-01 126 views
-3

我真的很抱歉这个愚蠢的问题,但我一直在阅读各种东西,但似乎无法通过我厚厚的头骨。我真的很绝望,所以请耐心等待。去一个片段的其他片段

所以我得到了一个充满文字的片段,说fragment_chapter_1.xml。其中一个文本需要进一步的解释,所以我想在文本(Copyright Act (Amendment) 1997)被点击(它不是可点击xml布局)时,它会去fragment_explain_law.xml

fragment_explain_law.xml有一个TextView explainLaw,这将得到的setText(在ExplainLawFragment.java),以它自己在fragment_chapter_1.xml单击文本的解释。我使用这种方式,因为如果其他文本被点击,它将重复使用相同的fragment_explain_law.xml,但explainLaw将设置不同的解释。

这是代码。

  1. Chapter1Fragment.java,它会查看fragment_chapter_one.xmllaw1Copyright Act (Amendment) 1997的ID。

    @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. 当得到点击law1fragment_explain_law.xml将被显示。为此,我通过去ExplainLawFragment.java(我做对了吗?)。

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

结果:没有发生。

logcat的,当我点击law1V/SettingsInterface: from settings cache , name = sound_effects_enabled , value = 1

我有什么错?请帮忙。

回答

2

你可以这样调用第二个片段....

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

}

希望这将帮助你..

+0

这个解决问题。非常感谢你! – August

+0

尽管标题没有改变 – August

+0

你可以在第二个片段onCreateView()方法中设置标题,如.... getActivity()。setTitle(“Your title”); –

相关问题