Android复杂页面组件化策略

张开发
2026/4/20 15:57:08 15 分钟阅读

分享文章

Android复杂页面组件化策略
常见方案对比方案适用场景特点include 布局静态拆分不需要复用简单布局复用自定义 ViewUI逻辑封装可复用封装度高Fragment动态加载独立生命周期灵活但复杂RecyclerView列表每行算一个组件最常用实际建议复杂页面如患者详情页 ├── 患者基本信息卡片 → include 或 自定义 CardView ├── 医嘱列表区域 → RecyclerView Adapter ├── 执行记录区域 → RecyclerView Adapter ├── 备注信息区域 → include └── 底部操作栏 → include不是越拆越好2-3 个简单区域 → 直接写一个 XML重复使用的区域 → 抽成 include 或自定义 View需要独立管理生命周期 → Fragment列表类型 → RecyclerView多组件编码示例示例场景登录页拆分LoginActivity ├── 顶部 Logo 区域LogoView ├── 账号输入区域AccountInputView ├── 验证码区域CaptchaView └── 登录按钮区域LoginButtonView方案 Ainclude ViewBinding简单拆分activity_login.xml?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:layout_widthmatch_parent android:layout_heightmatch_parent android:orientationvertical android:padding16dp !-- 顶部 Logo -- include android:idid/layout_logo layoutlayout/layout_login_logo / !-- 账号输入 -- include android:idid/layout_account layoutlayout/layout_account_input / !-- 验证码 -- include android:idid/layout_captcha layoutlayout/layout_captcha / !-- 登录按钮 -- include android:idid/layout_login_btn layoutlayout/layout_login_button / /LinearLayoutLoginActivity.javapublic class LoginActivity extends BaseActivityActivityLoginBinding { Override protected void initView(Bundle savedInstanceState) { // 每个子布局都可以单独操作 binding.layoutLogo.setLogo(R.drawable.ic_logo); binding.layoutAccount.setHint(请输入账号); binding.layoutCaptcha.setOnRefreshListener(() - viewModel.loadCaptcha()); binding.layoutLoginBtn.setOnClickListener(() - submitLogin()); } Override protected void observeViewModel() { // 集中管理所有 LiveData viewModel.getCaptchaState().observe(this, resource - { if (resource.isSuccess()) { binding.layoutCaptcha.setImage(resource.getData().imageBase64); } }); } }方案 B自定义 View封装业务逻辑适合复杂且可复用的组件比如验证码组件CaptchaView.java自定义组件public class CaptchaView extends LinearLayout { private ActivityCaptchaBinding binding; private OnRefreshListener refreshListener; public CaptchaView(Context context) { super(context); init(); } private void init() { binding ActivityCaptchaBinding.inflate(LayoutInflater.from(getContext()), this, true); binding.btnRefresh.setOnClickListener(v - { if (refreshListener ! null) refreshListener.onRefresh(); }); } /** 设置验证码图片 */ public void setCaptchaImage(String base64) { // Base64 → Bitmap → 设置图片 } /** 获取用户输入的验证码 */ public String getCaptchaCode() { return binding.etCaptcha.getText().toString().trim(); } /** 清空输入 */ public void clear() { binding.etCaptcha.setText(); } /** 显示加载中 */ public void showLoading() { binding.progressBar.setVisibility(View.VISIBLE); } public void setOnRefreshListener(OnRefreshListener listener) { this.refreshListener listener; } public interface OnRefreshListener { void onRefresh(); } }activity_login.xml使用自定义组件?xml version1.0 encodingutf-8? LinearLayout xmlns:androidhttp://schemas.android.com/apk/res/android android:layout_widthmatch_parent android:layout_heightmatch_parent android:orientationvertical android:padding16dp !-- 直接使用自定义组件 -- com.xx.xxxxx.widget.CaptchaView android:idid/captchaView android:layout_widthmatch_parent android:layout_heightwrap_content / /LinearLayoutLoginActivity.java更简洁public class LoginActivity extends BaseActivityActivityLoginBinding { Override protected void initView(Bundle savedInstanceState) { // 验证码组件的逻辑封装在 CaptchaView 内部 binding.captchaView.setOnRefreshListener(() - viewModel.loadCaptcha()); } Override protected void observeViewModel() { viewModel.getCaptchaState().observe(this, resource - { if (resource.isSuccess()) { binding.captchaView.setCaptchaImage(resource.getData().imageBase64); } }); } private void submitLogin() { String captcha binding.captchaView.getCaptchaCode(); // ... } }方案 CFragment动态拆分适合需要独立管理生命周期的场景// PatientDetailActivity 使用多个 Fragment public class PatientDetailActivity extends BaseActivity { Override protected void onCreate(Bundle savedInstanceState) { if (savedInstanceState null) { // 动态添加 Fragment getSupportFragmentManager().beginTransaction() .add(R.id.container_info, new PatientInfoFragment()) .add(R.id.container_orders, new OrderListFragment()) .add(R.id.container_records, new RecordListFragment()) .commit(); } }

更多文章