一、Fragment是什么
碎片(Fragment)是一种可以嵌入在活动当中的UI片段,它能让程序更加合理和充分地利用大屏幕空间,因而在平板上应用的非常广泛
嵌入在 活动中的UI片段,放在v4包,可以很好的兼容
二、Fragment静态加载怎么用
在一个活动中添加两个碎片,并让这两个碎片平分活动空间,代码如下
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@color/brown"
tools:context=".fragment.HelloFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Hello blank fragment"/>
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@color/red"
tools:context=".fragment.ListFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
三、Fragment动态加载怎么用
碎片A
<?xml version="1.0" encoding="utf-8"?>
<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=".fragment.AFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@color/chocolate">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</LinearLayout>
</LinearLayout>
碎片B
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@color/darkgreen"
tools:context=".fragment.BFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
碎片C
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@color/darkgray"
tools:context=".fragment.CFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Main3Activity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:id="@+id/fragment_three">
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:text="消息"
android:gravity="center"
android:textSize="25sp"
android:id="@+id/message"
android:layout_weight="1"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textSize="25sp"
android:id="@+id/dong"
android:text="动态"/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:textSize="25sp"
android:id="@+id/tel"
android:text="联系人"/>
</LinearLayout>
</LinearLayout>
运行结果图
四、ViewPager+Fragment实现页卡滑动
Activity页面代码
package com.example.a17708.myapplication;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import com.example.a17708.myapplication.fragment.MyFragment;
import com.example.a17708.myapplication.fragment.ContactFragment;
import com.example.a17708.myapplication.fragment.FriendsFragment;
import java.util.ArrayList;
import java.util.List;
public class VPActivity extends AppCompatActivity implements View.OnClickListener{
private TextView cont;
private TextView friend;
private TextView my;
private View cont_view;
private View friend_view;
private View my_view;
private FriendsFragment friendsFragment;
private MyFragment myFragment;
private ContactFragment contactFragment;
private List<Fragment> fragmentList=new ArrayList<>();
private ViewPager viewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vp);
cont=findViewById(R.id.cont);
friend=findViewById(R.id.friend);
my=findViewById(R.id.my);
cont.setOnClickListener(this);
friend.setOnClickListener(this);
my.setOnClickListener(this);
cont_view=findViewById(R.id.cont_view);
friend_view=findViewById(R.id.friend_view);
my_view=findViewById(R.id.my_view);
viewPager=findViewById(R.id.vp);
contactFragment=new ContactFragment();
friendsFragment=new FriendsFragment();
myFragment=new MyFragment();
fragmentList.add(contactFragment);
fragmentList.add(friendsFragment);
fragmentList.add(myFragment);
MyPagerAdapter adapter=new MyPagerAdapter(getSupportFragmentManager(),fragmentList);
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
cont_view.setBackgroundColor(Color.GREEN);
friend_view.setBackgroundColor(Color.GREEN);
my_view.setBackgroundColor(Color.GREEN);
switch (position) {
case 0:
cont_view.setBackgroundColor(Color.RED);
break;
case 1:
friend_view.setBackgroundColor(Color.RED);
break;
case 2:
my_view.setBackgroundColor(Color.RED);
break;
default:
break;
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
}
@Override
public void onClick(View v) {
switch (viewPager.getId()){
case R.id.cont:
viewPager.setCurrentItem(0);
break;
case R.id.friend:
viewPager.setCurrentItem(1);
break;
case R.id.my:
viewPager.setCurrentItem(2);
break;
default:
break;
}
}
}
适配器代码
package com.example.a17708.myapplication;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import java.util.List;
public class MyPagerAdapter extends FragmentPagerAdapter{
private List<Fragment> fragmentList;
public MyPagerAdapter(FragmentManager fm,List<Fragment> fragmentList) {
super(fm);
this.fragmentList=fragmentList;
}
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
}
contact碎片代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@color/pink"
tools:context=".fragment.ContactFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
friends碎片代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="@color/peachpuff"
tools:context=".fragment.FriendsFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment" />
</FrameLayout>
运行结果图
五、Fragment生命周期
1.运行状态
当一个碎片是可见的,并且它所关联的活动正处于运行状态时,该碎片也处于运行状态
2.
-onAttach(activity):当Fragment与activity发生关联是调用。(依附)
-oncreate()-oncreateview(Layoutinflater,ViewGroup,Bundle):创建该fragment的视图
-onActivityCreated(Bundle):当activity的oncreate方法返回时调用-onstart()-onresume()-碎片已激活-onpause()-onstop()-onDestoryview()-onDestory()-onDetach()-碎片被销毁
场景演示 : 切换到该Fragment:onAttach- onCreate-onCreateView-onActivityCreated- onStart-onResume
屏幕灭掉:onPause-onSaveInstanceState- onStop
屏幕解锁:onStart-onResume
切换到其他Fragment:onPause–onStoponDestroyView
切换回本身的Fragment: onCreateView–onActivityCreated-onStart–onResume
回到桌面:onPause–onSaveInstanceState–onStop
回到应用:onStart–onResume
退出应用:onPause-onStop-onDestroyView-onDestroy-onDetach
六、fragment通信
1.创建一个接口,并在接口里面创建用于处理回调的函数,函数上面声明一个参数,参数的数据类型是Fragment之间通讯的数据类型
2.在一个Fragment中创建用于注册回调的函数,函数上面声明一个参数,参数的数据类型是接口的类型
3.在另外的一个Fragment中创建一个内部类,并且将内部类实现回调的接口并实现接口中的函数,然后在外部类中通过第一个Fragment的类名直接调用第一个Fragment中用于注册回调的函数,将自己内部类的对象作为参数传递
4.实现数据传递时,只要第一个Fragment调用接口中处理数据的函数,那另外一个Fragment就会收到传递的数据