简体   繁体   English

Android数据绑定:布局到视图

[英]Android databinding: Layout to the view

I am programming an android-App and I am using the MVVM-Pattern. 我正在编写一个android-App,我正在使用MVVM-Pattern。 I have problems seeing the data in the layout in my view. 我在视图中查看布局中的数据时遇到问题。 I am new to android programming, so maybe i am thinking the wrong way. 我是android编程的新手,所以也许我在想错误的方法。 In C# i had no problems creating an MVVMC-App. 在C#中我创建MVVMC-App没有问题。 To be honest, i do not understand how to show the contents in the layouts in the activity :-( This is my code in the view: 说实话,我不明白如何在活动的布局中显示内容:-(这是我在视图中的代码:

@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();
        }
    });
}

And thats how my easy SelectItemViewModel looks like 这就是我的简单SelectItemViewModel的样子

public TestItem item;

public SelectItemViewModel() {
    item = new TestItem("Test", "User");
}

public List<TestItem> getItemList() {
    return this.aTestItemList;
}

And here are my XML-Files: 这是我的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>

This is my layout file 这是我的布局文件

<?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>

All i want to do first is just display the testitem... Since it displays it when i just show the layout, i am sure that the binding and my viewmodel works. 我想先做的就是显示testitem ...因为当我只显示布局时显示它,我确信绑定和我的viewmodel工作。 I don`t understand how the data in the layout will be shown in my view. 我不明白如何在我的视图中显示布局中的数据。

Option 1- forward variable from activity xml to included xml: 选项1-从活动xml到包含xml的转发变量:

First, 第一,

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);

You're setting your activity content twice which is wrong, replace it with just: 您将活动内容设置为两次,这是错误的,只需将其替换为:

DataBindingUtil.setContentView(this,R.layout.activity_select_item);
this.aSelectItemViewModel = new SelectItemViewModel();
binding.setItem(this.aSelectItemViewModel.item);

To set your activity xml as your layout. 将活动xml设置为布局。

Next, your activity_select_item xml should also be wrapped with a <layout> tag and a <data> tag inside to denote this is a data binding layout: 接下来,您的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>

Finally, forward the variable to your included layout inside the <include> tag: 最后,将变量转发到<include>标记内的包含布局:

   .
   .
   .
<include 
    layout="@layout/content_select_item" 
    app:item="@{item}"/>
   .
   .
   .
</android.support.design.widget.CoordinatorLayout>

Option 2- find included view and bind it 选项2-查找包含的视图并绑定它

First, mark included view with id: 首先,使用id标记包含的视图:

<include 
    id="@+id/content"
    layout="@layout/content_select_item" />

Next, find it in code and bind it: 接下来,在代码中找到它并绑定它:

 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_select_item);
 View content= findViewById(R.id.content);
 ViewDataBinding binding = DataBindingUtil.bind(content);

Finally, set the variable on the generic binding using generated id inside BR class (like R class but for data binding variables): 最后,使用BR类中生成的id在泛型绑定上设置变量(如R类,但对于数据绑定变量):

this.aSelectItemViewModel = new SelectItemViewModel();
binding.setVariable(BR.item, aSelectItemViewModel.item);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM