标签: android-databinding

有没有办法将绑定附加到 setContentView 活动

目前,如果我想附加绑定,我会这样做

DataBindingUtil.setContentView(this, R.layout.main_activity);
Run Code Online (Sandbox Code Playgroud)

我遇到了需要调用的自定义第三方活动的问题

setContentView(R.layout.main_activity);
Run Code Online (Sandbox Code Playgroud)

如何将绑定附加到设置了布局的活动 setContentView

android android-databinding

3
推荐指数
1
解决办法
1050
查看次数

MainActivityBinding.inflate(getLayoutInflater()) 页面未更新

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main2);
    Main2Binding binding1 =  Main2Binding.inflate(getLayoutInflater());
    User user = new User("Test", "User");
    MyHandlers myHandlers = new MyHandlers(this);
    MyStringUtils myStringUtils=  new MyStringUtils();
    binding1.setUser(user);
    binding1.setHandlers(myHandlers);
}
Run Code Online (Sandbox Code Playgroud)

页面没有更新,

MainActivityBinding binding1 = DataBindingUtil.setContentView (this, R.layout.main_activity); there is no problem
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题呢?

android android-databinding

3
推荐指数
1
解决办法
2173
查看次数

禁用某些布局的数据绑定

是否可以禁用某些布局的 Android 数据出价?

dataBinding {
    enabled = true // not for all layout, possible ?
}
Run Code Online (Sandbox Code Playgroud)

android android-gradle-plugin android-databinding

3
推荐指数
2
解决办法
1080
查看次数

是否可以根据类方法响应有条件地显示 XML 包含?

我想知道是否可以使用 DataBinding 根据方法布尔响应有条件地显示布局。这就是我想要做的

XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="View"
            type="android.view.View"/>

        <variable
            name="App"
            type="com.app.JTApp"/>
    </data>

    <include layout="@layout/view_checkout_total_cardview"
             android:visibility="@{App.isLandscape() ? View.GONE : View.VISIBLE}" />

</layout>
Run Code Online (Sandbox Code Playgroud)

JTApp 类:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="View"
            type="android.view.View"/>

        <variable
            name="App"
            type="com.app.JTApp"/>
    </data>

    <include layout="@layout/view_checkout_total_cardview"
             android:visibility="@{App.isLandscape() ? View.GONE : View.VISIBLE}" />

</layout>
Run Code Online (Sandbox Code Playgroud)

目前这不起作用。我错过了什么或者这是不可能的吗?我来自网络,使用 Angular 等框架可以做到这一点。

xml android android-databinding

3
推荐指数
1
解决办法
2737
查看次数

Android 数据绑定“??” 符号和来自资源的参数化字符串。如何避免“空”文本?

我的TextView应用程序中有一些由strings.xml资源文件中的键和动态传递的参数组合而成。我使用 Android Data Binding 其中的 '??' 表示法意味着它应该用空白字符串替换空值,如下例所示。但是当参数值为空直到TextView数据满载时,我们会看到类似“null min”的东西,而我喜欢它是:“min”。

<TextView
    android:id="@+id/text_runtime"
    android:layout_width="wrap_content"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:padding="2dp"
    android:text="@{@string/min(vm.runtime) ?? ``}"
    tools:text="120 min" />
Run Code Online (Sandbox Code Playgroud)

strings.xml

<string name="min">
    <xliff:g id="min">%d</xliff:g>
    min
</string>
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明 在此处输入图片说明

android android-resources android-databinding

3
推荐指数
1
解决办法
4009
查看次数

Android数据绑定与数组的双向绑定

我想在Android中使用数据绑定在数组上实现双向绑定。

这是我拥有的代码的简化版本:

<data>
    <variable
        name="values"
        type="Integer[]" />
</data>

<EditText
    ...
    android:text="@={Converter.toString(values[0])} />
Run Code Online (Sandbox Code Playgroud)

但是当我尝试构建此代码时,我收到如下消息:

cannot find method setTo(java.lang.Integer[], int, java.lang.Integer) in class android.databinding.ViewDataBinding

如果可能的话,如何实现与数组的双向绑定?

arrays android-databinding two-way-binding

3
推荐指数
1
解决办法
5702
查看次数

Android 数据绑定库 - 未解析的参考

我正在使用 Kotlin 并尝试使用新的数据绑定库,但我遇到了未解决的xxxBinding类引用错误。

顶层 build.gradle

buildscript {
    ext.kotlin_version = '1.2.30'
    ext.gradle_version = '3.1.0'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath "com.android.tools.build:gradle:$gradle_version"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:3.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}


allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)

应用程序 build.gradle

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.gms.google-services' …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-databinding

3
推荐指数
1
解决办法
3439
查看次数

Android 数据绑定:有没有显示/隐藏包含标签的好方法?

我刚刚开始在 android 中处理数据绑定。理想情况下,我希望有一个包含一些通用 xml 元素的根 xml 文件,然后其内部内容可以是三个 xml 文件之一

<data>
  <variable name="action" type="com.example.android.action"/>
</data>

<TextView>
<TextView>
<!--Only show one of these includes based on the binding data-->

<!-- if action.item -->
<include layout="item.xml"
 bind:item="@{action.item}">

<!-- else if action.udpate -->
<include layout="update.xml"
 bind:update="@{action.update}">

<!-- else if action.video -->
<include layout="video.xml"
 bind:video="@{action.video}">

<TextView>
... etc
Run Code Online (Sandbox Code Playgroud)

所以基本上基于动作内部存在的子对象(项目、更新或视频)显示其布局并绑定视图但不显示其他包含。我应该只使用 android 的View:Visibility还是有我忽略的包含的东西?

android android-layout android-databinding

3
推荐指数
1
解决办法
5208
查看次数

如何在数据绑定中使用 kotlin.Int?

我写了一个如下的布局xml。但是 Kotlin 编译器说Cannot resolve symbol 'Int'

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<layout ...>
  <data>
    <import type="androidx.databinding.ObservableArrayMap" />
    <variable
      name="myList"
      type="ObservableArrayMap&lt;Int,String&gt;" />
  </data>

<!-- ...... -->    

</layout>
Run Code Online (Sandbox Code Playgroud)

是否可以在 android 数据绑定 xml 中使用 kotlin 内置函数?

android kotlin android-databinding

3
推荐指数
1
解决办法
1929
查看次数

BindingAdapter 中的高阶函数

我在 android 中使用 DataBinding 并且我有一个自定义视图:CarouselView

我为此编写了一个绑定适配器:

@BindingAdapter("onClick")
fun setOnClick(carouselView: CarouselView, onClick: (position: Int) -> Unit) {
    carouselView.setImageClickListener { position ->
        onClick.run(position)
    }
}
Run Code Online (Sandbox Code Playgroud)

在 xml 中:

<com.synnapps.carouselview.CarouselView
            android:id="@+id/carouselView"
            ...
            app:onClick="@{(p) -> vm.onAdsClicked(p)}"/>
Run Code Online (Sandbox Code Playgroud)

但它不编译。所以我在 Stackoverflow 中看到了这个答案。但我的问题是我不能使用 Runnable 而不是 kotlin hoc 函数,因为我需要传递一个参数来运行。

我该如何解决?

android higher-order-functions kotlin android-databinding android-binding-adapter

3
推荐指数
1
解决办法
1287
查看次数