繁体   English   中英

Android:浏览片段时更改活动标题; 单击返回按钮时带回相同的标题

[英]Android: Change activity title when navigating fragments; Bring back same titles when back button clicked

我有一个名为Categories_collection的片段,应用程序最初使用该片段启动,活动标题是应用程序标题。

第1类
2类
-2.1类
-2.2类
第3类

我做的是,当用户点击该有小孩我一个类别重新调用一个名为母公司额外字段相同categories_collection片段加载是点击类别的儿童的类别。 这一切都已经完成并且运行良好。

我需要的是以下

  1. 用户单击类别2。主活动标题更改为“类别2”
  2. 现在,用户在单击孩子时正在浏览“类别2”的孩子。 活动标题将更改为子类别名称,即“类别2.1”,依此类推。
  3. 最重要的是,单击“后退按钮”时,活动的标题将更改为原来的标题。 所以可以说我从打开子类别返回,因此标题继续更改为“类别2”,因为我继续浏览“类别2”的子级

我希望这可以解释我的需求。

谢谢。

制作方法并在backpressed上调用它:

void onBack() {
//Find all your fragments with id
        Categroy_Child_Fragment fr = (Categroy_Child_Fragment) fragmentManager
                .findFragmentByTag("list_items");

        Final_Categories_Fragment fr1 = (Final_Categories_Fragment) fragmentManager
                .findFragmentByTag("last");
        Grid_View_Fragment_For_All fr2 = (Grid_View_Fragment_For_All) fragmentManager
                .findFragmentByTag("grid_view");


//Then check if they are null or not and according to that remove them and settitle to actionbar

        if (fr2 != null) {
            fragmentManager
                    .beginTransaction()
                    .setCustomAnimations(R.anim.out_to_left, R.anim.out_to_left)
                    .remove(fr2).commit();

            actionBar.setTitle("title");
        } else if (fr1 != null) {
            fragmentManager
                    .beginTransaction()
                    .setCustomAnimations(R.anim.bottom_down, R.anim.bottom_down)
                    .remove(fr1).commit();

            actionBar.setTitle("title");

        } else if (fr != null) {
            fragmentManager.beginTransaction()
                    .setCustomAnimations(R.anim.right_out, R.anim.right_out)
                    .remove(fr).commit();

            actionBar.setTitle(R.string.all_cat);

        } else {

            super.onBackPressed();
        }
    }

在每个应该更改其活动标题的片段中,我都会在片段的onResume()方法中放置类似的内容。

@Override
public void onResume() {
    super.onResume();
    ((ParentActivity) getActivity()).setTitle("FragmentTitle"); 
}

然后必须在您的活动中创建应更改其标题的setTitle(String title)方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM