一,碎片是什么
碎片(Fragment)是一种可以嵌入在活动中的UI它可以让程序更加合理的利用大屏幕的空间。
二,碎片的使用方式
1.静态添加碎片
新建一个左侧碎片布局left_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button"/>
</LinearLayout>
新建一个右侧布局碎片
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="This is right fragment" />
</LinearLayout>
新建一个LeftFragment类并让他继承自Fragment 选择support-v4库中的Fragment
public class LeftFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment,container,false);
return view;
}
}
同样新建一个RightFragment类
public class RightFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate( R.layout.right_fragment, container, false );
return view;
}
}
修改activity_main.xml引入这两个Fragment
<?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"
tools:context="com.example.wangyamin.fragmenttest.MainActivity">
<fragment
android:id="@+id/left_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.example.wangyamin.fragmenttest.LeftFragment"/>
<Fragment
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"></Fragment>
</LinearLayout>
2.动态添加碎片
在上面的基础上新建another_right_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffff00">
<TextView
android:id="@+id/another_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="This is another right fragment"/>
</LinearLayout>
新建AnotherRightFragment 作为右侧碎片
public class AnothreRightFragment extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate( R.layout.another_right_fragment, container,false);
return view;
}
}
修改activity_main.xml 将右侧的碎片替换成布局
<?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"
tools:context="com.example.wangyamin.fragmenttest.MainActivity">
<fragment
android:id="@+id/left_fragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.example.wangyamin.fragmenttest.LeftFragment"/>
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"></FrameLayout>
</LinearLayout>
动态的添加Fragment
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById( R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch ( v.getId() ){
case R.id.button:
replaceFragment( new AnothreRightFragment() );
}
}
});
replaceFragment( new RightFragment() );
}
private void replaceFragment(android.support.v4.app.Fragment fragment){
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace( R.id.right_layout, fragment );
transaction.addToBackStack(null);
transaction.commit();
}
}
动态添加的碎片的步骤
创建待添加的碎片的实例
获取FragmentManger,在活动中可以直接调用getSupportFragmentManger()方法
开启一个事务,通过调用beginTransaction()方法开启
向容器中添加或者替换碎片,一般使用replace方法实现
提交事务,调用commit()方法实现
transaction.addToBackStack(null);
这个方法可以让保存上个碎片,你退出一个碎片,而不是一个activity
碎片和活动间的通信
活动中获得碎片
RightFragment RightFragment= (RightFragment)getFragmentManger().findFragmentById( R.id.right_fragment);
碎片中调用活动的方法
MainActivity activity = (MainActivity)getActivity();
三,碎片的生命周期和回调
1.运行状态
2.暂停状态
3.停止状态
4.销毁状态
四,限定符动态添加Fragment
修改activity_main.xml
<?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"
tools:context="com.example.wangyamin.fragmenttest.MainActivity">
<fragment
android:id="@+id/left_fragment"
android:layout_height="match_parent"
android:layout_weight="match_parent"
android:name="com.example.wangyamin.fragmenttest.LeftFragment"/>
</LinearLayout>
res/下新建layout-large文件夹,下面也新建一个名为activity_main.xml的布局包含两个Fragment
当系统认为是大屏幕就会加载layout-large文件夹下的布局即双页模式
使用最小宽度限定符(单位dp)
在res/新建layout-sw600dp 意味着设备宽度大于600dp时会加载layout-sw600dp下的布局,小于600dp时会加载默认layout下的布局