Fragment作为Android的一个基本组件,学会使用它还是很方便的,当然第一次接触Fragment,我还是很蒙的,真的是不会使用。但是随着时间的积累,我慢慢的也懂了,想想刚开始的无奈,现在真是有一点小开森啊^_^
好了,开始讲使用的方法了。
Fragment有两种是使用的方法,根据使用的方法不一样,它分为动态使用和静态使用,下面我分别进行介绍。
要使用Fragment,我们要先新建一个类来继承Fragment,下面我给出两个类:
public class BottomFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View _View = inflater.inflate(R.layout.fragment_bottom_layout, container,false);
return _View;
}
}
public class CenterFragment extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View _View = inflater.inflate(R.layout.fragment_center_layout, container,false);
return _View;
}
}
在这两个类里面,我们需要有两个布局文件:
<?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:background="#ff0000"
android:orientation="vertical" >
</LinearLayout></span>
因为我们只是一个示例,所以我就不往布局文件里放什么控件了,只需要一个简单的文件就行了。
然后我给大家看一下MainActivity的布局文件,也就是Fragment的使用方法1--动态使用:
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.fragment_demo.MainActivity" >
<FrameLayout
android:id="@+id/layout_center"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="5"
android:background="#ff0000"
>
</FrameLayout>
<FrameLayout
android:id="@+id/layout_bottom"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#00ff00"
>
</FrameLayout>
</LinearLayout>
在这个布局文件里我们放了两个FrameLayout,这就是用来放Fragment的,我们需要动态的将Fragment放在FrameLayout中,这个在MainACtivity中实现.
MainActivity.java
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/**
* java代码动态添加一个Fragment到Activity
*/
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.add(R.id.layout_center,new CenterFragment());
transaction.add(R.id.layout_bottom, new BottomFragment());
transaction.commit();
}
}
这就是动态的加载Fragment,因为它可以加不同的Fragment到布局中,灵活性很高。
下面试静态的加载Fragment,也就是使用方法2:静态加载不要在MainActivity中设置什么,直接在布局文件中进行设置就可以了,这样灵活性比较差。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.fragment_demo.MainActivity" >
<fragment
android:name="com.example.fragment_demo.CenterFragment"
android:id="@+id/fragment_center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="5"
/>
<fragment
android:name="com.example.fragment_demo.BottomFragment"
android:id="@+id/fragment_bottom"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
</LinearLayout>
在布局文件里使用<fragment />标签来设置Fragment,这样虽然方便一点,但是灵活性很差,不建议使用。
好了,Fragment的使用方法我已经说好了,但是具体的使用,比如Fragment和Activity的通信,Fragment和Fragment的通信都是比较复杂的东西,大家可以进一步的深入了解Fragment的使用^_^.