Fragment 的创建与 Activity 的创建类似,要创建一个 Fragment 必须要创建一个类继承自 Fragment 。
1.新建一个左侧的碎片布局文件 left _ layout .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="单击我" />
</LinearLayout>
2.新建一个右侧的碎片布局文件 right_ layout .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:background="@android:color/darker_gray"
android:orientation="vertical" >
<ImageView
android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="60dp"
android:src="@mipmap/ic_launcher"
/>
</LinearLayout>
3.新建一个testLeft_Fragment类,继承自Fragment
package com.example.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class testLeft_Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.left_layout,container,false);
return view;
}
}
4.这里仅仅是重写了 Fragment 的 onCreateView ()方法,然后这个方法中通过LayoutInflater的 inflate ()方法将刚才定义的 left _ layout 布局动态加载进来,然后再新建一个 testRight _ Fragment 类,继承自 Fragment 。
package com.example.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class testRight_Fragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.right_layout,container,false);
return view;
}
}
5.新建second_right_fragment.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:background="@drawable/bg"
android:orientation="vertical"
>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="我是左边单击出来的" />
</LinearLayout>
6.新建testSecondRightFragment作为另一个右侧碎片
package com.example.fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class testSecondRightFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view=inflater.inflate(R.layout.second_right_fragment,container,false);
return view;
}
}
7.修改activity_main.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="horizontal" >
<fragment
android:id="@+id/left_fragment"
android:name="com.example.fragment.testLeft_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="@+id/right_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<!--可以在这个容器中动态加载Fragment-->
<fragment
android:id="@+id/right_fragment"
android:name="com.example.fragment.testRight_Fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
8.可以看到,现在将右侧碎片放在了一个 FrameLayout 中,这是 Android 中最简单的一种布局,它没有任何的定位方式,所有的控件都会摆放在布局的左上角。由于这里仅需要在布局中放人一个碎片,因此非常适合使用 FrameLayout 。之后将在代码中替换 FrameLayout 里的内容,从而实现动态添加碎片的功能。
修改 MainAetivity 中的代码:
package com.example.fragment;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends FragmentActivity implements View.OnClickListener {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button:
testSecondRightFragment secFragment=new testSecondRightFragment();
FragmentManager fragmentManager=getSupportFragmentManager();
FragmentTransaction transaction=fragmentManager.beginTransaction();
transaction.replace(R.id.right_layout,secFragment);
transaction.addToBackStack(null);
transaction.commit();
break;
default:
break;
}
}
}
可以看到,首先给左侧碎片中的按钮注册了一个单击事件,然后将动态添加碎片的逻辑都放在了单击事件中进行。结合代码可以看出,动态添加碎片主要分为如下5步:
(1)创建待添加的碎片实例。
(2)获取到 FragmentManager ,在活动中可以直接调用 getFragmentManager ()方法得到。
(3)开启一个事务,通过调用 beginTransaction ()方法开启。
(4)向容器内加人碎片一般使用 replace ()方法实现,需要传人容器的 id 和待添加的碎片实例。
(5)提交事务,调用 commit()方法来完成。