【问题标题】:Go to other fragment from a fragment从一个片段转到另一个片段
【发布时间】:2016-09-30 09:59:38
【问题描述】:

对于这个愚蠢的问题,我真的很抱歉,但我一直在阅读各种各样的东西,但似乎无法通过我厚厚的头骨。我在这里真的很绝望,所以请多多包涵。

所以我得到了一个完整的文本片段,比如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.xml。 law1 是 Copyright 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. 当law1 被点击时,fragment_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;
    }
    

结果:什么也没发生。

点击law1时的Logcat:V/SettingsInterface: from settings cache , name = sound_effects_enabled , value = 1

我哪里做错了?请帮忙。

【问题讨论】:

    标签: java android xml android-fragments


    【解决方案1】:

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

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

    }

    希望这会对你有所帮助..

    【讨论】:

    • 虽然标题没有改变
    • 您可以在第二个片段 onCreateView() 方法中设置标题,例如.... getActivity().setTitle("Your title");
    猜你喜欢
    • 2017-03-25
    • 2016-06-12
    • 2022-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-19
    • 2020-05-31
    • 1970-01-01
    相关资源
    最近更新 更多