Fragment 碎片
底部按钮实现上方界面的切换
布局
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 用帧布局去实现界面布局切换 -->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/group"
android:id="@+id/frame_layout"></FrameLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:id="@+id/group">
<RadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableTop="@drawable/home_select"
android:button="@null"
android:text="首页"
android:id="@+id/home"
android:gravity="center"
android:textColor="@drawable/txt"
android:checked="true"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableTop="@drawable/tv_select"
android:button="@null"
android:text="直播"
android:id="@+id/tv"
android:gravity="center"
android:textColor="@drawable/txt"/>
<RadioButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:drawableTop="@drawable/me_select"
android:button="@null"
android:text="我的"
android:id="@+id/me"
android:gravity="center"
android:textColor="@drawable/txt"/>
</RadioGroup>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
RadioGroup radioGroup;
FrameLayout frameLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioGroup = findViewById(R.id.group);
frameLayout = findViewById(R.id.frame_layout);
//初始显示
Home home = new Home();
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,home).commit();
//底部按钮点击事件
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.home:
Home home = new Home();
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,home).commit();//更换帧布局内容
break;
case R.id.tv:
TV tv = new TV();
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,tv).commit();//更换帧布局内容
break;
case R.id.me:
Me me = new Me();
getSupportFragmentManager().beginTransaction().replace(R.id.frame_layout,me).commit();//更换帧布局内容
break;
}
}
});
}
}
Fragment 类
public class Home extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.home, null);
Button btn = view.findViewById(R.id.home_btn);
return view;
}
}
Fragment常用方法:
1.添加Fragment方法:add()
2.替换Fragment方法:replace()
3.移除Fragment方法:remove()
4.隐藏/显示Fragment方法:hide()/show()
参数介绍:第一个要替换布局的id,第二个要添加Fragment对象
使用
1.获得FragmentManager对象
FragmentManager fragmentManager=getFragmentManager();
2:开启事务
FragmentTransaction transaction = fragmentManager.beginTransaction();
3:通过FragmentTransaction 调用add()、replace()方法管理fragment
(添加到回退栈中 addToBackStack(“名字”))
4:transaction .commit();
Fragment的生命周期
1.onAttach() :Fragment与Activity有联系。
2.onCreate():创建Fragment
3.onCreateView():创建Fragment视图,尽量不要做耗时操作
4.onActivityCreated():当Activity中的onCreate方法执行完后调用。
5.onStart():启动。
6.onResume():可见
7.onPause():不可见
8.onStop():停止。
9. onDestroyView() :销毁Fragment视图
10.onDestroy():销毁fragment对象
11.onDetach():Fragment和Activity解除关联的时候调用