我正在编写一个android-App,我正在使用MVVM-Pattern。我在视图中查看布局中的数据时遇到问题。我是android编程的新手,所以也许我在想错误的方法。在C#中我创建MVVMC-App没有问题。说实话,我不明白如何在活动中的布局中显示内容:-( 这是我在视图中的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
//This is the code which shows the white window with my data
super.onCreate(savedInstanceState);
ContentSelectItemBinding binding = DataBindingUtil.setContentView(this, R.layout.content_select_item);
this.aSelectItemViewModel = new SelectItemViewModel();
binding.setItem(this.aSelectItemViewModel.item);
//when i add this, it will just show the empty view
setContentView(R.layout.activity_select_item);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
这就是我的轻松SelectItemViewModel的样子
public TestItem item;
public SelectItemViewModel() {
item = new TestItem("Test", "User");
}
public List<TestItem> getItemList() {
return this.aTestItemList;
}
这是我的XML文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="private.testapp.view.SelectItemActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_select_item" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
这是我的布局文件
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="item" type="private.testapp.model.TestItem"/>
</data>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:layout_width="wrap_content"
android:textSize="128sp"
android:layout_height="wrap_content"
android:text="@{item.name}"/>
<TextView android:layout_width="wrap_content"
android:textSize="128sp"
android:layout_height="wrap_content"
android:text="@{item.description}"/>
</LinearLayout>
</layout>
我想先做的只是显示testitem ...因为当我只显示布局时显示它,我确信绑定和我的viewmodel有效。 我不明白如何在我的视图中显示布局中的数据。
答案 0 :(得分:2)
选项1-从活动xml到包含xml的转发变量:
首先,
ContentSelectItemBinding binding = DataBindingUtil.setContentView(this,R.layout.content_select_item);
this.aSelectItemViewModel = new SelectItemViewModel();
binding.setItem(this.aSelectItemViewModel.item);
//when i add this, it will just show the empty view
setContentView(R.layout.activity_select_item);
您将活动内容设置为两次错误,只需将其替换为:
DataBindingUtil.setContentView(this,R.layout.activity_select_item);
this.aSelectItemViewModel = new SelectItemViewModel();
binding.setItem(this.aSelectItemViewModel.item);
将活动xml设置为布局。
接下来,您的activity_select_item xml也应该包含一个<layout>
标记和一个<data>
标记,以表示这是一个数据绑定布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="item" type="private.testapp.model.TestItem"/>
</data>
<android.support.design.widget.CoordinatorLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="private.testapp.view.SelectItemActivity">
.
.
.
</layout>
最后,将变量转发到<include>
标记内的包含布局:
.
.
.
<include
layout="@layout/content_select_item"
app:item="@{item}"/>
.
.
.
</android.support.design.widget.CoordinatorLayout>
选项2-查找包含的视图并将其绑定
首先,用id:
标记包含的视图<include
id="@+id/content"
layout="@layout/content_select_item" />
接下来,在代码中找到并绑定它:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_item);
View content= findViewById(R.id.content);
ViewDataBinding binding = DataBindingUtil.bind(content);
最后,使用BR类中生成的id在泛型绑定上设置变量(如R类,但对于数据绑定变量):
this.aSelectItemViewModel = new SelectItemViewModel();
binding.setVariable(BR.item, aSelectItemViewModel.item);