工作内容:
1.Fragment类与Activity生命周期
2.Fragment的动态加载(重要)和静态加载
3.Fragment与ViewPager的联合使用
学习分享:
一、Fragment类与Activity生命周期
执行顺序:
Activity_onCreate
Fragment_onCreate
Fragment_onCreateView
Fragment_onViewCreated
Fragment_onActivityCreated
Activity_onStart
Fragment_onStart
Activity_onResume
Fragment_onResume
点击了list图标,进入home界面
Fragment_onPause
Activity_onPause
Fragment_onStop
Activity_onStop
回到test页面
Activity_onRestart
Activity_onStart
Fragment_onStart
Activity_onResume
Fragment_onResume
点返回键销毁Activity
Fragment_onPause
Activity_onPause
Fragment_onStop
Activity_onStop
Fragment_onDestroyView
Fragment_onDestroy
Fragment_onDestroy
Activity_onDestroy
二、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="wrap_content" android:padding="6dp" android:orientation="horizontal"> <ImageView android:layout_width="30dp" android:layout_height="30dp" android:background="@drawable/reset" android:id="@+id/iv_frag2" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="News " android:textSize="30sp" android:layout_marginLeft="10dp" android:id="@+id/tv_frag2" /> </LinearLayout>
使用这个fragment:在Activity的布局文件中加入下面代码import android.os.Bundle;import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.TextView; /** * Created by Administrator on 2016/9/1. */ public class Frag_2 extends Fragment { private View view ; private TextView tv_frag2; private ImageView iv_frag2; /** * 必须重写的方法 * @param inflater 用于解析View 【可以理解为:将布局文件与java文件绑定】 * @param container 父容器(可以是FrameLayout,LinearLayout等等) * @param savedInstanceState bundle对象用于传值 * @return */ @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.frag_2,container,false); return view; } /** * 在onCreateView之后执行 * @param view onCreateView中解析得到的view(一个布局文件) * @param savedInstanceState */ @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); initView(); } private void initView() { tv_frag2 = (TextView)view.findViewById(R.id.tv_frag2); iv_frag2 = (ImageView)view.findViewById(R.id.iv_frag2); iv_frag2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("LOGKEY---->","点击了frag2的书信图标"); } }); } }import android.os.Bundle;import android.support.annotation.Nullable;import android.support.v4.app.Fragment;import android.util.Log;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.TextView;/** * Created by Administrator on 2016/9/1. */public class Frag_2 extends Fragment { private View view ; private TextView tv_frag2; private ImageView iv_frag2; /** * 必须重写的方法 * @param inflater 用于解析View * @param container 父容器(可以是FrameLayout,LinearLayout等等) * @param savedInstanceState bundle对象用于传值 * @return */ @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { view = inflater.inflate(R.layout.frag_2,container,false); return view; } /** * 在onCreateView之后执行 * @param view onCreateView中解析得到的view(一个布局文件) * @param savedInstanceState */ @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); initView(); } private void initView() { tv_frag2 = (TextView)view.findViewById(R.id.tv_frag2); iv_frag2 = (ImageView)view.findViewById(R.id.iv_frag2); iv_frag2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.e("LOGKEY---->","点击了frag2的书信图标"); } }); }}
<fragment android:layout_width="match_parent" android:layout_height="wrap_content" android:name="com.plane.people.planebattle.Frag_2" android:id="@+id/fragment" android:layout_gravity="center_horizontal" />
Fragment动态加载(重要——常用)
这里讲几个Fragment的实现类动态加载进一个FrameLayout
xml文件:(放了一个ImageView)
frag_spring.xml 对应Fragment类:public class Fragment_spring extends Fragment{}
frag_summer.xml 对应Fragment类:public class Fragment_summer extends Fragment{}
frag_autumn.xml 对应Fragment类:public class Fragment_autumn extends Fragment{}
frag_winter.xml 对应Fragment类:public class Fragment_winter extends Fragment{}
frag_spring.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"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/spring" /> </LinearLayout>Java代码:
【这里继承的Fragment 选择包:android.support.v4.app.Fragment】
原因:Fragment 放入ViewPager中时可以使用一个适配器:android.support.v4.app.FragmentPagerAdapter,这样可以很简单的完成他们之间的融合。
import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.Toast; /** * Created by Administrator on 2016/9/1. */ public class Fragment_spring extends Fragment { @Nullable @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.frag_spring,container,false); } @Override public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); ImageView imageView = (ImageView)view.findViewById(R.id.iv_spring); imageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getActivity(), "春天来了", Toast.LENGTH_SHORT).show(); } }); } }Activity的代码:
import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.FrameLayout; public class FragmentTestActivity extends AppCompatActivity implements View.OnClickListener{ private FrameLayout frameLayout; private Fragment fragment_sp,fragment_su,fragment_au,fragment_wi; private FragmentManager fm; private FragmentTransaction ft; private int [] tvIDs = {R.id.tv_sp,R.id.tv_su,R.id.tv_au,R.id.tv_wi};//TextView的id数组 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_test_view_pager); initListener(); initData(); } //添加点击事件 private void initListener() { for (int i = 0; i < tvIDs.length; i++) { findViewById(tvIDs[i]).setOnClickListener(this); } } //初始化数据 private void initData() { /** * (引入包为android.app时可以使用getFragmentManager()获取FragmentManager) * 使用getSupportFragmentManager()获取 FragmentManager 需要当前Activity继承FragmentActivity * 或其子类(AppCompatActivity是其子类) * 【引入import android.support.v4.app.类名,需使用使用getSupportFragmentManager()】 */ fm = getSupportFragmentManager(); frameLayout = (FrameLayout)findViewById(R.id.frame_test); //设置默认显示 ft = fm.beginTransaction(); fragment_sp = new Fragment_spring(); ft.replace(R.id.frame_test,fragment_sp); ft.commit(); } /** * 实现点击几个TextView显示对应的Fragment * @param v */ @Override public void onClick(View v) { ft = fm.beginTransaction(); //开始事务(提交一次就需要开启一次) switch (v.getId()){ case R.id.tv_sp: if(fragment_sp == null){ fragment_sp = new Fragment_spring(); } /** * 用fragment_sp来替换FrameLayout中的内容 * ft.replace 相当于先 ft.remove 后 ft.add */ ft.replace(R.id.frame_test,fragment_sp); break; case R.id.tv_su: if(fragment_su == null){ fragment_su = new Fragment_summer(); } ft.replace(R.id.frame_test,fragment_su); break; case R.id.tv_au: if(fragment_au == null){ fragment_au = new Fragment_autumn(); } ft.replace(R.id.frame_test,fragment_au); break; case R.id.tv_wi: if(fragment_wi == null){ fragment_wi = new Fragment_winter(); } ft.replace(R.id.frame_test,fragment_wi); break; } ft.commit(); //必须要—用于提交事务 } }演示效果:【点击屏幕下方的文字显示对应的Fragment,点击图片,提示“春天来了”,“夏天来了”等字样】