我试图使用android数据绑定插件到目前为止没有运气.
我正在使用:Gradle 2.2.1; Intellij IDEA 15.
项目级build.gradle:
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.5.0'
classpath "com.android.databinding:dataBinder:1.0-rc2"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
Run Code Online (Sandbox Code Playgroud)
模块build.gradle:
apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'
buildscript {
repositories {
jcenter()
}
dependencies {
}
}
repositories {
jcenter()
}
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.app4.app4"
minSdkVersion 10
targetSdkVersion …Run Code Online (Sandbox Code Playgroud) 我将对象的字段——字符串、整数等——绑定到布局文件。例如:
<data>
<variable
name="appState"
type="com.example.app.AppState"
/>
</data>
Run Code Online (Sandbox Code Playgroud)
和
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:title="@{appState.thing}"
/>
Run Code Online (Sandbox Code Playgroud)
这工作正常。但是,我HashMap在该 appState 对象中也有一个值。
是否可以从 this 绑定到值,即android:text="@{appState.thehashmap['thekey']"?
当前的表达式语法似乎不支持它。
但是,我想知道,有没有办法?非常感谢。
大多数流行的 MVVM 框架允许对列表类型属性进行某种循环。例如 Knockout 有foreach-binding。它允许您遍历 list 属性的元素,并且对于每个元素,foreach 循环中包含的标记都被复制。在副本内部,当前元素用作数据绑定的上下文。
我在 Android 中寻找类似的东西,但我只看到了将列表绑定到特定 ui 元素的可能性。但这在声明性方面很糟糕,因为我需要在代码中创建一个 UI 元素。
有没有办法使用 Android 的数据绑定功能来模拟类似的东西?如果没有,是否有解决方法?或者扩展绑定语法的方法?
我有 android 数据绑定问题。我有不同配置的布局,例如:activity_main.xml / land/activity_main.xml 等。目前当我使用 setContentView 方法时,只需传递布局名称,它会自动检测应该选择哪个布局来设置内容视图。
但是如果我使用数据绑定,那么解决方案是什么。据我所知,绑定的名称会因配置而异。所以如果我使用 ActivityMainBinding,那总是相同布局的数据绑定。我阅读了有关指定标记(每个配置的布尔值)并使用 if/else 语句然后膨胀所需绑定的解决方案,但这是非常糟糕的解决方案。
对于活动/片段对不同的配置布局/端口/sw600-端口/土地等使用不同布局的情况,任何人都可以提出更好的解决方案吗?
谢谢!
我使用的是Android Studio 3.0 Canary 6版本.我在我的类中启用了dataBinding,代码没有显示任何错误.但是,当我构建APK时,构建失败并显示以下错误:
Error:(8, 37) Unresolved reference: databinding
Error:(22, 26) Unresolved reference: ActivityMainBinding
Error:(38, 50) Unresolved reference: ActivityMainBinding
Error:(43, 52) Unresolved reference: ActivityMainBinding
Error:(46, 52) Unresolved reference: ActivityMainBinding
Error:(49, 52) Unresolved reference: ActivityMainBinding
Error:(52, 52) Unresolved reference: ActivityMainBinding
Error:(55, 52) Unresolved reference: ActivityMainBinding
Error:(58, 52) Unresolved reference: ActivityMainBinding
Error:(61, 52) Unresolved reference: ActivityMainBinding
Error:(64, 52) Unresolved reference: ActivityMainBinding
Error:(67, 52) Unresolved reference: ActivityMainBinding
Error:(70, 52) Unresolved reference: ActivityMainBinding
Error:(73, 52) Unresolved reference: ActivityMainBinding
Error:Execution failed for task ':app:compileDebugKotlin'. …Run Code Online (Sandbox Code Playgroud) data-binding android kotlin build.gradle android-databinding
我试图从Activity本身(而不是来自适配器)监听我的Recycler视图上的行点击(项目点击).
我的适配器到目前为止如下所示:
public class ListMiestnostiAdapter extends RecyclerView.Adapter<ListMiestnostiAdapter.ViewHolder>{
private List<Miestnost> data;
// you provide access to all the views for a data item in a view holder
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewDataBinding binding;
public ViewHolder(ViewDataBinding binding) {
super(binding.getRoot());
this.binding = binding;
}
public void bind(Object obj){
binding.setVariable(BR.obj, obj);
binding.executePendingBindings();
}
}
public ListMiestnostiAdapter(List<Miestnost> data) {
this.data = data;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
ActivityListMiestnostiRowBinding …Run Code Online (Sandbox Code Playgroud) android android-adapter android-recyclerview android-databinding
在一个测试应用程序中,我有一个带有一个 Edittext 和一个按钮的活动。Edittext 指向人员姓名的 ObservableField 并单击按钮,我正在修改人员姓名,以便编辑文本中的文本应该更改。但是更改人名不会在 edittext 内更新。
工作时: 如果我创建一个新的 Person 对象并将其设置为 observable 字段,它就会工作。
当它不起作用 但是如果我修改同一个 person 对象,有人知道为什么它在这种情况下不起作用吗?
人.java:
public class Person {
private int age;
private String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Run Code Online (Sandbox Code Playgroud)
人物视图模型:
public class PersonViewModel { …Run Code Online (Sandbox Code Playgroud) 我试图在我的错误文本中加载复数TextInputEditText:
<android.support.design.widget.TextInputEditText
android:id="@+id/et_subject_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textCapWords"
android:lines="1"
android:maxLines="1"
android:singleLine="true"
android:text="@{ model.name }"
android:hint="@string/hint_name"
app:validateRegex='@{"^*\\S{2}.*$"}'
app:validateRegexMessage="@{@plurals/error_too_short(model.minNameLength)}"/>
Run Code Online (Sandbox Code Playgroud)
但是在我的应用程序中,它显示了我的字符串而%d不是model.minNameLength值。这app:validateRegex是一个由数据绑定验证器库定义的属性。
我的复数:
<plurals name="error_too_short">
<item quantity="one" >Too short, enter at least %d visible character.</item>
<item quantity="other" >Too short, enter at least %d visible characters.</item>
</plurals>
Run Code Online (Sandbox Code Playgroud)
怎么了?或者有另一种在xml中显示复数的方法吗?
PS 如果重要的话,我使用数据绑定引擎 V2。
在Java中,我可以使用以下方法轻松地将静态函数传递给布局xml:
public static String formatUnixTime(long timeInSeconds, String pattern) {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat(pattern, Locale.US);
String value = simpleDateFormat.format(new Date(timeInSeconds * 1000));
return value;
}
Run Code Online (Sandbox Code Playgroud)
在xml中:
android:text='@{Utils.formatUnixTime(model.start_time, "hh:mm:ss")}'
Run Code Online (Sandbox Code Playgroud)
但是我在科特林尝试过companion但没有运气。它说
error: cannot find symbol
import my.package.name.HistoryItemBindingImpl;
^
symbol: class HistoryItemBindingImpl
location: package my.package.name
Run Code Online (Sandbox Code Playgroud)
这就是我在科特林尝试过的
class Utils {
companion object {
fun formatUnixTime(timeInSeconds : Long, pattern: String) : String {
val simpleDateFormat = SimpleDateFormat(pattern, Locale.US)
val value = simpleDateFormat.format(Date(timeInSeconds * 1000))
return value
}
}
Run Code Online (Sandbox Code Playgroud)
并在xml中
android:text='@{Utils.Companion.formatUnixTime(model.start_time, "hh:mm:ss")}'
Run Code Online (Sandbox Code Playgroud)
真希望有人能帮忙。谢谢!
使用@Max …
好的,我的任务是将项目迁移到AndroidX,以减少项目中支持库的混乱情况。我已经根据官方文档启用了AndroidX,但是现在尝试通过相应的自动生成的Binding类(通过在模块gradle中启用数据绑定而创建)来充气视图时,出现了运行时错误。
挖掘自动生成的源代码时,我遇到了这种方法,该方法导致代码抛出:
public List<DataBinderMapper> collectDependencies() {
ArrayList<DataBinderMapper> result = new ArrayList(1);
result.add(new com.android.databinding.library.baseAdapters.DataBinderMapperImpl());
return result;
}
Run Code Online (Sandbox Code Playgroud)
如您所见,自动生成的代码正在尝试从com.android.databinding程序包中实例化一个类,但是该程序包在输出APK中不存在,因为我已从gradle中删除了支持依赖项(因为AndroidX应该替换了它们)。我可以看到androidx有一个数据绑定包,所以我假设上面的自动生成的代码应该引用androidx.databinding包,但是没有。
这是工具中的错误还是我配置错误?
这是我的gradle文件(出于安全原因省略了一些位):
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-android-extensions'
//These variable refer to release builds so make sure they are correct. If you need to override them
//for some specific development needs then use variables that can be passed to gradle on command line.
String releaseVersionName = '1.0.0'
int releaseVersionCode = 1
int releaseMinSdk = 18 …Run Code Online (Sandbox Code Playgroud)