Android_手机开发-Fragment、FragmentManager、FragmentTransaction的综合使用
南昌航空大学实验报告
2014 年05 月08 日
课程名称:Android 实验名称:Fragment、FragmentManager、FragmentTransaction的综合使用班级:姓名:同组人:
指导教师评定:签名:
一、实验目的
熟悉Fragment、FragmentManager、FragmentTransaction之间的综合使用;
熟悉Fragment之间的切换。
二、实验要求
1、编写一个聊天记录,要求如下:
使用RelativeLayout布局,Button、EditText、ListView、TextView等控件实现简单布局;
重写onCreateView方法;
编写点击监听器(Onclicklistener和OnItemClickListener);
实现简单Fragment切换。
●检查布局是否可以在虚拟机显示
●点击按钮是否可用
●简单Fragment切换是否可以正常使用
编写测试程序。
三、实验步骤
①、
1.先编写RelativeLayout实现Fragment的主页面;
2.编写activity extends Fragment初始化按钮;
3.编写监听器,实现Fragment之间的切换;
4.测试切换是否可正常使用。
四、参考源代码
activity_main.xml
android:id="@+id/fragment_content" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/f">
android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_alignParentBottom="true" > android:id="@+id/activity_str" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world"/>
Fragment1.xml
android:id="@+id/fragment_1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world"/> Fragment2.xml android:id="@+id/changyongyu" android:layout_width="match_parent" android:layout_height="130dp"> listview_item.xml android:id="@+id/item" android:layout_height="10dp" android:layout_width="match_parent" android:textSize="9sp" /> record_fragment.xml android:id="@+id/title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="聊天记录" android:gravity="center" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" /> android:layout_width="fill_parent" android:layout_height="200dp" android:background="@drawable/i" android:layout_below="@id/title" > android:id="@+id/record" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world"/> Fragment.java public class Fragment extends android.support.v4.app.Fragment{ @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View contentView = inflater.inflate(https://www.360docs.net/doc/bf13518697.html,yout.fragment1, null); TextView title = (TextView) contentView.findViewById(R.id.fragment_1); title.setText("my first fragment"); return contentView; } } Fragment2.java public class Fragment2 extends Fragment{ private EditText edit; private Button button; private ListView listView; private String [] strs; private FragmentManager manager; private FragmentTransaction transaction; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View contentView = inflater.inflate(https://www.360docs.net/doc/bf13518697.html,yout.fragment2, null); manager = getFragmentManager(); transaction = manager.beginTransaction(); Fragment f1 = new Fragment(); transaction.add(R.id.fragment_content, f1); listView = (ListView) contentView.findViewById(R.id.changyongyu); SimpleAdapter adapter = new SimpleAdapter(getActivity().getApplicationContext(), getData(), https://www.360docs.net/doc/bf13518697.html,yout.listview_item, new String[]{"common"}, new int []{R.id.item}); listView.setAdapter(adapter); return contentView; } private ArrayList ArrayList strs = new String []{ "Hello!","Hi!","Nice to meet you!","Nice to meet you,too!","What are you doing!","How are you!", }; for(int i=0;i HashMap map.put("common", strs[i]); list.add(map); } return list; } @Override public void onResume() { super.onResume(); listView.setOnItemClickListener(new Listener().itemListener); }; public class Listener{ public OnItemClickListener itemListener = new OnItemClickListener() { @Override public void onItemClick(AdapterView> arg0, View arg1, int position, long arg3) { // TODO Auto-generated method stub com.example.fragment.Fragment record = new com.example.fragment.Fragment(); transaction = manager.beginTransaction(); transaction.replace(R.id.fragment_content, record); transaction.addToBackStack(null); https://www.360docs.net/doc/bf13518697.html,mit(); String str = strs[position]; EditText text = (EditText)getActivity().findViewById(R.id.activity_str); text.setText(str); } }; } } RecordFragment.java public class RecordFragment extends Fragment{ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View contentView = inflater.inflate(https://www.360docs.net/doc/bf13518697.html,yout.record_fragment, null); TextView record = (TextView) contentView.findViewById(R.id.record); String str = getActivity().getIntent().getStringExtra("str"); record.setText(str); return contentView; } } MainActivity.java public class MainActivity extends FragmentActivity { //private StringBuffer aStr = new StringBuffer(""); private EditText activityStr; private FragmentManager manager; private FragmentTransaction transaction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(https://www.360docs.net/doc/bf13518697.html,yout.activity_main); Button send = (Button) findViewById(R.id.send_activity); activityStr = (EditText) findViewById(R.id.activity_str); send.setOnClickListener(sendListener); // FrameLayout content = (FrameLayout) findViewById(R.id.fragment_content); manager = getSupportFragmentManager(); transaction = manager.beginTransaction(); Fragment f1 = new Fragment(); transaction.add(R.id.fragment_content, f1); https://www.360docs.net/doc/bf13518697.html,mit(); Button record = (Button) findViewById(R.id.record); Button strs = (Button) findViewById(R.id.strs); strs.setOnClickListener(strListener); record.setOnClickListener(recordListener); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } private OnClickListener sendListener = new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub TextView f1 = (TextView) findViewById(R.id.fragment_1); // aStr = aStr.append(f1.getText().toString()); String message = activityStr.getText().toString(); StringBuffer str = new StringBuffer(); str = str.append(getIntent().getStringExtra("str")).append(message).append("\n"); String text = null; if(str.length()>200){ text = str.toString().substring(str.length()-200, str.length()); }else { text = str.toString(); } getIntent().putExtra("str", str.toString()); f1.setText(text); } }; private OnClickListener recordListener = new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub RecordFragment record = new RecordFragment(); transaction = manager.beginTransaction(); transaction.replace(R.id.fragment_content, record); transaction.addToBackStack(null); https://www.360docs.net/doc/bf13518697.html,mit(); } }; private OnClickListener strListener = new OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub Fragment2 str = new Fragment2(); transaction = manager.beginTransaction(); transaction.replace(R.id.fragment_content, str); transaction.addToBackStack(null); https://www.360docs.net/doc/bf13518697.html,mit(); } }; } 五、实验结果 六、 实验体会 1、掌握Fragment 、FragmentManager 、FragmentTransaction 之间的综合使用; 2、掌握Fragment 之间的切换机制; 3、理解Activity 的生命周期,确定在哪触发监听事件。