我想设置背景颜色或null使用DataBinding库在我的视图上,但我尝试运行它时遇到异常.
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.Integer.intValue()' on a null object reference
Run Code Online (Sandbox Code Playgroud)
我是这样做的:
android:background="@{article.sponsored ? @color/sponsored_article_background : null}"
Run Code Online (Sandbox Code Playgroud)
我也试过设置转换但它没有用.
@BindingConversion
public static ColorDrawable convertColorToDrawable(int color) {
return new ColorDrawable(color);
}
Run Code Online (Sandbox Code Playgroud)
最后,我使用解决方法解决了它,@BindingAdapter但我想知道如何正确地做到这一点.
我正在使用MVVM和DataBinding构建Android应用程序.我在ViewModel中有一个函数来启动一个Activity.是否可以在ViewModel中进行onClick调用?
像这样.
public class MyViewModel {
public void onClick(View view, long productId) {
Context context = view.getContext();
Intent intent = new Intent(context, ProductDetailActivity.class);
intent.putExtra("productId", productId);
context.startActivity(intent);
}
}
Run Code Online (Sandbox Code Playgroud)
在我的XML中:
...
android:onClick="@{(v) -> viewModel.onClick(v, viewModel.product.id)}">
Run Code Online (Sandbox Code Playgroud)
或者将它移动到View并从EventBus或Rx调用它并在我的ViewModel中只有POJO是最佳做法吗?
这必须是最基本的问题,但经过一整天的阅读教程和文档后,我似乎无法理解在哪里放这些方法.没有一个指南提到放置这个东西的地方,只是提到在静态方法上使用注释.什么静态方法?任何静态方法都可以,无论什么类?什么是好习惯?创建一个CustomBinding类来托管所有这些静态方法?
只要我有方法有一个BindingAdapter注释,第一个参数是一个View,它会起作用吗?
我假设如果第一个参数是View类型我可以将绑定属性放在任何类型的视图上,它会触发方法吗?因此,如果我有特定的视图说EditText,这是否意味着只有在布局文件的EditText视图中找到属性时才调用该方法?
我正在使用新的Android数据绑定,它工作得很好.我可以使用EditText,TextView,Radio和复选框执行数据绑定.现在,我无法在微调器中进行数据绑定.
在下面的链接中找到了一些线索: Android spinner数据绑定与xml布局
但是,仍然无法找到解决方案.还需要执行双向数据绑定.应该反映微调器数据的选择值.
有人可以给我一个例子吗?
这是我的xml代码:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/tools"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<data>
<import type="android.view.View" />
<variable
name="viewModel"
type="com.ViewModels.model" />
</data>
<Spinner
android:id="@+id/assessmemt_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="@dimen/carview_margin"
android:layout_toRightOf="@+id/text_bp"
android:drawSelectorOnTop="true"
android:spinnerMode="dropdown"
android:visibility="@{viewModel.type.equals(@string/spinner_type)? View.VISIBLE : View.GONE}" />
</layout>
Run Code Online (Sandbox Code Playgroud)
查看型号:
public class AssessmentGetViewModel {
private String valueWidth;
private ArrayList<String> values;
private String type;
public String getValueWidth() { return this.valueWidth; }
public void setValueWidth(String valueWidth) { this.valueWidth = valueWidth; }
public ArrayList<String> getvalues() { return this.values; }
public void setvalues(ArrayList<String> values) { this.values …Run Code Online (Sandbox Code Playgroud) 我在使用Integer数据类型实现双向绑定时遇到了一些问题.
public class User {
private String firstName;
private String lastName;
private int age;
public User() {}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getFirstName() {
return this.firstName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getLastName() {
return this.lastName;
}
public void setAge(int age) {
this.age = age;
}
public int getAge() {
return this.age;
}
}
Run Code Online (Sandbox Code Playgroud)
XML:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data class="UserDataBinding">
<variable
name="user"
type="com.databinding.model.User" />
</data>
<LinearLayout …Run Code Online (Sandbox Code Playgroud) 我想在同一个视图中多次使用我的一个布局include.假设我有custom.xml一些包含TextViews.
custom.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我已将此布局多次包含在parent.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include layout="@layout/custom"
android:id="@+id/layout1"/>
<include layout="@layout/custom"
android:id="@+id/layout2"/>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
现在我想将我的数据模型绑定到这个布局,但问题是我不知道如何绑定两个不同的数据模型layout1,layout2因为它们都是从一个布局中引用的custom.xml.据我所知,我可以在我的xml布局中添加此标记:
<data>
<variable name="user" type="com.myproject.model.User"/>
</data>
Run Code Online (Sandbox Code Playgroud)
但我需要绑定两个不同的数据模型custom.xml.
我的问题是如何在一个视图中多次包含布局并使用数据绑定将不同的数据传递给它们?比如将数据传递给布局但不是将模型静态绑定到xml.
我也发现这个问题确实存在同样的问题但是由于数据绑定是在较新版本的android中发布的,我正在寻找一种方法来解决使用数据绑定的相同问题.以下是我引用澄清的问题的一部分:
例如,我有一个精心设计的布局,我希望在我的视图中显示三次.每个实例都需要不同的值.由于
include基本上是XML并将其粘贴到此处,因此我需要更强大的功能.
我将Android Studio升级到3.4之后,将Android Gradle插件升级到3.4并升级到5.1.1
我收到如下数据绑定错误
我确保已清理项目并进行重建,已清除缓存并重新启动了AS。
升级之前从未发生过此问题
我可以确认这是因为新的gradle更新
DataBinderMapperImpl.java:54:错误:找不到符号
e:[kapt]发生异常:android.databinding.tool.util.LoggedErrorException:发现数据绑定错误。**** /数据绑定错误**** msg:尽管已注册导入表达式,但缺少
我找到了解决方案:解决方案:
我正在尝试实现侦听器绑定,但是当我运行我的代码时,我收到以下错误:
Caused by: android.databinding.tool.util.LoggedErrorException: Found data binding errors.
****/ data binding error ****msg:Cannot find the setter for attribute 'android:onClick' with parameter type lambda on android.widget.Button. file:~/GithubBrowser/app/src/main/res/layout/loading_state.xml loc:30:31 - 30:52 ****\ data binding error ****
Run Code Online (Sandbox Code Playgroud)
这是有问题的布局文件:
<?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">
<data>
<import type="com.example.app.data.model.Resource"/>
<import type="com.example.app.data.model.Status"/>
<variable name="resource" type="Resource"/>
<variable name="callback" type="com.example.app.ui.common.RetryCallback"/>
</data>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:padding="@dimen/default_margin">
<Button android:id="@+id/retry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/retry"
android:onClick="@{() -> callback.retry()}"/>
</LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
这是RetryCallback布局中引用的接口:
package com.example.app.ui.common
interface RetryCallback {
fun …Run Code Online (Sandbox Code Playgroud) 我发现很难发现android.databinding.ObservableList作为数据绑定功能的真正存在理由.
起初它看起来像一个很酷的工具来显示列表,通过数据绑定,将它们添加xml到一个RecyclerView.为此,我制作了一个像这样的BindingAdapter:
@BindingAdapter(value = {"items"}, requireAll = false)
public static void setMyAdapterItems(RecyclerView view, ObservableList <T> items) {
if(items != null && (view.getAdapter() instanceof MyAdapter)) {
((GenericAdapter<T>) view.getAdapter()).setItems(items);
}
}
Run Code Online (Sandbox Code Playgroud)
这样一来,我可以使用该属性app:items的RecyclerView一个MyAdapter集来,以更新其项目.
现在最好的功能ObservableList是你可以添加一个OnListChangedCallback,它可以处理RecyclerView添加/移动/删除/更改项目中可用的相同事件,而无需实际重新加载整个列表.
所以我想要实现的逻辑是以下:
MyAdapterObservableArrayList包装它们并将它传递给bindingBindingAdapter传递的项目MyAdapterMyAdapter接收到新的项目,它会清除其旧的并添加OnListChangedCallback到ObservableList接收处理微变ObservableList,MyAdapter将相应更改而不会完全刷新binding变量,这样BindingAdapter …android observablelist android-recyclerview android-databinding android-binding-adapter
android ×10
mvvm ×2
androidx ×1
data-binding ×1
decoupling ×1
java ×1
kotlin ×1
viewmodel ×1
xml ×1