我有一个像这样运行的例子:
<Switch
android:id="@+id/sw_state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:enabled="@{!swAuto.checked}"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:text="Zustand"
android:textColor="@{swAuto.checked ? @color/outlet_preview_card_text_disabled : @color/outlet_preview_card_text}"
android:textSize="14sp"
tools:textColor="@color/outlet_preview_card_text"/>
Run Code Online (Sandbox Code Playgroud)
但我需要在另一个视图中设置alpha,具体取决于另一个视图的可见性,如:
<LinearLayout
android:id="@+id/ll_global_outlet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:background="@drawable/round_background_white"
android:orientation="vertical"
android:alpha="@{ivProgressBar.visibility == View.VISIBLE ? 0.90 : 1}"
android:padding="10dp">
Run Code Online (Sandbox Code Playgroud)
但是我收到了这个错误:
错误:(45,30)无法解析类型'android.widget.ImageView'上的双向绑定属性'visibility'
知道我做错了什么吗?
这是我应该依赖的观点:
<ImageView
android:id="@+id/iv_progress_bar"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_gravity="center"
android:visibility="gone"
tools:visibility="visible"
android:background="@drawable/progress_bar_animation"/>
Run Code Online (Sandbox Code Playgroud) 我在我的应用程序中使用数据绑定.我有一个应用程序模块,数据绑定工作成功.
但是,我也有一个'家庭模块'我试图使用相同的技术,但数据绑定在xml中出错,具有以下内容:
错误:(63,34)没有指定资源类型(在'onClick'上,值为'@ {viewModel :: onButtonClicked}').
我发现这个错误报告表明这是一个问题,但它已被修复.
我看不出代码有任何问题,我认为问题是因为它在库模块中.
我能做些什么才能让它发挥作用?
家里模块
activity_home.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"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable name="viewModel" type="com.flowmellow.projectx.home.ui.viewmodel.HomeViewModel"/>
</data>
<android.support.design.widget.CoordinatorLayout
android:id="@+id/home_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.flowmellow.projectx.home.ui.viewmodel.HomeActivity">
<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>
<android.support.constraint.ConstraintLayout
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:id="@+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.flowmellow.projectx.home.ui.viewmodel.HomeActivity">
<TextView
android:text="@string/activity_home_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/activity_home_title"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:layout_marginStart="16dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginLeft="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginRight="16dp"
app:layout_constraintBottom_toTopOf="@+id/activity_home_button"
android:layout_marginBottom="64dp" />
<Button
android:text="@string/activity_home_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/activity_home_button"
style="@style/Widget.AppCompat.Button.Colored"
android:onClick="@{viewModel::onButtonClicked}"/>
</android.support.constraint.ConstraintLayout>
</android.support.design.widget.CoordinatorLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
HomeActivity
public class HomeActivity …Run Code Online (Sandbox Code Playgroud) 模型的setter和getter方法具有一个参数,如下所示:
public int getPrice(Object key) {
}
public void setPrice(Object key, int price) {
}
Run Code Online (Sandbox Code Playgroud)
XML看起来像这样:
<EditText
android:id="@+id/edtPrice"
style="@style/CreateShipperItemValueEditTextView"
android:layout_centerVertical="true"
android:hint="@string/hint_price"
android:inputType="number"
android:maxLines="1"
android:text="@={shipper.getPrice(priceKey)}"/>
Run Code Online (Sandbox Code Playgroud)
android:text =“ @ = {shipper.getPrice(priceKey)}”
构建过程中的编译器错误表明我们应该使用@InverseMethod批注。我尝试这样的事情:
@InverseMethod(value = "getPrice")
@Override
public void setPrice(Object key, int price) {
super.setPrice(key, price);
}
Run Code Online (Sandbox Code Playgroud)
但是在这种情况下,我有下一个错误。
error: @InverseMethods must have a non-void return type
Run Code Online (Sandbox Code Playgroud)
因此,我很高兴在这里对整个流程进行很好的解释。谢谢
我有这个名为Item的对象
public class Item extends BaseObservable
{
private double dose;
private Integer reportedDose;
@Bindable
public double getDose() {
return dose;
}
public void setDose(double dose) {
this.dose = dose;
notifyPropertyChanged(BR.dose);
}
}
Run Code Online (Sandbox Code Playgroud)
现在关注@GeorgeMount上一篇教程:https://medium.com/google-developers/android-data-binding-inverse-functions-95aab4b11873 我添加了Converter类:
public class Converter
{
@InverseMethod("toDouble")
public static String toString(TextView view, double oldValue,
double value) {
NumberFormat numberFormat = getNumberFormat(view);
try {
// Don't return a different value if the parsed value
// doesn't change
String inView = view.getText().toString();
double parsed =
numberFormat.parse(inView).doubleValue();
if (parsed …Run Code Online (Sandbox Code Playgroud) 我知道我可以使用@ dimen/something ...但是想知道为什么这不起作用以及如何使它工作.将帮助我理解数据绑定解析器的黑盒子.
在我的XML for lineralayout元素:
android:layout_width="@{DataBoundData.dis.equals(IN_PROGRESS) ? 60dp :
(DataBoundData.dis.equals(POSTED) ? 60dp : 0dp)}"
Run Code Online (Sandbox Code Playgroud)
它在60dp的'p'上显示错误.我已经尝试过60d\p 60dp和其他几个并没有任何效果
我想通过数据绑定在视图中使用我的Data类的Date值.如果我在Date字段上使用toString()方法,它可以工作.但我想自定义Date值.所以我用Method创建了Utils对象.这是Util对象
object DateUtils {
fun toSimpleString(date: Date) : String {
val format = SimpleDateFormat("dd/MM/yyy")
return format.format(date)
}
}
Run Code Online (Sandbox Code Playgroud)
但是如果我想在xml中使用这个方法就像这样
<data>
<import type="de.mjkd.journeylogger.Utils.DateUtils"/>
<variable
name="journey"
type="de.mjkd.journeylogger.data.Journey"/>
</data>
...
android:text="@{DateUtils.toSimpleString(journey.date)}"
Run Code Online (Sandbox Code Playgroud)
我收到一个错误 cannot find method toSimpleString(java.util.Date) in class ...
这是我的Dataclass:
data class Journey(var title: String, var date: Date?, var destination: String)
Run Code Online (Sandbox Code Playgroud)
这段代码有什么问题?
我曾经创建DataBindingAdapter用于在数据绑定中创建自定义xml属性的创建。
object DataBindingAdapter {
@BindingAdapter("android:src")
fun setImageByRes(imageView: ImageView, @DrawableRes resId: Int) {
imageView.setImageResource(resId)
}
}
Run Code Online (Sandbox Code Playgroud)
在Java中运行良好 。但不能在科特林工作。
据我了解,kotlin中的对象类似于Java的静态方法。但是它不适用于数据绑定。
java.lang.IllegalStateException:必需的DataBindingComponent在类FragmentBottomBarBinding中为null。acr.browser.lightning.utils.DataBindingAdapter中的BindingAdapter不是静态的,需要使用从DataBindingComponent检索的对象。如果您不使用带DataBindingComponent的膨胀方法,请使用DataBindingUtil.setDefaultComponent或将所有BindingAdapter方法设为静态。
我的Viewmodels包含在“ ViewModels”包中。当在我的片段布局xml文件中将其中一个设置为数据变量类型时,生成的ViewDataBinding类将尝试将包当作文件导入。例如:
import com.xyz.myapp.ViewModels;
Run Code Online (Sandbox Code Playgroud)
而不是:
import com.xyz.myapp.ViewModels.*;
Run Code Online (Sandbox Code Playgroud)
然后继续引用视图模型,因为这会ViewModels.MyFragmentViewModel导致进一步的错误。我发现解决此问题的方法是将我所有的视图模型文件都放在com.xyz.myapp目录中。这样做时,一切正常。
这和这里的要求相同。我的声誉不足,无法发表评论。我想念什么吗?我需要设置一些选项吗?还是这只是一个错误?
import com.xyz.myapp.ViewModels;
Run Code Online (Sandbox Code Playgroud)
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="vm"
type="com.xyz.myapp.ViewModels.MyFragmentViewModel"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="@{vm.TEMP}" />
</LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
import com.xyz.myapp.ViewModels.*;
Run Code Online (Sandbox Code Playgroud)
这是生成的类。错误行被注释。4个错误
import com.xyz.myapp.ViewModels; //Error
public abstract class FragmentTestBinding extends ViewDataBinding {
@NonNull
public final CheckBox checkBox;
@NonNull
public final EditText editText;
@Bindable
protected ViewModels.ProfileViewModel mVm; //Error
protected FragmentTestBinding(DataBindingComponent _bindingComponent, View _root,
int _localFieldCount, …Run Code Online (Sandbox Code Playgroud) 我正在尝试在我的android应用程序中实现MVVM体系结构。我也用Kotlin。
这是我的绑定适配器类:
class BindingAdapter {
companion object {
@JvmStatic @BindingAdapter("app:visibleGone") fun showHide(view: View, show: Boolean) {
view.visibility =
if (show)
View.VISIBLE
else
View.GONE
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的XML文件:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="isLoading"
type="boolean"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cardview_light_background"
android:orientation="vertical">
<TextView
android:id="@+id/loading_rates"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:text="@string/loading_rates"
android:textAlignment="center"
app:visibleGone="@{isLoading}"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/cardview_light_background"
android:orientation="vertical"
app:visibleGone="@{!isLoading}">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:layout_marginTop="20dp"
android:gravity="center_horizontal"
android:text="@string/rate_list"
android:textAlignment="center"
android:textSize="@dimen/rate_text"
android:textStyle="bold"/>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rate_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layoutManager="LinearLayoutManager"/>
</LinearLayout>
</LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
错误消息显示:
Found data binding errors.
****/ data binding …Run Code Online (Sandbox Code Playgroud) 请有人帮我!我快疯了,这应该工作。尝试构建Android项目时,出现以下错误消息:
Android resource linking failed
/Users/slehrbaum/StudioProjects/OneNightComps/Android/app/build/intermediates/incremental/mergeDebugResources/stripped.dir/layout/fragment_login.xml:17: error: attribute errorText (aka lehrbaum.de.onenightcomps:errorText) not found.
error: failed linking file resources.
Run Code Online (Sandbox Code Playgroud)
错误消息确实提到了errorText属性。我以这种方式在xml中使用errorText属性(此处为完整xml):
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/usernameField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/username"
app:hintEnabled="true"
app:errorEnabled="true"
app:errorText="Hi"
>
<!--app:errorText="Please provide a username."-->
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="username"
android:inputType="text"
android:text="@={viewModel.username}"
/>
</com.google.android.material.textfield.TextInputLayout>
Run Code Online (Sandbox Code Playgroud)
这是我在Kotlin文件(此处为完整文件)中定义errorText的方式:
object ViewDataBindingExtensions {
@JvmStatic
@BindingAdapter("errorText")
fun bindErrorText(textInputLayout: TextInputLayout, errorText: String) {
textInputLayout.error = errorText
}
}
Run Code Online (Sandbox Code Playgroud)
我只是不明白为什么会这样。我可以在布局文件中输入某种类型的导入信息,以说明BindingAdapter的位置吗?我的Gradle文件有问题吗?在这个问题上,我将它与GitHub项目进行了比较, 这个问题显然已经解决了,我看不出我的项目有什么区别。根据答案,我应该将Kotlin-kapt插件添加到我的Gradle构建中。我还浏览了项目的其余部分并进行了比较。无济于事。您可以在这里找到我的整个build.gradle文件以及该项目的其余部分。
请帮我!