标签: android-databinding

android 数据绑定的一个属性中可以有两个参数吗?

我已阅读此博客中的自定义设置器。https://medium.com/google-developers/android-data-binding-custom-setters-55a25a7aea47#.7ylt58pzz

我想知道我是否可以使用以下这样的东西

在 xml 中

android:imageUrl="@{param1,param2}"
Run Code Online (Sandbox Code Playgroud)

在java类中

@BindingAdapter(value={"imageUrl"}, requireAll=false)
public static void setImageUrl(ImageView imageView, String url,
        Drawable placeHolder) {
    if (url == null) {
        imageView.setImageDrawable(placeholder);
    } else {
        MyImageLoader.loadInto(imageView, url, placeholder);
    }
}
Run Code Online (Sandbox Code Playgroud)

视图中的一个属性和两个参数。由于我在android数据绑定中看到了lambdas表达式(实际上它并没有真正用JDK 8编译)https://developer.android.com/topic/libraries/data-binding/index.html#listener_binding,任务变量可以添加在处理程序事件中。

android:onClick="@{(theView) -> presenter.onSaveClick(theView, task)}"
Run Code Online (Sandbox Code Playgroud)

自定义绑定方法中是否可以有两个(或更多)参数?

android android-databinding

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

在onClick时获取其他视图的值。数据绑定

我有android数据绑定的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">

    <data>
        <variable name="presenter" type="com.myapp.presentation.presenter.CreateUserPresenter"/>
    </data>
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <android.support.design.widget.TextInputLayout
                    android:id="@+id/firstNameInput"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="16dp"
                    android:paddingTop="8dp"
                    android:paddingRight="16dp"
                    android:paddingBottom="8dp">
                    <android.support.design.widget.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:inputType="textCapSentences"
                        android:hint="@string/first_name" />
                </android.support.design.widget.TextInputLayout>
                <android.support.design.widget.TextInputLayout
                    android:id="@+id/secondNameInput"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="16dp"
                    android:paddingTop="8dp"
                    android:paddingRight="16dp"
                    android:paddingBottom="8dp">
                    <android.support.design.widget.TextInputEditText
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:inputType="textCapSentences"
                        android:hint="@string/second_name" />
                </android.support.design.widget.TextInputLayout>
                <android.support.v7.widget.AppCompatButton
                    android:id="@+id/btnCreate"
                    style="@style/Widget.AppCompat.Button.Colored"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_margin="12dp"
                    android:text="@string/create"
                    android:onClick="@{(view) -> presenter.createUser()}" />    
            </LinearLayout>    
</layout>
Run Code Online (Sandbox Code Playgroud)

CreateUserPresenter.class

public class CreateUserPresenter {

    public CreateUserPresenter () {}

    public void createUser(String firstName, String lastName) {

    }
}
Run Code Online (Sandbox Code Playgroud)

如何在单击按钮的方法中传递输入( …

xml android android-databinding

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

一个textview数据绑定android中的名字和姓氏

如何在数据绑定中连接字符串.我想显示firstName和lastName是相同的TextView.任何帮助都要提前感谢.

<data>

    <variable
        name="task"
        type="com.myapp.app.Task" />
</data>

<TextView
    android:id="@+id/name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="6dp"
    android:drawablePadding="6dp"
    android:ellipsize="end"
    android:gravity="center"
    android:maxLines="1"
    android:text="@={task.assignedTo.firstName}"//how to include lastname
    android:textColor="@color/textBlack"
    android:textSize="14sp"
    tools:text="Saikiran" />
Run Code Online (Sandbox Code Playgroud)

android android-databinding

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

使用自定义XML标记和数据绑定构建不成功的Gradle

当我尝试使用数据绑定和绑定布局中的自定义XML标记运行项目时,我收到下面描述的构建错误:

布局:

<data>
   <variable
        name="data"
        type="com.melontech.sff.viewmodel.DataViewModel" />
</data> 

...

<ImageView 
   android:id="@+id/image"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:src="@mipmap/placeholder_img"
   app:imageUrl="@{data.imageUrl}" />

...
Run Code Online (Sandbox Code Playgroud)

View Model具有以下Binding适配器:

@BindingAdapter("bind:imageUrl")
public static void getImageUrl(ImageView imageView, String url) {
    Timber.d("LOAD IMAGE WITH URL %s", url);
}
Run Code Online (Sandbox Code Playgroud)

创建ViewHolder并进行绑定的代码如下:

@Override
public ModelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    ModelListItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), R.layout.model_list_item, parent, false);
    return new ModelViewHolder(binding);
}
Run Code Online (Sandbox Code Playgroud)

构建错误:

Error:Execution failed for task ':app:transformJackWithJackForDebug'.
Run Code Online (Sandbox Code Playgroud)

com.android.jack.ir.JNodeInternalError:java.lang.Exception:java.lang.RuntimeException:failure,请参阅日志以获取详细信息.无法生成视图绑定器java.lang.IllegalArgumentException:element public default java.util.stream.IntStream codePoints()不是包含类型java.lang.String的成员,也不是com.android.jack.eclipse中的任何超类.位于android.databinding.tool.reflection.annotation的android.databinding.tool.reflection.annotation.AnnotationMethod.(AnnotationMethod.java:49)中的jdt.internal.compiler.apt.model.TypesImpl.asMemberOf(TypesImpl.java:129) .AnnotationClass.getDeclaredMethods(AnnotationClass.java:314)位于android.databinding.tool.exol.exol.MethodBaseExpr.resolveListenersAsMethodReference(MethodBaseExpr.java:71)的android.databinding.tool.reflection.ModelClass.getAbstractMethods(ModelClass.java:401)在android.databinding.tool.BindingTarget.resolveListeners(BindingTarget.java)的android.databinding.tool.Binding.resolveListeners(Binding.java:65)的android.databinding.tool.expr.FieldAccessExpr.resolveListeners(FieldAccessExpr.java:131): 164)在android.databinding.tool.LayoutBinder.(LayoutBinder.java:250)位于android.databinding.tool.DataBinder上的android.databinding.tool.DataBinder.(DataBinder.java:52)android.databinding.tool.CompilerChef.ensureDataBinder(CompilerChef.java:88) (CompilerChef.java:187)在android.databinding.annotationprocessor.ProcessExpressions.writeResourceBundle(ProcessExpressions.java:184)在android.databinding.annotationprocessor.ProcessExpressions.onHandleStep(ProcessExpressions.java:86)在android.databinding.annotationprocessor.ProcessDataBinding $ ProcessingStep.runStep(ProcessDataBinding.java:189)位于android的DB.databprocess.Arocessprocess.ProcessDataBinding $ ProcessingStep.access $ 000(ProcessDataBinding.java:174),位于com的android.databinding.annotationprocessor.ProcessDataBinding.process(ProcessDataBinding.java:79). com.android.jack.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.round(RoundDispatc)中的android.jack.eclipse.jdt.internal.compiler.apt.dispatch.RoundDispatcher.handleProcessor(RoundDispatcher.java:139)her.java:121)位于com.android.jack.eclipse.jdt.internal.compiler的com.android.jack.eclipse.jdt.internal.compiler.apt.dispatch.BaseAnnotationProcessorManager.processAnnotations(BaseAnnotationProcessorManager.java:159). Comiler.processAnnotations(Compiler.java:909)位于com.android.jack.frontend.java.JAstBuilder.compile的com.android.jack.eclipse.jdt.internal.compiler.Compiler.compile(Compiler.java:434) JAstBuilder.java:269)com.android.jack.frontend.java.JackBatchCompiler.performCompilation(JackBatchCompiler.java:219)at com.android.jack.eclipse.jdt.internal.compiler.batch.Main.compile(Main. java:1712)com.android.jack.frontend.java.JackBatchCompiler.compile(JackBatchCompiler.java:184)at …

data-binding android gradle android-custom-attributes android-databinding

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

Android数据绑定的疑惑

It seems to me that Android Data Binding is an interesting tool, but it doesn't tie very well with the (overly) complex architecture of Android. Many example or tutorials show only some basic scenario that obviously works, but when things become harder then problems arise.

For example: many views (like RecyclerView or ViewPager) require adapters or decorators that need Context and it seems wrong to pass the Context to every ViewModel because it breaks the separation of layers.

ViewFlipper: how …

data-binding android android-context android-recyclerview android-databinding

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

数据绑定:根据条件选择字符串资源之一

我的数据对象中有一个布尔变量,当它为true时,要显示来自资源的一个字符串,而当它为false时,要显示另一个。我正在尝试这样做:

android:text="@{sendit.bccMode ? @string/sharebox.bcc_mode_on : @string/sharebox.bcc_mode_off}"
Run Code Online (Sandbox Code Playgroud)

但是出现编译错误:

**** /数据绑定错误**** msg:找不到访问器java.lang.String.bcc_mode_on

我做错了什么?

android android-databinding

1
推荐指数
2
解决办法
1944
查看次数

无法将硬编码的字符串值传递给 Kotlin 中的 @BindingAdapter

将我的 Android 应用程序 Java 代码转换为 Kotlin,我使用数据绑定将自定义字体设置为 TextViews。我曾经从 XML 传递字体字符串,如下所示

app:customFont="@{'harmonia-semibold.ttf'}"

将@BindingAdapter 转换为 kotlin 后,上面的行不起作用,并且预期会抛出expr 或 lambda 表达式,出现 '''错误。用 getter 方法替换硬编码的字符串值非常有效。下面是我的绑定适配器,不知道为什么它不采用硬编码字符串

@JvmStatic 
@BindingAdapter("app:customFont")
fun setCustomFont(textView: TextView, font: String) {                       
      textView.typeface = Typeface.createFromAsset(textView.context.assets, font)
}
Run Code Online (Sandbox Code Playgroud)

谢谢

android kotlin android-databinding android-binding-adapter

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

Android DataBinding LiveData-未通知DialogFragment和BottomSheetDialogFragment中的更改

我有这样BottomSheetDialogFragment的布局:

<data>

    <variable
        name="viewModel"
        type="com.sample.MyViewModel" />
</data>

<TextView android:id="@+id/tvValue"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text='@{String.format("%.1f", viewModel.weight)}'/>

<Button android:id="@+id/cmdUpdate"
                android:layout_height="wrap_content"
                android:layout_width="match_parent"
                android:onClick="@{() -> viewModel.updateWeight()}"
                android:text="@string/update" />
Run Code Online (Sandbox Code Playgroud)

这是kotlin代码:

  // MyViewModel
  val weight = MutableLiveData<Double>()
  fun updateWeight() {
     weight.value?.let {
        weight.value = (it + 0.1)
    }
  }

 // BottomSheetDialogFragment bind view model
 val myViewModel = ViewModelProviders.of(it, factory).get(MyViewModel::class)
 binding.viewModel = myViewModel

// code showing BottomSheet:
 val fragment = MyBottomSheetFragment()
 fragment.show(fragmentManager, "bottomsheet")
Run Code Online (Sandbox Code Playgroud)

第一次打开底片片段,它可以显示重量值,但是当我单击按钮更新重量时,什么也没有发生。从调试器中,我可以看到该updateWeight方法已调用,并且权重值已更改,但未TextView更新。这也发生在其他DialogFragment上。
如果我使用常规代码,则相同的代码也可以工作Fragment
。DialogFragment和DataBinding是否有问题?

android kotlin android-databinding android-livedata android-viewmodel

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

数据绑定失败,"无法猜测"

自Android Studio更新后,3.2.0我遇到以下问题:

任务':mobile:dataBindingGenBaseClassesDebug'的执行失败.

>无法猜测com.ACME.database.model.Order

也看到了这个答案,暗示了"包名必须以小写字母开头".

......似乎是相似的,好像这个variable任务是原因:

<data class=".databinding.OrderFragmentBinding">
    <variable name="order" type="com.ACME.database.model.Order"/>
    ...
</data>
Run Code Online (Sandbox Code Playgroud)

found:用于绑定类的新数据绑定编译器,它不解释行为的变化.

:这样的作业是否也受到该命名惯例的影响?我的意思是,有没有机会(除了改变大写的包名)以使数据绑定v2"猜测"成功?

android android-gradle-plugin android-databinding android-studio-3.2 androidx

1
推荐指数
5
解决办法
6377
查看次数

使用 android-databinding 方法格式化友好的日期

我想将我的日期 YYYY/MM/DD 格式化为更友好的模式。

我使用android-databinding。

我期望的输出应该是示例:2006 年 8 月 22 日,星期二。我当前从 Json 输入的是“2018-09-27”(模型中的字符串数据)

我的代码:

public class DateUtils {

SimpleDateFormat fromServer = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat myFormat = new SimpleDateFormat("dddd, dd MMMM yyyy");

   public String getDateToFromat  (String reciveDate)  {
       String newFormatString = myFormat.format(fromServer.parse(reciveDate));
 return newFormatString;
   };

}
Run Code Online (Sandbox Code Playgroud)

我的布局:

<layout xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data class ="CurrencyBindingDetailItem">
        <import type="com.example.htw.currencyconverter.utils.DateUtils"/>
        <import type="android.view.View" />
        <variable name="currencyItemDetailDate" type="com.example.htw.currencyconverter.model.CurrencyDate"/>
        <variable name="currencyBindingItemDetail" type="com.example.htw.currencyconverter.model.CurrencyBinding"/>
        <variable name="callback" type="com.example.htw.currencyconverter.callback.ClickCallback"/>
    </data>
    <TextView
        android:textSize="28dp"
        android:text="@{DateUtils.getDateToFromat(currencyItemDetailDate.date)}"
        android:textColor="@color/primary_text"
        android:id="@+id/date_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center" />
Run Code Online (Sandbox Code Playgroud)

我确实有错误:

Found data binding …
Run Code Online (Sandbox Code Playgroud)

android android-studio android-databinding

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