Fragment是什么?
Fragment(片段,碎片),正如其翻译那样,它在一个activity里面表示一个行为或者用户接口的一部分(碎片表示轻量级和灵活)。我们可以将不同的Fragments组合起来放到一个activity中,或者在不同的activity中重用一个fragment。你可以将一个fragment看成是一个activity中的一个片段,它有自己的生命周期(如何从创建到销毁),接受它自己的输入事件,并且可以在activity运行时动态的添加或者移除一个fragment(类似一个可以在不同的activity中重用的子activity)。
Fragment静态加载怎么使用?
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.example.fly.myapplication20180604.BlankFragment1"></fragment>
简单来说创建一个Fragment文件,然后将其放入到MainActivity中的布局文件中去,即为Fragment的静态加载,这也是Fragment最简单的使用;
Fragment动态加载怎么使用?
<!--在布局文件中加上一个FrameLayout将其充满-->
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5"
>
</FrameLayout>
将FrameLayout放入MainActivity的布局文件当中,而在Class文件当中,运用到两个方法:
fragmentManager=getFragmentManager();
fragmentTransaction=fragmentManager.beginTransaction();
使用Activity的getSupportFragmentManager() .beginTransaction();获得FragmentManager对象.
@Override
public void onClick(View v) {
fragmentTransaction=fragmentManager.beginTransaction();
switch (v.getId()){
case R.id.btn1:
if(blankFragment==null){
blankFragment=new BlankFragment();
}
fragmentTransaction.replace(R.id.frame,blankFragment);
break;
case R.id.btn2:
if(blankFragment1==null){
blankFragment1=new BlankFragment1();
}
fragmentTransaction.replace(R.id.frame,blankFragment1);
break;
case R.id.btn3:
if(blankFragment2==null){
blankFragment2=new BlankFragment2();
}
fragmentTransaction.replace(R.id.frame,blankFragment2);
break;
default:
break;
}
fragmentTransaction.commit();
再调用事务的一系列方法,达到切换Fragmen界面的效果。
ViewPager+Fragment实现页卡滑动
下面所举的例子就是一个简单的小东西,它是由ViewPager加上三个Fragment实现页卡滑动的
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!--在布局文件中加上一个FrameLayout将其充满-->
<FrameLayout
android:id="@+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="5"
>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_weight="1">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="消息"
android:id="@+id/btn1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="联系人"
android:id="@+id/btn2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="动态"
android:id="@+id/btn3"/>
</LinearLayout>
</LinearLayout>
再通过创建一个新的ViewPager的Activity中的布局文件中加上如下一个ViewPager
<android.support.v4.view.ViewPager
android:id="@+id/vp_main"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
在Class文件中需要创建一个适配器并绑定该适配器
MyPagerAdapter adapter = new MyPagerAdapter(getSupportFragmentManager(), fragmentList);
viewPager.setAdapter(adapter);
再通过加上ViewPager的监听事件结合起来实现ViewPager+Fragment的页卡滑动
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
xiaoxi_view.setBackgroundColor(Color.GRAY);
lianxiren_view.setBackgroundColor(Color.GRAY);
dongtai_view.setBackgroundColor(Color.GRAY);
tv1.setTextColor(Color.GRAY);
tv2.setTextColor(Color.GRAY);
tv3.setTextColor(Color.GRAY);
switch (position) {
case 0:
xiaoxi_view.setBackgroundColor(Color.YELLOW);
tv1.setTextColor(Color.YELLOW);
break;
case 1:
lianxiren_view.setBackgroundColor(Color.YELLOW);
tv2.setTextColor(Color.YELLOW);
break;
case 2:
dongtai_view.setBackgroundColor(Color.YELLOW);
tv3.setTextColor(Color.YELLOW);
break;
default:
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
Fragment与Activity的通信
1、在Fragment中调用Activity中的方法:
Fragment可以通过getActivity()方法来获得Activity的实例,然后就可以调用一些例如findViewById()之类的方法
View listView = getActivity().findViewById(R.id.list);
在Fragment中的点击事件中添加一段文字,这个时候,点击Fragment中的按钮,让Activity中的TextView显示出这段文字。
其实就是让Fragment中的文本显示在Activity中:
View view=inflater.inflate(R.layout.fragment_blank, container, false);
button=view.findViewById(R.id.btn_title);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
VPActivity vpActivity= (VPActivity) getActivity();
vpActivity.motify("我是消息Fragment");
}
});
return view;
}
通过getActivity()方法来获得Activity的实例,然后就可以调用findViewById()的方法得到其中的EditText控件。
2、从Activity传值给Fragment
通过是用Bundle对象来传入东西和拿出东西,其中最重要的一个方法即为
blankFragment.setArguments();
它起到将其传输到Fragment的作用,下面是全部的代码:
Bundle bundle=new Bundle();
bundle.putString("name","张三");
blankFragment.setArguments(bundle);
Bundle bundle=getArguments();
String name=bundle.getString("name");
button.setText(name);
return view;
上面两段分别是Activity和Fragment中的代码块,通过这两个方法实现了Activity传值到Fragment中去;
Fragment的生命周期
Fragment必须是依存与Activity而存在的,因此Activity的生命周期会直接影响到Fragment的生命周期。官网这张图很好的说明了两者生命周期的关系:
可以看到Fragment比Activity多了几个额外的生命周期回调方法:
onAttach(Activity)
当Fragment与Activity发生关联时调用。
onCreateView(LayoutInflater, ViewGroup,Bundle)
创建该Fragment的视图
onActivityCreated(Bundle)
当Activity的onCreate方法返回时调用
onDestoryView()
与onCreateView想对应,当该Fragment的视图被移除时调用
onDetach()
与onAttach相对应,当Fragment与Activity关联被取消时调用