这个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;
}
}
}