Frament是什么
碎片frament是一种可以嵌入在活动中的UI片段,它能让程序更加合理和充分的利用大屏幕的空间,因而在平板上应用广泛。碎片frament和活动activity很像,也可以包含布局,也有自己的生命周期。
如何使用碎片frament才能最大限度的利用平板屏幕的空间呢?如果要开发一个新闻应用app,其中一个界面使用listview展示一组新闻标题,当点击了其中的一个标题,就打开另一个界面显示新闻的详细内容。如果是在手机中设计,那么我们可以将新闻标题列表放在一个活动中,将新闻的详细内容放在另一个活动中。而如果在平板上也这么设计,新闻标题就会被拉长至填充整个平板的屏幕,这样会出现界面有大片的空白区域的问题,导致界面不够美观。因此在平板中设计时最好的设计就是将新闻标题列表界面和新闻的详细内容界面分别放在两个碎片中,然后再同一个活动activity中引入这两个碎片,这样就可以达到充分利用屏幕空间的目的。
Frament的使用方式
Fragment的简单用法
新建项目FramentTest,在一个活动中添加两个碎片,让两个碎片平分空间。
新建左侧碎片布局文件,left_frament.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/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button"/>
</LinearLayout>
然后新建右侧碎片布局文件right_frament.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"
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 the right frament"/>
</LinearLayout>
这里将布局背景设置为绿色,放置了一个textview控件显示一段文字。
然后创建LeftFrament类继承自Frament
package com.example.test.framenttest;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class LeftFrament extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_frament,container,false);
return view;
}
}
这里仅仅是重写了onCreateView方法然后再这个方法中通过LayoutInflater的inflate方法将上面定义的left_frament布局加载进来,然后再同样的方法创建RightFrament类,代码如下:
package com.example.test.framenttest;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class RightFrament extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.right_frament,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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.framenttest.MainActivity">
<fragment
android:id="@+id/left_frament"
android:name="com.example.test.framenttest.LeftFrament"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<fragment
android:id="@+id/right_frament"
android:name="com.example.test.framenttest.RightFrament"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
使用标签来在布局中添加碎片,其中android:name属性来显示指明添加的碎片类名,需要将类的包名也加上。
运行效果如下:
动态添加碎片Fragment
上面介绍了在布局文件中加载碎片的方法,碎片的真正强大之处在于它可以在程序运行时动态的添加到活动中,根据具体情况来添加碎片就可以使程序界面更加美观。
在上面代码的基础上新建another_right_frament.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"
android:background="#ffff00">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="this is another right frament"/>
</LinearLayout>
这个布局文件只是将背景颜色修改为了黄色,其他都一样,然后新建类AnotherRightFrament,代码如下:
package com.example.test.framenttest;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AnotherRightFrament extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.another_right_frament,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:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test.framenttest.MainActivity">
<fragment
android:id="@+id/left_frament"
android:name="com.example.test.framenttest.LeftFrament"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/>
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<fragment
android:id="@+id/right_frament"
android:name="com.example.test.framenttest.RightFrament"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</FrameLayout>
</LinearLayout>
这里我们添加了FrameLayout布局文件,将碎片right_frament包括其中。,下面修改代码在代码中替换FrameLayout中的布局文件,如下:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button =(Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
AnotherRightFrament frament = new AnotherRightFrament();
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction =fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.right_layout,frament);
fragmentTransaction.commit();
break;
}
}
这里我们把布局替换的逻辑加在了button的按钮点击事件中,当点击按钮后界面会有布局变化。动态替换碎片的步骤:
(1)创建待加载的碎片实例
(2)获取FramentManager,在活动中可以直接调用getFramentManager方法得到
(3)开启一个事务,通过调用beginTranction方法
(4)向容器中加入碎片,一般调用replace方法实现,需要传入容器的id和待添加的碎片实例
(5)提交事务,调用commit方法完成。