找回密码
 立即注册
首页 业界区 业界 Android复杂页面组件化策略

Android复杂页面组件化策略

缍米 14 小时前
常见方案对比

方案适用场景特点include 布局静态拆分,不需要复用简单,布局复用自定义 ViewUI+逻辑封装,可复用封装度高Fragment动态加载,独立生命周期灵活,但复杂RecyclerView列表,每行算一个组件最常用实际建议
  1. 复杂页面(如患者详情页)
  2. ├── 患者基本信息卡片   → include 或 自定义 CardView
  3. ├── 医嘱列表区域       → RecyclerView + Adapter
  4. ├── 执行记录区域       → RecyclerView + Adapter
  5. ├── 备注信息区域       → include
  6. └── 底部操作栏<?xml version="1.0" encoding="utf-8"?>
  7. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  8.     android:layout_width="match_parent"
  9.     android:layout_height="match_parent"
  10.     android:orientation="vertical"
  11.     android:padding="16dp">
  12.    
  13.     <com.xx.xxxxx.widget.CaptchaView
  14.         android:id="@+id/captchaView"
  15.         android:layout_width="match_parent"
  16.         android:layout_height="wrap_content" />
  17. </LinearLayout> → include
复制代码
不是越拆越好

  • 2-3 个简单区域 → 直接写一个 XML
  • 重复使用的区域 → 抽成 include 或自定义 View
  • 需要独立管理生命周期 → Fragment
  • 列表类型 → RecyclerView
多组件编码示例

示例场景:登录页拆分
  1. LoginActivity
  2. ├── 顶部 Logo 区域(LogoView)
  3. ├── 账号输入区域(AccountInputView)
  4. ├── 验证码区域(CaptchaView)
  5. └── 登录按钮区域(LoginButtonView)
复制代码
方案 A:include + ViewBinding(简单拆分)

activity_login.xml
  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.     android:padding="16dp">
  7.    
  8.     <include
  9. <?xml version="1.0" encoding="utf-8"?>
  10. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  11.     android:layout_width="match_parent"
  12.     android:layout_height="match_parent"
  13.     android:orientation="vertical"
  14.     android:padding="16dp">
  15.    
  16.     <com.xx.xxxxx.widget.CaptchaView
  17.         android:id="@+id/captchaView"
  18.         android:layout_width="match_parent"
  19.         android:layout_height="wrap_content" />
  20. </LinearLayout>android:id="@+id/layout_logo"
  21. <?xml version="1.0" encoding="utf-8"?>
  22. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  23.     android:layout_width="match_parent"
  24.     android:layout_height="match_parent"
  25.     android:orientation="vertical"
  26.     android:padding="16dp">
  27.    
  28.     <com.xx.xxxxx.widget.CaptchaView
  29.         android:id="@+id/captchaView"
  30.         android:layout_width="match_parent"
  31.         android:layout_height="wrap_content" />
  32. </LinearLayout>layout="@layout/layout_login_logo" />
  33.    
  34.     <include
  35. <?xml version="1.0" encoding="utf-8"?>
  36. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  37.     android:layout_width="match_parent"
  38.     android:layout_height="match_parent"
  39.     android:orientation="vertical"
  40.     android:padding="16dp">
  41.    
  42.     <com.xx.xxxxx.widget.CaptchaView
  43.         android:id="@+id/captchaView"
  44.         android:layout_width="match_parent"
  45.         android:layout_height="wrap_content" />
  46. </LinearLayout>android:id="@+id/layout_account"
  47. <?xml version="1.0" encoding="utf-8"?>
  48. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  49.     android:layout_width="match_parent"
  50.     android:layout_height="match_parent"
  51.     android:orientation="vertical"
  52.     android:padding="16dp">
  53.    
  54.     <com.xx.xxxxx.widget.CaptchaView
  55.         android:id="@+id/captchaView"
  56.         android:layout_width="match_parent"
  57.         android:layout_height="wrap_content" />
  58. </LinearLayout>layout="@layout/layout_account_input" />
  59.    
  60.     <include
  61. <?xml version="1.0" encoding="utf-8"?>
  62. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  63.     android:layout_width="match_parent"
  64.     android:layout_height="match_parent"
  65.     android:orientation="vertical"
  66.     android:padding="16dp">
  67.    
  68.     <com.xx.xxxxx.widget.CaptchaView
  69.         android:id="@+id/captchaView"
  70.         android:layout_width="match_parent"
  71.         android:layout_height="wrap_content" />
  72. </LinearLayout>android:id="@+id/layout_captcha"
  73. <?xml version="1.0" encoding="utf-8"?>
  74. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  75.     android:layout_width="match_parent"
  76.     android:layout_height="match_parent"
  77.     android:orientation="vertical"
  78.     android:padding="16dp">
  79.    
  80.     <com.xx.xxxxx.widget.CaptchaView
  81.         android:id="@+id/captchaView"
  82.         android:layout_width="match_parent"
  83.         android:layout_height="wrap_content" />
  84. </LinearLayout>layout="@layout/layout_captcha" />
  85.    
  86.     <include
  87. <?xml version="1.0" encoding="utf-8"?>
  88. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  89.     android:layout_width="match_parent"
  90.     android:layout_height="match_parent"
  91.     android:orientation="vertical"
  92.     android:padding="16dp">
  93.    
  94.     <com.xx.xxxxx.widget.CaptchaView
  95.         android:id="@+id/captchaView"
  96.         android:layout_width="match_parent"
  97.         android:layout_height="wrap_content" />
  98. </LinearLayout>android:id="@+id/layout_login_btn"
  99. <?xml version="1.0" encoding="utf-8"?>
  100. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  101.     android:layout_width="match_parent"
  102.     android:layout_height="match_parent"
  103.     android:orientation="vertical"
  104.     android:padding="16dp">
  105.    
  106.     <com.xx.xxxxx.widget.CaptchaView
  107.         android:id="@+id/captchaView"
  108.         android:layout_width="match_parent"
  109.         android:layout_height="wrap_content" />
  110. </LinearLayout>layout="@layout/layout_login_button" />
  111. </LinearLayout>
复制代码
LoginActivity.java
  1. public class LoginActivity extends BaseActivity {
  2.     @Override
  3.     protected void initView(Bundle savedInstanceState) {
  4. <?xml version="1.0" encoding="utf-8"?>
  5. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  6.     android:layout_width="match_parent"
  7.     android:layout_height="match_parent"
  8.     android:orientation="vertical"
  9.     android:padding="16dp">
  10.    
  11.     <com.xx.xxxxx.widget.CaptchaView
  12.         android:id="@+id/captchaView"
  13.         android:layout_width="match_parent"
  14.         android:layout_height="wrap_content" />
  15. </LinearLayout>// 每个子布局都可以单独操作
  16. <?xml version="1.0" encoding="utf-8"?>
  17. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  18.     android:layout_width="match_parent"
  19.     android:layout_height="match_parent"
  20.     android:orientation="vertical"
  21.     android:padding="16dp">
  22.    
  23.     <com.xx.xxxxx.widget.CaptchaView
  24.         android:id="@+id/captchaView"
  25.         android:layout_width="match_parent"
  26.         android:layout_height="wrap_content" />
  27. </LinearLayout>binding.layoutLogo.setLogo(R.drawable.ic_logo);
  28. <?xml version="1.0" encoding="utf-8"?>
  29. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  30.     android:layout_width="match_parent"
  31.     android:layout_height="match_parent"
  32.     android:orientation="vertical"
  33.     android:padding="16dp">
  34.    
  35.     <com.xx.xxxxx.widget.CaptchaView
  36.         android:id="@+id/captchaView"
  37.         android:layout_width="match_parent"
  38.         android:layout_height="wrap_content" />
  39. </LinearLayout>binding.layoutAccount.setHint("请输入账号");
  40. <?xml version="1.0" encoding="utf-8"?>
  41. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  42.     android:layout_width="match_parent"
  43.     android:layout_height="match_parent"
  44.     android:orientation="vertical"
  45.     android:padding="16dp">
  46.    
  47.     <com.xx.xxxxx.widget.CaptchaView
  48.         android:id="@+id/captchaView"
  49.         android:layout_width="match_parent"
  50.         android:layout_height="wrap_content" />
  51. </LinearLayout>binding.layoutCaptcha.setOnRefreshListener(() -> viewModel.loadCaptcha());
  52. <?xml version="1.0" encoding="utf-8"?>
  53. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  54.     android:layout_width="match_parent"
  55.     android:layout_height="match_parent"
  56.     android:orientation="vertical"
  57.     android:padding="16dp">
  58.    
  59.     <com.xx.xxxxx.widget.CaptchaView
  60.         android:id="@+id/captchaView"
  61.         android:layout_width="match_parent"
  62.         android:layout_height="wrap_content" />
  63. </LinearLayout>binding.layoutLoginBtn.setOnClickListener(() -> submitLogin());
  64.     }
  65.     @Override
  66.     protected void observeViewModel() {
  67. <?xml version="1.0" encoding="utf-8"?>
  68. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  69.     android:layout_width="match_parent"
  70.     android:layout_height="match_parent"
  71.     android:orientation="vertical"
  72.     android:padding="16dp">
  73.    
  74.     <com.xx.xxxxx.widget.CaptchaView
  75.         android:id="@+id/captchaView"
  76.         android:layout_width="match_parent"
  77.         android:layout_height="wrap_content" />
  78. </LinearLayout>// 集中管理所有 LiveData
  79. <?xml version="1.0" encoding="utf-8"?>
  80. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  81.     android:layout_width="match_parent"
  82.     android:layout_height="match_parent"
  83.     android:orientation="vertical"
  84.     android:padding="16dp">
  85.    
  86.     <com.xx.xxxxx.widget.CaptchaView
  87.         android:id="@+id/captchaView"
  88.         android:layout_width="match_parent"
  89.         android:layout_height="wrap_content" />
  90. </LinearLayout>viewModel.getCaptchaState().observe(this, resource -> {
  91. <?xml version="1.0" encoding="utf-8"?>
  92. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  93.     android:layout_width="match_parent"
  94.     android:layout_height="match_parent"
  95.     android:orientation="vertical"
  96.     android:padding="16dp">
  97.    
  98.     <com.xx.xxxxx.widget.CaptchaView
  99.         android:id="@+id/captchaView"
  100.         android:layout_width="match_parent"
  101.         android:layout_height="wrap_content" />
  102. </LinearLayout>    if (resource.isSuccess()) {
  103. <?xml version="1.0" encoding="utf-8"?>
  104. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  105.     android:layout_width="match_parent"
  106.     android:layout_height="match_parent"
  107.     android:orientation="vertical"
  108.     android:padding="16dp">
  109.    
  110.     <com.xx.xxxxx.widget.CaptchaView
  111.         android:id="@+id/captchaView"
  112.         android:layout_width="match_parent"
  113.         android:layout_height="wrap_content" />
  114. </LinearLayout><?xml version="1.0" encoding="utf-8"?>
  115. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  116.     android:layout_width="match_parent"
  117.     android:layout_height="match_parent"
  118.     android:orientation="vertical"
  119.     android:padding="16dp">
  120.    
  121.     <com.xx.xxxxx.widget.CaptchaView
  122.         android:id="@+id/captchaView"
  123.         android:layout_width="match_parent"
  124.         android:layout_height="wrap_content" />
  125. </LinearLayout>binding.layoutCaptcha.setImage(resource.getData().imageBase64);
  126. <?xml version="1.0" encoding="utf-8"?>
  127. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  128.     android:layout_width="match_parent"
  129.     android:layout_height="match_parent"
  130.     android:orientation="vertical"
  131.     android:padding="16dp">
  132.    
  133.     <com.xx.xxxxx.widget.CaptchaView
  134.         android:id="@+id/captchaView"
  135.         android:layout_width="match_parent"
  136.         android:layout_height="wrap_content" />
  137. </LinearLayout>    }
  138. <?xml version="1.0" encoding="utf-8"?>
  139. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  140.     android:layout_width="match_parent"
  141.     android:layout_height="match_parent"
  142.     android:orientation="vertical"
  143.     android:padding="16dp">
  144.    
  145.     <com.xx.xxxxx.widget.CaptchaView
  146.         android:id="@+id/captchaView"
  147.         android:layout_width="match_parent"
  148.         android:layout_height="wrap_content" />
  149. </LinearLayout>});
  150.     }
  151. }
复制代码
方案 B:自定义 View(封装业务逻辑)

适合复杂且可复用的组件,比如验证码组件:
CaptchaView.java(自定义组件)
  1. public class CaptchaView extends LinearLayout {
  2.     private ActivityCaptchaBinding binding;
  3.     private OnRefreshListener refreshListener;
  4.     public CaptchaView(Context context) {
  5. <?xml version="1.0" encoding="utf-8"?>
  6. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  7.     android:layout_width="match_parent"
  8.     android:layout_height="match_parent"
  9.     android:orientation="vertical"
  10.     android:padding="16dp">
  11.    
  12.     <com.xx.xxxxx.widget.CaptchaView
  13.         android:id="@+id/captchaView"
  14.         android:layout_width="match_parent"
  15.         android:layout_height="wrap_content" />
  16. </LinearLayout>super(context);
  17. <?xml version="1.0" encoding="utf-8"?>
  18. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  19.     android:layout_width="match_parent"
  20.     android:layout_height="match_parent"
  21.     android:orientation="vertical"
  22.     android:padding="16dp">
  23.    
  24.     <com.xx.xxxxx.widget.CaptchaView
  25.         android:id="@+id/captchaView"
  26.         android:layout_width="match_parent"
  27.         android:layout_height="wrap_content" />
  28. </LinearLayout>init();
  29.     }
  30.     private void init() {
  31. <?xml version="1.0" encoding="utf-8"?>
  32. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  33.     android:layout_width="match_parent"
  34.     android:layout_height="match_parent"
  35.     android:orientation="vertical"
  36.     android:padding="16dp">
  37.    
  38.     <com.xx.xxxxx.widget.CaptchaView
  39.         android:id="@+id/captchaView"
  40.         android:layout_width="match_parent"
  41.         android:layout_height="wrap_content" />
  42. </LinearLayout>binding = ActivityCaptchaBinding.inflate(LayoutInflater.from(getContext()), this, true);
  43. <?xml version="1.0" encoding="utf-8"?>
  44. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  45.     android:layout_width="match_parent"
  46.     android:layout_height="match_parent"
  47.     android:orientation="vertical"
  48.     android:padding="16dp">
  49.    
  50.     <com.xx.xxxxx.widget.CaptchaView
  51.         android:id="@+id/captchaView"
  52.         android:layout_width="match_parent"
  53.         android:layout_height="wrap_content" />
  54. </LinearLayout>binding.btnRefresh.setOnClickListener(v -> {
  55. <?xml version="1.0" encoding="utf-8"?>
  56. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  57.     android:layout_width="match_parent"
  58.     android:layout_height="match_parent"
  59.     android:orientation="vertical"
  60.     android:padding="16dp">
  61.    
  62.     <com.xx.xxxxx.widget.CaptchaView
  63.         android:id="@+id/captchaView"
  64.         android:layout_width="match_parent"
  65.         android:layout_height="wrap_content" />
  66. </LinearLayout>    if (refreshListener != null) refreshListener.onRefresh();
  67. <?xml version="1.0" encoding="utf-8"?>
  68. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  69.     android:layout_width="match_parent"
  70.     android:layout_height="match_parent"
  71.     android:orientation="vertical"
  72.     android:padding="16dp">
  73.    
  74.     <com.xx.xxxxx.widget.CaptchaView
  75.         android:id="@+id/captchaView"
  76.         android:layout_width="match_parent"
  77.         android:layout_height="wrap_content" />
  78. </LinearLayout>});
  79.     }
  80.     /** 设置验证码图片 */
  81.     public void setCaptchaImage(String base64) {
  82. <?xml version="1.0" encoding="utf-8"?>
  83. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  84.     android:layout_width="match_parent"
  85.     android:layout_height="match_parent"
  86.     android:orientation="vertical"
  87.     android:padding="16dp">
  88.    
  89.     <com.xx.xxxxx.widget.CaptchaView
  90.         android:id="@+id/captchaView"
  91.         android:layout_width="match_parent"
  92.         android:layout_height="wrap_content" />
  93. </LinearLayout>// Base64 → Bitmap → 设置图片
  94.     }
  95.     /** 获取用户输入的验证码 */
  96.     public String getCaptchaCode() {
  97. <?xml version="1.0" encoding="utf-8"?>
  98. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  99.     android:layout_width="match_parent"
  100.     android:layout_height="match_parent"
  101.     android:orientation="vertical"
  102.     android:padding="16dp">
  103.    
  104.     <com.xx.xxxxx.widget.CaptchaView
  105.         android:id="@+id/captchaView"
  106.         android:layout_width="match_parent"
  107.         android:layout_height="wrap_content" />
  108. </LinearLayout>return binding.etCaptcha.getText().toString().trim();
  109.     }
  110.     /** 清空输入 */
  111.     public void clear() {
  112. <?xml version="1.0" encoding="utf-8"?>
  113. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  114.     android:layout_width="match_parent"
  115.     android:layout_height="match_parent"
  116.     android:orientation="vertical"
  117.     android:padding="16dp">
  118.    
  119.     <com.xx.xxxxx.widget.CaptchaView
  120.         android:id="@+id/captchaView"
  121.         android:layout_width="match_parent"
  122.         android:layout_height="wrap_content" />
  123. </LinearLayout>binding.etCaptcha.setText("");
  124.     }
  125.     /** 显示加载中 */
  126.     public void showLoading() {
  127. <?xml version="1.0" encoding="utf-8"?>
  128. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  129.     android:layout_width="match_parent"
  130.     android:layout_height="match_parent"
  131.     android:orientation="vertical"
  132.     android:padding="16dp">
  133.    
  134.     <com.xx.xxxxx.widget.CaptchaView
  135.         android:id="@+id/captchaView"
  136.         android:layout_width="match_parent"
  137.         android:layout_height="wrap_content" />
  138. </LinearLayout>binding.progressBar.setVisibility(View.VISIBLE);
  139.     }
  140.     public void setOnRefreshListener(OnRefreshListener listener) {
  141. <?xml version="1.0" encoding="utf-8"?>
  142. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  143.     android:layout_width="match_parent"
  144.     android:layout_height="match_parent"
  145.     android:orientation="vertical"
  146.     android:padding="16dp">
  147.    
  148.     <com.xx.xxxxx.widget.CaptchaView
  149.         android:id="@+id/captchaView"
  150.         android:layout_width="match_parent"
  151.         android:layout_height="wrap_content" />
  152. </LinearLayout>this.refreshListener = listener;
  153.     }
  154.     public interface OnRefreshListener {
  155. <?xml version="1.0" encoding="utf-8"?>
  156. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  157.     android:layout_width="match_parent"
  158.     android:layout_height="match_parent"
  159.     android:orientation="vertical"
  160.     android:padding="16dp">
  161.    
  162.     <com.xx.xxxxx.widget.CaptchaView
  163.         android:id="@+id/captchaView"
  164.         android:layout_width="match_parent"
  165.         android:layout_height="wrap_content" />
  166. </LinearLayout>void onRefresh();
  167.     }
  168. }
复制代码
activity_login.xml(使用自定义组件)
  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.     android:padding="16dp">
  7.    
  8.     <com.xx.xxxxx.widget.CaptchaView
  9.         android:id="@+id/captchaView"
  10.         android:layout_width="match_parent"
  11.         android:layout_height="wrap_content" />
  12. </LinearLayout>
复制代码
LoginActivity.java(更简洁)
  1. public class LoginActivity extends BaseActivity {    @Override    protected void initView(Bundle savedInstanceState) {<?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.     android:padding="16dp">
  7.    
  8.     <com.xx.xxxxx.widget.CaptchaView
  9.         android:id="@+id/captchaView"
  10.         android:layout_width="match_parent"
  11.         android:layout_height="wrap_content" />
  12. </LinearLayout>// 验证码组件的逻辑封装在 CaptchaView 内部<?xml version="1.0" encoding="utf-8"?>
  13. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  14.     android:layout_width="match_parent"
  15.     android:layout_height="match_parent"
  16.     android:orientation="vertical"
  17.     android:padding="16dp">
  18.    
  19.     <com.xx.xxxxx.widget.CaptchaView
  20.         android:id="@+id/captchaView"
  21.         android:layout_width="match_parent"
  22.         android:layout_height="wrap_content" />
  23. </LinearLayout>binding.captchaView.setOnRefreshListener(() -> viewModel.loadCaptcha());    }    @Override    protected void observeViewModel() {<?xml version="1.0" encoding="utf-8"?>
  24. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  25.     android:layout_width="match_parent"
  26.     android:layout_height="match_parent"
  27.     android:orientation="vertical"
  28.     android:padding="16dp">
  29.    
  30.     <com.xx.xxxxx.widget.CaptchaView
  31.         android:id="@+id/captchaView"
  32.         android:layout_width="match_parent"
  33.         android:layout_height="wrap_content" />
  34. </LinearLayout>viewModel.getCaptchaState().observe(this, resource -> {<?xml version="1.0" encoding="utf-8"?>
  35. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  36.     android:layout_width="match_parent"
  37.     android:layout_height="match_parent"
  38.     android:orientation="vertical"
  39.     android:padding="16dp">
  40.    
  41.     <com.xx.xxxxx.widget.CaptchaView
  42.         android:id="@+id/captchaView"
  43.         android:layout_width="match_parent"
  44.         android:layout_height="wrap_content" />
  45. </LinearLayout>    if (resource.isSuccess()) {<?xml version="1.0" encoding="utf-8"?>
  46. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  47.     android:layout_width="match_parent"
  48.     android:layout_height="match_parent"
  49.     android:orientation="vertical"
  50.     android:padding="16dp">
  51.    
  52.     <com.xx.xxxxx.widget.CaptchaView
  53.         android:id="@+id/captchaView"
  54.         android:layout_width="match_parent"
  55.         android:layout_height="wrap_content" />
  56. </LinearLayout><?xml version="1.0" encoding="utf-8"?>
  57. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  58.     android:layout_width="match_parent"
  59.     android:layout_height="match_parent"
  60.     android:orientation="vertical"
  61.     android:padding="16dp">
  62.    
  63.     <com.xx.xxxxx.widget.CaptchaView
  64.         android:id="@+id/captchaView"
  65.         android:layout_width="match_parent"
  66.         android:layout_height="wrap_content" />
  67. </LinearLayout>binding.captchaView.setCaptchaImage(resource.getData().imageBase64);<?xml version="1.0" encoding="utf-8"?>
  68. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  69.     android:layout_width="match_parent"
  70.     android:layout_height="match_parent"
  71.     android:orientation="vertical"
  72.     android:padding="16dp">
  73.    
  74.     <com.xx.xxxxx.widget.CaptchaView
  75.         android:id="@+id/captchaView"
  76.         android:layout_width="match_parent"
  77.         android:layout_height="wrap_content" />
  78. </LinearLayout>    }<?xml version="1.0" encoding="utf-8"?>
  79. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  80.     android:layout_width="match_parent"
  81.     android:layout_height="match_parent"
  82.     android:orientation="vertical"
  83.     android:padding="16dp">
  84.    
  85.     <com.xx.xxxxx.widget.CaptchaView
  86.         android:id="@+id/captchaView"
  87.         android:layout_width="match_parent"
  88.         android:layout_height="wrap_content" />
  89. </LinearLayout>});    }    private void submitLogin() {<?xml version="1.0" encoding="utf-8"?>
  90. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  91.     android:layout_width="match_parent"
  92.     android:layout_height="match_parent"
  93.     android:orientation="vertical"
  94.     android:padding="16dp">
  95.    
  96.     <com.xx.xxxxx.widget.CaptchaView
  97.         android:id="@+id/captchaView"
  98.         android:layout_width="match_parent"
  99.         android:layout_height="wrap_content" />
  100. </LinearLayout>String captcha = binding.captchaView.getCaptchaCode();<?xml version="1.0" encoding="utf-8"?>
  101. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  102.     android:layout_width="match_parent"
  103.     android:layout_height="match_parent"
  104.     android:orientation="vertical"
  105.     android:padding="16dp">
  106.    
  107.     <com.xx.xxxxx.widget.CaptchaView
  108.         android:id="@+id/captchaView"
  109.         android:layout_width="match_parent"
  110.         android:layout_height="wrap_content" />
  111. </LinearLayout>// ...    }}
复制代码
方案 C:Fragment(动态拆分)

适合需要独立管理生命周期的场景:
  1. // PatientDetailActivity 使用多个 Fragmentpublic class PatientDetailActivity extends BaseActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {<?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.     android:padding="16dp">
  7.    
  8.     <com.xx.xxxxx.widget.CaptchaView
  9.         android:id="@+id/captchaView"
  10.         android:layout_width="match_parent"
  11.         android:layout_height="wrap_content" />
  12. </LinearLayout>if (savedInstanceState == null) {<?xml version="1.0" encoding="utf-8"?>
  13. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  14.     android:layout_width="match_parent"
  15.     android:layout_height="match_parent"
  16.     android:orientation="vertical"
  17.     android:padding="16dp">
  18.    
  19.     <com.xx.xxxxx.widget.CaptchaView
  20.         android:id="@+id/captchaView"
  21.         android:layout_width="match_parent"
  22.         android:layout_height="wrap_content" />
  23. </LinearLayout>    // 动态添加 Fragment<?xml version="1.0" encoding="utf-8"?>
  24. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  25.     android:layout_width="match_parent"
  26.     android:layout_height="match_parent"
  27.     android:orientation="vertical"
  28.     android:padding="16dp">
  29.    
  30.     <com.xx.xxxxx.widget.CaptchaView
  31.         android:id="@+id/captchaView"
  32.         android:layout_width="match_parent"
  33.         android:layout_height="wrap_content" />
  34. </LinearLayout>    getSupportFragmentManager().beginTransaction()<?xml version="1.0" encoding="utf-8"?>
  35. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  36.     android:layout_width="match_parent"
  37.     android:layout_height="match_parent"
  38.     android:orientation="vertical"
  39.     android:padding="16dp">
  40.    
  41.     <com.xx.xxxxx.widget.CaptchaView
  42.         android:id="@+id/captchaView"
  43.         android:layout_width="match_parent"
  44.         android:layout_height="wrap_content" />
  45. </LinearLayout><?xml version="1.0" encoding="utf-8"?>
  46. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  47.     android:layout_width="match_parent"
  48.     android:layout_height="match_parent"
  49.     android:orientation="vertical"
  50.     android:padding="16dp">
  51.    
  52.     <com.xx.xxxxx.widget.CaptchaView
  53.         android:id="@+id/captchaView"
  54.         android:layout_width="match_parent"
  55.         android:layout_height="wrap_content" />
  56. </LinearLayout>.add(R.id.container_info, new PatientInfoFragment())<?xml version="1.0" encoding="utf-8"?>
  57. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  58.     android:layout_width="match_parent"
  59.     android:layout_height="match_parent"
  60.     android:orientation="vertical"
  61.     android:padding="16dp">
  62.    
  63.     <com.xx.xxxxx.widget.CaptchaView
  64.         android:id="@+id/captchaView"
  65.         android:layout_width="match_parent"
  66.         android:layout_height="wrap_content" />
  67. </LinearLayout><?xml version="1.0" encoding="utf-8"?>
  68. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  69.     android:layout_width="match_parent"
  70.     android:layout_height="match_parent"
  71.     android:orientation="vertical"
  72.     android:padding="16dp">
  73.    
  74.     <com.xx.xxxxx.widget.CaptchaView
  75.         android:id="@+id/captchaView"
  76.         android:layout_width="match_parent"
  77.         android:layout_height="wrap_content" />
  78. </LinearLayout>.add(R.id.container_orders, new OrderListFragment())<?xml version="1.0" encoding="utf-8"?>
  79. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  80.     android:layout_width="match_parent"
  81.     android:layout_height="match_parent"
  82.     android:orientation="vertical"
  83.     android:padding="16dp">
  84.    
  85.     <com.xx.xxxxx.widget.CaptchaView
  86.         android:id="@+id/captchaView"
  87.         android:layout_width="match_parent"
  88.         android:layout_height="wrap_content" />
  89. </LinearLayout><?xml version="1.0" encoding="utf-8"?>
  90. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  91.     android:layout_width="match_parent"
  92.     android:layout_height="match_parent"
  93.     android:orientation="vertical"
  94.     android:padding="16dp">
  95.    
  96.     <com.xx.xxxxx.widget.CaptchaView
  97.         android:id="@+id/captchaView"
  98.         android:layout_width="match_parent"
  99.         android:layout_height="wrap_content" />
  100. </LinearLayout>.add(R.id.container_records, new RecordListFragment())<?xml version="1.0" encoding="utf-8"?>
  101. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  102.     android:layout_width="match_parent"
  103.     android:layout_height="match_parent"
  104.     android:orientation="vertical"
  105.     android:padding="16dp">
  106.    
  107.     <com.xx.xxxxx.widget.CaptchaView
  108.         android:id="@+id/captchaView"
  109.         android:layout_width="match_parent"
  110.         android:layout_height="wrap_content" />
  111. </LinearLayout><?xml version="1.0" encoding="utf-8"?>
  112. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  113.     android:layout_width="match_parent"
  114.     android:layout_height="match_parent"
  115.     android:orientation="vertical"
  116.     android:padding="16dp">
  117.    
  118.     <com.xx.xxxxx.widget.CaptchaView
  119.         android:id="@+id/captchaView"
  120.         android:layout_width="match_parent"
  121.         android:layout_height="wrap_content" />
  122. </LinearLayout>.commit();<?xml version="1.0" encoding="utf-8"?>
  123. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  124.     android:layout_width="match_parent"
  125.     android:layout_height="match_parent"
  126.     android:orientation="vertical"
  127.     android:padding="16dp">
  128.    
  129.     <com.xx.xxxxx.widget.CaptchaView
  130.         android:id="@+id/captchaView"
  131.         android:layout_width="match_parent"
  132.         android:layout_height="wrap_content" />
  133. </LinearLayout>}    }}
复制代码
总结

场景应用复杂页面拆分

  • 重复用→抽组件
  • 不重复→直接写
多组件编码

  • 简单用 include
  • 复杂用自定义 View
  • 动态加载用 Fragment
实际项目中

  • 登录页这种简单页面 → 直接写一个 XML + Activity
  • 详情页 → include 抽公共部分
  • 列表页 → RecyclerView + 多类型 ViewHolder
  • Tab 切换 → Fragment

来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

相关推荐

您需要登录后才可以回帖 登录 | 立即注册