一.静态Fragment
1.定义Fragment的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000000">
<TextView
android:textSize="20sp"
android:text="This is headlines fragment"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2.创建对应布局文件的Fragment类
对与Fragment类的布局文件的加载需要使用动态加载LayoutInflater.inflater()
获取LayoutInflater实例的三种方式
1.LayoutInflater inflater = getLayoutInflater();在Activity中获取实例对象
2.LayoutInflater inflater = (LayoutInflater) context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
3.LayoutInflater inflater = LayoutInflater.from(context);
public class HeadlinesFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_headlines, container, false);
}
}
定义显示Fragment的Activity的布局文件
在布局文件中需要添加Fragment的位置添加
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="定义的Fragment对应类"
android:id="@+id/article_fragment"/>
定义显示Activity的类
public class FragmentActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
}
}
二.动态Fragment
1.定义需要动态添加的Fragment布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#ffff00">
<TextView
android:text="This is another fragment"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
2.定义需要动态添加的Fragment类
public class anotherFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_another, container, false);
}
}
3.Activity的布局文件中添加Fragment容器
<FrameLayout
android:id="@+id/right_layout"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.myfirst.myfirst.HeadlinesFragment"
android:id="@+id/headlines_fragment"/>
</FrameLayout>
4.获取FragmentManager
在活动中可以直接获取FragmentManager对象
FragmentManager fragmentManager = getFragmentManager();
5.开启一个事务,通过调用beginTransaction()方法开启
FragmentTransaction transaction = fragmentManager
.beginTransaction();
6.调用替换的方法
传入的两个参数,第一个是放置Fragmnet的容器的id,第二个参数是新的Fragment对象的实例对象
transaction.replace(R.id.right_layout,fragment);
7.提交事务
transaction.commit();
所有代码
fragment_article.xml
<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:text="Button"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
ArticleFragment.java
public class ArticleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_article, container, false);
}
}
fragment_headlines.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#000000">
<TextView
android:textSize="20sp"
android:text="This is headlines fragment"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
HeadlinesFragment.java
public class HeadlinesFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_headlines, container, false);
}
}
another_fragment.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#ffff00">
<TextView
android:text="This is another fragment"
android:textSize="20sp"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
anotherFragment.java
public class anotherFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_another, container, false);
}
}
activity_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<fragment
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:name="com.example.myfirst.myfirst.ArticleFragment"
android:id="@+id/article_fragment"/>
<FrameLayout
android:id="@+id/right_layout"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.example.myfirst.myfirst.HeadlinesFragment"
android:id="@+id/headlines_fragment"/>
</FrameLayout>
</LinearLayout>
FragmentActivity.java
public class FragmentActivity extends Activity implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v){
switch (v.getId()){
case R.id.button:
anotherFragment fragment = new anotherFragment();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout,fragment);
//使按下back键可以返回到上一个碎片
transaction.addToBackStack(null);
transaction.commit();
break;
default:
break;
}
}
}
以上是对Fragment学习的记录。