本人初学Fragment,写有几个简单例子,现提出来方便大家学习和研究。
本程序的重点是在一个Activity中放入了两个Fragment。
页面布局如下面代码(fragment_hide_show.xml):
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical|center_horizontal" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Demonstration of hiding and showing fragments." /> <LinearLayout android:orientation="horizontal" android:padding="4dip" android:gravity="center_vertical" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/frag1hide" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hide" /> <fragment android:name="polycom.com.cn.FirstFragment" android:id="@+id/fragment1" android:layout_weight="1" android:layout_width="0px" android:layout_height="wrap_content" /> </LinearLayout> <LinearLayout android:orientation="horizontal" android:padding="4dip" android:gravity="center_vertical" android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/frag2hide" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hide" /> <fragment android:name="polycom.com.cn.SecondFragment" android:id="@+id/fragment2" android:layout_weight="1" android:layout_width="0px" android:layout_height="wrap_content" /> </LinearLayout> </LinearLayout>
FragmentActivity.java
import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class FragmentActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_hide_show); FragmentManager fm = getFragmentManager(); //下面的两行代码是在Activity中按钮添加监听事件,控制Fragment的显示和隐藏 addShowHideListener(R.id.frag1hide, fm.findFragmentById(R.id.fragment1)); addShowHideListener(R.id.frag2hide, fm.findFragmentById(R.id.fragment2)); } void addShowHideListener(int buttonId, final Fragment fragment) { //获取activity中的button final Button button = (Button)findViewById(buttonId); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { FragmentTransaction ft = getFragmentManager().beginTransaction(); /*为Fragment设置淡入淡出效果,Android开发网提示这里这两个动画资源是android内部资源无需我们手动定义。*/ ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out); if (fragment.isHidden()) { ft.show(fragment); button.setText("隐藏"); } else { ft.hide(fragment); button.setText("显示"); } ft.commit(); } }); } }
import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class FirstFragment extends Fragment { TextView mTextView; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //从文件 example_fragment.xml 加载了一个layout View v = inflater.inflate(R.layout.labeled_text_edit, container, true); View tv = v.findViewById(R.id.msg); ((TextView)tv).setText("The fragment saves and restores this text."); mTextView = (TextView)v.findViewById(R.id.saved); if (savedInstanceState != null) { mTextView.setText(savedInstanceState.getCharSequence("text")); } return v; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putCharSequence("text", mTextView.getText()); } }SecondFragment.java
import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; public class SecondFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { //从文件 example_fragment.xml 加载了一个layout View v = inflater.inflate(R.layout.labeled_text_edit, container, true); View tv = v.findViewById(R.id.msg); ((TextView)tv).setText("The TextView saves and restores this text."); //另外一种TextView的保存模式 ((TextView)v.findViewById(R.id.saved)).setSaveEnabled(true); return v; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:padding="4dip" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/msg" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="0" android:paddingBottom="4dip" /> <EditText android:id="@+id/saved" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="@string/initial_text" android:freezesText="true"> <requestFocus /> </EditText> </LinearLayout>