Java 使用包含标签的 Android 数据绑定

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32947440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 14:04:06  来源:igfitidea点击:

Android Data Binding using include tag

javaandroiddata-bindingandroid-buttonandroid-databinding

提问by Kamil Nekanowicz

Update note:

更新说明:

The above example works properly, because release 1.0-rc4 fixedthe issue of needing the unnecessary variable.

上面的例子工作正常,因为版本 1.0-rc4修复了需要不必要变量的问题。

Original question:

原问题:

I do exactly as it is described in the documentationand it does not work:

我完全按照文档中的描述进行操作,但它不起作用:

main.xml:

主文件:

<layout xmlns:andr...
    <data>
    </data>
       <include layout="@layout/buttons"></include>
....

buttons.xml:

按钮.xml:

<layout xmlns:andr...>
    <data>
    </data>
    <Button
        android:id="@+id/button"
        ...." />

MyActivity.java:

我的Activity.java:

 ... binding = DataBindingUtil.inflate...
binding.button; ->cannot resolve symbol 'button'

how to get button?

如何获得按钮?

采纳答案by George Mount

The problem is that the included layout isn't being thought of as a data-bound layout. To make it act as one, you need to pass a variable:

问题是包含的布局不被认为是数据绑定布局。要使其成为一体,您需要传递一个变量:

buttons.xml:

按钮.xml:

<layout xmlns:andr...>
  <data>
    <variable name="foo" type="int"/>
  </data>
  <Button
    android:id="@+id/button"
    ...." />

main.xml:

主文件:

<layout xmlns:andr...
...
   <include layout="@layout/buttons"
            android:id="@+id/buttons"
            app:foo="@{1}"/>
....

Then you can access buttons indirectly through the buttons field:

然后您可以通过按钮字段间接访问按钮:

MainBinding binding = MainBinding.inflate(getLayoutInflater());
binding.buttons.button

As of 1.0-rc4 (just released), you no longer need the variable. You can simplify it to:

从 1.0-rc4(刚刚发布)开始,您不再需要该变量。您可以将其简化为:

buttons.xml:

按钮.xml:

<layout xmlns:andr...>
  <Button
    android:id="@+id/button"
    ...." />

main.xml:

主文件:

<layout xmlns:andr...
...
   <include layout="@layout/buttons"
            android:id="@+id/buttons"/>
....

回答by Cosmin Constantin Firta

An other interesting thing on this is that you can pas variables to the imported layout from the binder like this:

另一个有趣的事情是,您可以将变量从活页夹传递给导入的布局,如下所示:

MainBinding binding = MainBinding.inflate(getLayoutInflater());
binding.buttons.setVariable(BR.varID, variable)

回答by Khemraj

Easy Complete Example

简单的完整示例

Just set idto included layout, and use binding.includedLayout.anyView.

只需设置id为包含布局,然后使用binding.includedLayout.anyView.

This example helps passing a value to <include& accessing included views in code.

此示例有助于将值传递给<include和访问代码中包含的视图。

Step 1

第1步

You have layout_common.xml, want to pass Stringto included layout.

你有layout_common.xml,想传递String给包含的布局。

You will create Stringvariable in layout and refer this Stringto TextView.

您将String在布局中创建变量并将其引用StringTextView.

<data>
    // declare fields
    <variable
        name="passedText"
        type="String"/>
</data>

<TextView
    android:id="@+id/textView"
    ...
    android:text="@{passedText}"/> //set field to your view.

Step 2

第2步

Include this layout to parent layout. Give an idto included layout, so that we can use that in binding class. Now you can pass String passedTextto your <includetag.

将此布局包含到父布局中。给一个id包含的布局,以便我们可以在绑定类中使用它。现在您可以将 String 传递passedText给您的<include标签。

activity_main.xml

activity_main.xml

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

    <LinearLayout
        ..
        >

        <include
            android:id="@+id/includedLayout"
            layout="@layout/layout_common"
            app:passedText="@{@string/app_name}" // here we pass any String 
            />

    </LinearLayout>
</layout>
  • You can use now binding.includedLayout.textViewin your class.
  • You can pass any variables to included layout like above.

    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    binding.includedLayout.textView.setText("text");
    
  • 您现在可以binding.includedLayout.textView在您的班级中使用。
  • 您可以像上面一样将任何变量传递给包含的布局。

    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    binding.includedLayout.textView.setText("text");
    

NoteBoth layouts (parent & included) should be binding layout, wrapped with <layout

注意两个布局(父级和包含)都应该binding layout<layout

回答by Rodrigo Salomao

You can make your bind work on your include just adding a ID to it like so:

你可以让你的绑定在你的包含上工作,只需向它添加一个 ID,如下所示:

<include
            android:id="@+id/loading"
            layout="@layout/loading_layout"
            bind:booleanVisibility="@{viewModel.showLoading}" />

回答by Sadeq Hitex

just set an id for your include layout

只需为您的包含布局设置一个 id

    <include
        android:id="@+id/layout"
        layout="@layout/buttons" />

then

然后

BUTTONSBINDING binding = yourMainBinding.layout;

BUTTONSBINDINGis res/layout/buttons.xml

BUTTONSBINDING是 res/layout/buttons.xml

now :

现在 :

binding.button.setText("simple_Way");