2016-09-20 123 views

回答

0

我試過這個,它爲我工作。首先,我在第一次點擊按鈕時添加了片段,然後在隨後的附加和分離按鈕上添加了片段。所以它創建了片段,然後不破壞它只顯示並隱藏它。

這是代碼....最初計數是0時在MainActivity首次創建

 public void Settings(View view){ 

     if(count==0){ 
     count++; 
     // add a fragment for the first time 
     MyFragment frag=new MyFragment(); 
     FragmentTransaction ft=manager.beginTransaction(); 
     ft.add(R.id.group,frag,"A"); 
     ft.commit(); 
     }else{ 

     //check if fragment is visible, if no, then attach a fragment 
     //else if its already visible,detach it 
     Fragment frag=manager.findFragmentByTag("A"); 
     if(frag.isVisible() && frag!=null){ 
      FragmentTransaction ft=manager.beginTransaction(); 
      ft.detach(frag); 
      ft.commit(); 
     }else{ 
      FragmentTransaction ft=manager.beginTransaction(); 
      ft.attach(frag); 
      ft.commit(); 
     } 

    } 
-1

您應該爲此使用對話框片段。對話框片段具有片段所具有的所有生命週期,並具有像對話一樣的行爲。例如要顯示,只需調用dialogFragment.show()方法,並隱藏,調用dialogFragment.dismiss()方法。

下面是一個如何製作對話框片段的例子。

public class DialogFragmentExample extends DialogFragment{ 


@Override 
public void onStart() { 
    super.onStart(); 
// To make dialog fragment full screen. 
    Dialog dialog = getDialog(); 
    if (dialog != null) { 
     dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 
     dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); 

    } 
// 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE); 

    return inflater.inflate(R.layout.your_xml_layout, container, false); 
} 

// initialize your views here 

} 

並顯示此對話框片段;

DialogFragmentExample fragment = new DialogFragmentExample(); 
fragment.show(); 

同樣駁回,

fragment.dismiss(); 

希望這將幫助你!

0

片段交易的內部顯示/隱藏標誌將有所幫助。

FragmentManager fm = getFragmentManager(); 
fm.beginTransaction() 
      .setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out) 
      .show(somefrag) //or hide(somefrag) 
      .commit(); 
相關問題