簡體   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