【发布时间】:2016-07-24 02:08:03
【问题描述】:
我正在为带有数据绑定库的 Android 应用程序使用 MVVM 框架。
我有一些可重复使用的组件,应该包含所有活动。 f.e.工具栏、菜单、浮动操作按钮。
我想创建一个通用活动,它将实现所有这些可重用的功能,然后每个活动类都将从这个通用活动继承。我也有 GenericViewModel 类,其他所有 ViewModel 都是从这个泛型类继承的。
但是我的布局有问题。我想创建通用布局文件并动态包含子布局。 f.e.
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="data"
type="com.mypackage.genericViewModel" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
bind:data="@{data}"
layout="@layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<include
layout="@{Here i want to have dynamic variable}"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout></layout>
我不想为每个活动复制/粘贴此代码,但数据绑定库不允许动态包含布局。这种情况有解决办法吗?
【问题讨论】:
标签: android mvvm android-databinding