当前位置:   article > 正文

Android 自定义BaseFragment

Android 自定义BaseFragment
直接上代码:

BaseFragment代码

  1. package com.example.custom.fragment;
  2. import android.content.Context;
  3. import android.os.Bundle;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import androidx.annotation.NonNull;
  8. import androidx.annotation.Nullable;
  9. import androidx.fragment.app.Fragment;
  10. /**
  11. * 基本Fragment
  12. * */
  13. public abstract class BaseFragment extends Fragment {
  14. /**
  15. * 设置数据
  16. * */
  17. protected abstract void setData(Bundle savedInstanceState);
  18. /**
  19. * 绑定布局
  20. * */
  21. protected abstract int setContentLayout();
  22. /**
  23. * 初始化组件
  24. * */
  25. protected abstract void setControls(View view);
  26. @Override
  27. public void onAttach(@NonNull Context context) {
  28. super.onAttach(context);
  29. }
  30. @Override
  31. public void onCreate(@Nullable Bundle savedInstanceState) {
  32. super.onCreate(savedInstanceState);
  33. setData(savedInstanceState);
  34. }
  35. @Nullable
  36. @Override
  37. public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  38. View fragmentView = inflater.inflate(setContentLayout(),container,false);
  39. return fragmentView;
  40. }
  41. @Override
  42. public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
  43. super.onViewCreated(view, savedInstanceState);
  44. // 控件
  45. setControls(view);
  46. }
  47. @Override
  48. public void onStart() {
  49. super.onStart();
  50. }
  51. @Override
  52. public void onResume() {
  53. super.onResume();
  54. }
  55. @Override
  56. public void onPause() {
  57. super.onPause();
  58. }
  59. @Override
  60. public void onStop() {
  61. super.onStop();
  62. }
  63. @Override
  64. public void onDestroy() {
  65. super.onDestroy();
  66. }
  67. @Override
  68. public void onDetach() {
  69. super.onDetach();
  70. }
  71. }

HomeFragment代码:

  1. package com.example.custom.fragment;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.widget.TextView;
  5. import com.example.custom.R;
  6. public class HomeFragment extends BaseFragment{
  7. private TextView mainTv;
  8. @Override
  9. protected void setData(Bundle savedInstanceState) {
  10. }
  11. @Override
  12. protected int setContentLayout() {
  13. return R.layout.fragment_home;
  14. }
  15. @Override
  16. protected void setControls(View view) {
  17. mainTv = view.findViewById(R.id.mainTV);
  18. mainTv.setText("Home页面");
  19. }
  20. }

HomeFragment布局代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical">
  6. <TextView
  7. android:id="@+id/mainTV"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. android:gravity="center"
  11. android:text="home"/>
  12. </LinearLayout>

使用:

(1) main_layout.xml代码

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5. <androidx.fragment.app.FragmentContainerView
  6. android:id="@+id/fragment_container_view"
  7. android:layout_width="match_parent"
  8. android:layout_height="match_parent"/>
  9. <LinearLayout
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:layout_alignParentBottom="true"
  13. android:orientation="horizontal">
  14. <Button
  15. android:id="@+id/homeBtn"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:layout_weight="1"
  19. android:text="home"/>
  20. <Button
  21. android:id="@+id/shoppingBtn"
  22. android:layout_width="match_parent"
  23. android:layout_height="wrap_content"
  24. android:layout_weight="1"
  25. android:text="shopping"/>
  26. <Button
  27. android:id="@+id/myBtn"
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:layout_weight="1"
  31. android:text="my"/>
  32. </LinearLayout>
  33. </RelativeLayout>

(2) MainActivity.java代码:

  1. package com.example.custom.main;
  2. import android.os.Bundle;
  3. import android.view.View;
  4. import android.widget.Button;
  5. import androidx.fragment.app.FragmentManager;
  6. import androidx.fragment.app.FragmentTransaction;
  7. import com.example.custom.R;
  8. import com.example.custom.activity.BaseActivity;
  9. import com.example.custom.fragment.HomeFragment;
  10. import com.example.custom.fragment.MyFragment;
  11. import com.example.custom.fragment.ShoppingFragment;
  12. public class MainActivity extends BaseActivity {
  13. private Button home,shop,my;
  14. // 获取Fragment管理对象(此方法只能在继承AppCompatActivity中使用)
  15. private FragmentManager fragmentManager = getSupportFragmentManager();
  16. private FragmentTransaction transaction;
  17. // fragment
  18. private HomeFragment homeFragment = new HomeFragment();
  19. private ShoppingFragment shoppingFragment = new ShoppingFragment();
  20. private MyFragment myFragment = new MyFragment();
  21. @Override
  22. public void setData(Bundle savedInstanceState) {
  23. // 竖屏
  24. setScreenPortrait(true);
  25. }
  26. @Override
  27. public int setContentLayout() {
  28. return R.layout.main_layout;
  29. }
  30. @Override
  31. public void setControls() {
  32. home = findViewById(R.id.homeBtn);
  33. home.setOnClickListener(homeClick);
  34. shop = findViewById(R.id.shoppingBtn);
  35. shop.setOnClickListener(shopClick);
  36. my = findViewById(R.id.myBtn);
  37. my.setOnClickListener(myClick);
  38. // 默认home
  39. // 必须写下面的三条语句
  40. transaction = fragmentManager.beginTransaction();
  41. transaction.replace(R.id.fragment_container_view,homeFragment);
  42. transaction.commit();
  43. }
  44. View.OnClickListener homeClick = new View.OnClickListener() {
  45. @Override
  46. public void onClick(View view) {
  47. // 必须写下面的三条语句
  48. transaction = fragmentManager.beginTransaction();
  49. transaction.replace(R.id.fragment_container_view,homeFragment);
  50. transaction.commit();
  51. }
  52. };
  53. View.OnClickListener shopClick = new View.OnClickListener() {
  54. @Override
  55. public void onClick(View view) {
  56. // 必须写下面的三条语句
  57. transaction = fragmentManager.beginTransaction();
  58. transaction.replace(R.id.fragment_container_view,shoppingFragment);
  59. transaction.commit();
  60. }
  61. };
  62. View.OnClickListener myClick = new View.OnClickListener() {
  63. @Override
  64. public void onClick(View view) {
  65. // 必须写下面的三条语句
  66. transaction = fragmentManager.beginTransaction();
  67. transaction.replace(R.id.fragment_container_view,myFragment);
  68. transaction.commit();
  69. }
  70. };
  71. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/83712
推荐阅读
相关标签
  

闽ICP备14008679号