這個demo示範了隐藏和顯示fragment的方法,利用hide和show方法,但需要注意的是 這兩個方法在FragmentTransaction身上,要使用這兩個方法就必須首先開啟事務。
* 同時這裡示範了兩種儲存資料的方式,一種是利用fragment的onsaveInstance方法 另一種是通過設定控件儲存資料的屬性實作
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="@string/hello_world" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/hide1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hide"/>
<fragment
android:name="com.fishtosky.fragmenthideandshow.MainActivity$FirstFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/first"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<Button
android:id="@+id/hide2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hide"/>
<fragment
android:name="com.fishtosky.fragmenthideandshow.MainActivity$SecondFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/second"/>
</LinearLayout>
</LinearLayout>
fragment_content.xml:Fragment的布局檔案
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/tv_tip"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/edit_text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="initial text"
android:background="#00ff00"
android:freezesText="true"/>
<!-- freezesText="true"設定為true表示控件将儲存内容和光标的位置 -->
</LinearLayout>
MainActivity
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//定義方法将fragment與hide按鈕關聯起來
FragmentManager fm=getFragmentManager();
addShowHideListener(R.id.hide1,fm.findFragmentById(R.id.first));
addShowHideListener(R.id.hide2,fm.findFragmentById(R.id.second));
}
private void addShowHideListener(int buttonId, final Fragment fragment) {
final Button bt=(Button) findViewById(buttonId);
bt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//開啟fragment事務
FragmentTransaction ft=getFragmentManager().beginTransaction();
//設定fragment的切換動畫
ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
if(fragment.isHidden()){
bt.setText("Hide");
ft.show(fragment);
}else{
bt.setText("Show");
ft.hide(fragment);
}
ft.commit();
}
});
}
public static class FirstFragment extends Fragment {
private TextView et;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_content, container, false);
TextView tv=(TextView) v.findViewById(R.id.tv_tip);
tv.setText("The fragment saves and restores the text");
et=(TextView) v.findViewById(R.id.edit_text);
if(savedInstanceState!=null){
String text=savedInstanceState.getString("text");
et.setText(text);
}
return v;
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
String value=(String) et.getText();
outState.putString("text", value);
}
}
public static class SecondFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v=inflater.inflate(R.layout.fragment_content, container, false);
TextView tv=(TextView) v.findViewById(R.id.tv_tip);
tv.setText("The TextView saves and restores the text");
EditText et=(EditText) v.findViewById(R.id.edit_text);
//雖然我們在布局檔案中設定了EditText儲存内容和光标位置的屬性,但由于這個布局
//檔案是和其它fragment共享的,是以還需要在這裡進行設定
et.setSaveEnabled(true);
return v;
}
}
}