我遇到了數據綁定問題。我試圖包含一個XML佈局設置包括的TextView
。然而,它解析爲所包含的佈局的名稱由佈局前綴和_0即layout/common_helpinfo_0
如何克服實施字符串資源數據綁定的問題
後綴在主要佈局我有: -
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
....
<LinearLayout
.....
>
<TextView
.... />
<include layout="@layout/common_helpinfo"
android:id="@+id/hi_tag_world1"
app:tagstr="@{@string/hi_tag_world1}"
>
</include>
</LinearLayout>
....
</LinearLayout>
</layout>
所包含的佈局common_helpinfo
是: -
<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="tagstr" type="String"/>
</data>
<TextView
....
android:tag="@{tagstr}"
''''
android:onClick="clickHelpInfo"
>
</TextView>
</layout>
要測試此操作,我在我的MainActivity
中有以下內容: -
public void clickHelpInfo(View v) {
String hicaller = v.getTag().toString();
int vid = v.getId();
Toast.makeText(this,"You Clicked Help from button=" + hicaller + " ID+" + Integer.toString(vid)
, Toast.LENGTH_SHORT).show();
}
在主要佈局我也有,附帶的TextView中的硬編碼的等價: -
<TextView
....
android:tag="@string/hi_tag_world1"
''''
android:onClick="clickHelpInfo"
>
</TextView>
而這按預期工作,與吐司顯示的字符串資源hi_tag_world1
內容。
在ActivitMainBinding
,成功編譯後,我有這似乎顯示,其中不正確的數據被設置如下(注我實際上已經包括了common_helpinfo兩次都採取同樣): -
private static final android.util.SparseIntArray sViewsWithIds;
static {
sIncludes = new android.databinding.ViewDataBinding.IncludedLayouts(12);
sIncludes.setIncludes(1,
new String[] {"common_helpinfo"}, <<<<<
new int[] {3},
new int[] {R.layout.common_helpinfo}); <<<<<
sIncludes.setIncludes(2,
new String[] {"common_helpinfo"}, <<<<<
new int[] {4},
new int[] {R.layout.common_helpinfo}); <<<<<
sViewsWithIds = new android.util.SparseIntArray();
sViewsWithIds.put(R.id.vtext01, 5);
sViewsWithIds.put(R.id.vtext02, 6);
sViewsWithIds.put(R.id.vtext03, 7);
sViewsWithIds.put(R.id.tvhi03, 8);
sViewsWithIds.put(R.id.etext01, 9);
sViewsWithIds.put(R.id.actvemail, 10);
sViewsWithIds.put(R.id.lvemail, 11);
}
但是後來在ActivityMainBinding
我得到這似乎試圖得到正確的數據如下: -
@Override
protected void executeBindings() {
long dirtyFlags = 0;
synchronized(this) {
dirtyFlags = mDirtyFlags;
mDirtyFlags = 0;
}
// batch finished
if ((dirtyFlags & 0x4L) != 0) {
// api target 1
this.hiTagWorld1.setTagstr(getRoot().getResources().getString(R.string.hi_tag_world1));
this.hiTagWorld2.setTagstr(getRoot().getResources().getString(R.string.hi_tag_world2));
}
executeBindingsOn(hiTagWorld1);
executeBindingsOn(hiTagWorld2);
}
我看並根據我的代碼上How do I use databinding to combine a string from resources with a dynamic variable in XML?
我已經通過Data Binding Library
閱讀我打開數據綁定和擁有的build.gradle爲: -
android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "mjt.testvcsuse"
minSdkVersion 15
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
dataBinding {
enabled = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:25.1.0'
testCompile 'junit:junit:4.12'
}