0

我內部有一些Activity單一全屏Fragment。在那個Fragment裏面我有另一個Fragment疊加在屏幕的底部。因此,在屏幕的頂部部分我有內FragmentButton控制可見/隱藏:Android:顯示和隱藏片段

  1. Fragment的佈局:

    ... 
    <FrameLayout 
        android:id="@+id/inner_fragment" 
        android:layout_width="match_parent" 
        android:layout_height="200dp" 
        android:layout_below="@+id/top_part" 
        android:name="com.package.InnerFragment" /> 
    
  2. 的控制Button

    controlButton.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         if (!isInnerVisible) showHideInnerFragment("show"); 
         else showHideInnerFragment("hide"); 
        } 
    }); 
    
  3. 幫手法showHideInnerFragment

    private void showHideInnerFragment(String param) { 
        //variables are class members... 
        fragmentManager = getFragmentManager(); 
        fragmentTransaction = fragmentManager.beginTransaction(); 
        innerFragment = new InnerFragment(); 
        if (param.equals("show")) { 
         fragmentTransaction.add(R.id.inner_fragment, innerFragment); 
         isInnerVisible = true; 
        } else { 
         fragmentTransaction.remove(innerFragment); 
         isInnerVisible = false; 
        } 
        fragmentTransaction.addToBackStack(null); 
        fragmentTransaction.commit(); 
    } 
    

好 - 這完全不工作...

+0

showHideInnerFragment()或? – mjp66

+0

@ mjp66錯字固定.... – michael

+1

謝謝!我會在一分鐘後發佈一個答案,但它看起來像只調用方法與您的參數始終傳遞爲「顯示」 – mjp66

回答

1

看來,你的isInnerVisible永遠是假的(我假設它開始了爲假),用於刪除片段,但保持isInnerVisible假。你可能只需要圍繞切換:

private void showHideInnerFragment(String param) { 
    //variables are class members... 
    fragmentManager = getFragmentManager(); 
    fragmentTransaction = fragmentManager.beginTransaction(); 
    innerFragment = new InnerFragment(); 
    if (param.equals("show")) { 
     fragmentTransaction.add(R.id.inner_fragment, innerFragment); 
     isInnerVisible = false; 
    } else { 
     fragmentTransaction.remove(innerFragment); 
     isInnerVisible = true; 
    } 
    fragmentTransaction.addToBackStack(null); 
    fragmentTransaction.commit(); 
} 

我也想傳遞一個真/假值的參數,而不是一個字符串值,這使得它更易於理解的代碼是幹什麼的,因爲它是現在它有點不合理。我會在一個位的例子更新;)

編輯:這裏有一個更好的方式做你想要什麼:showHideGroupsFragment()

controlButton.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     showHideInnerFragment(isInnerVisible); 
    } 
}); 

private void showHideInnerFragment(boolean isVisible) { 
    //variables are class members... 
    fragmentManager = getFragmentManager(); 
    fragmentTransaction = fragmentManager.beginTransaction(); 
    innerFragment = new InnerFragment(); 
    if (isVisible == false) { 
     fragmentTransaction.add(R.id.inner_fragment, innerFragment); 
     isInnerVisible = true; 
    } else { 
     fragmentTransaction.remove(innerFragment); 
     isInnerVisible = false; 
    } 
    fragmentTransaction.addToBackStack(null); 
    fragmentTransaction.commit(); 
} 
+0

它仍然無法正常工作。我懷疑用'FragmentManager'添加和刪除方法是不正確的,應該是其他方法? – michael

+0

嗯。我曾經做過類似於你想要做的事情,並沒有問題。有可能我沒有看到明顯的東西。如何發佈xml只是踢? – mjp66