第一行代码Android学习:第四部分主要涉及到碎片的简单使用、动态添加碎片、在碎片中模拟返回栈、碎片和活动之间相互通信和碎片的生命周期
1.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="horizontal" tools:context=".MainActivity" > <fragment android:id="@+id/fg_left" android:name="com.example.dyhdm_04_00fragmenttest.LeftFragment" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" /> <FrameLayout android:id="@+id/fl_right" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" > <fragment android:id="@+id/fg_right" android:name="com.example.dyhdm_04_00fragmenttest.RightFragment" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> </LinearLayout> 2.fragment_left.xml <?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" > <Button android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="Button" /> </LinearLayout> 3.fragment_right.xml <?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:background="#00ff00" android:orientation="vertical" > <TextView android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="this is right fragment" android:textSize="20sp" /> </LinearLayout> 4.fragment_right_other.xml <?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:background="#ffff00" android:orientation="vertical" > <TextView android:id="@+id/bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:text="this is other right fragment" android:textSize="20sp" /> </LinearLayout> 5.LeftFragment.java /* * @Title: LeftFragment.java * @Description: TODO * @author: 张志安 * @date: 2016-8-15 下午3:33:41 * */ package com.example.dyhdm_04_00fragmenttest; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; /** * TODO 左面的fragment * @author 张志安 * @date: 2016-8-15 下午3:33:41 */ public class LeftFragment extends Fragment { /** * 重载方法 */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_left, container, false); getActivityMehtod(); return view; } /** * 用于在 Activity获取的方法 */ public void testMethod(){ Toast.makeText(getActivity(), "LeftFragment", Toast.LENGTH_SHORT).show(); } /** * TODO 在fragment中获取activity的方法 * * @throw * @return void */ protected void getActivityMehtod() { MainActivity activity = (MainActivity)getActivity(); Log.e("zza", activity.testMethod()+"123"); } } 6.RightFragment.java /* * @Title: LeftFragment.java * @Description: TODO * @author: 张志安 * @date: 2016-8-15 下午3:33:41 * */ package com.example.dyhdm_04_00fragmenttest; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * TODO 左面的fragment * * @author 张志安 * @date: 2016-8-15 下午3:33:41 */ public class RightFragment extends Fragment { /** * 重载方法 */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { Log.e("zza", "onCreateView"); View view = inflater.inflate(R.layout.fragment_right, container, false); return view; } /** * 重载方法 */ @Override public void onAttach(Activity activity) { super.onAttach(activity); Log.e("zza", "onAttach"); } /** * 重载方法 */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.e("zza", "onCreate"); } /** * 重载方法 */ @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); Log.e("zza", "onActivityCreated"); } /** * 重载方法 */ @Override public void onStart() { super.onStart(); Log.e("zza", "onStart"); } /** * 重载方法 */ @Override public void onResume() { super.onResume(); Log.e("zza", "onResume"); } /** * 重载方法 */ @Override public void onPause() { super.onPause(); Log.e("zza", "onPause"); } /** * 重载方法 */ @Override public void onStop() { super.onStop(); Log.e("zza", "onStop"); } /** * 重载方法 */ @Override public void onDestroy() { super.onDestroy(); Log.e("zza", "onDestroy"); } /** * 重载方法 */ @Override public void onDestroyView() { super.onDestroyView(); Log.e("zza", "onDestroyView"); } /** * 重载方法 */ @Override public void onDetach() { super.onDetach(); Log.e("zza", "onDetach"); } } 7.AnotherRightFragment.java /* * @Title: LeftFragment.java * @Description: TODO * @author: 张志安 * @date: 2016-8-15 下午3:33:41 * */ package com.example.dyhdm_04_00fragmenttest; import android.app.Fragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /** * TODO 左面的fragment * @author 张志安 * @date: 2016-8-15 下午3:33:41 */ public class AnotherRightFragment extends Fragment { /** * 重载方法 */ @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_right_other, container, false); return view; } } 8.MainActivity.java package com.example.dyhdm_04_00fragmenttest; import android.app.Activity; 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; import android.widget.Toast; public class MainActivity extends Activity { private Button bt; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); bt = (Button) findViewById(R.id.bt); bt.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 切换fragment的方法 // 1.创建待添加碎片的实例 AnotherRightFragment fragment = new AnotherRightFragment(); // 2.通过getFragmentManager()获取FragmentManager FragmentManager fragmentManager = getFragmentManager(); // 3.通过beginTransaction()开启一个事务 FragmentTransaction fragmentTransaction = fragmentManager .beginTransaction(); // 4.向容器内加入碎片,一般使用replace(),传入容器的id和碎片的实例 fragmentTransaction.replace(R.id.fl_right, fragment); // 将一个事务添加到返回栈中,非必须,不加按下返回直接退出 fragmentTransaction.addToBackStack(null); // 5.使用commit()提交事务 fragmentTransaction.commit(); getFragmentMehtod(); } }); } /** * TODO 在activity中获取fragment的方法 * * @throw * @return void */ protected void getFragmentMehtod() { LeftFragment leftFragment = (LeftFragment) getFragmentManager() .findFragmentById(R.id.fg_left); leftFragment.testMethod(); } /** * 用于在 fragment获取的方法 */ public String testMethod(){ Toast.makeText(MainActivity.this, "Activity", Toast.LENGTH_SHORT).show(); return "Activity"; } }代码下载地址
