用于数据绑定
1)我已经补充说
android {
dataBinding {
enabled = true
}
Run Code Online (Sandbox Code Playgroud)
到我的项目build.gradle,但发生此错误:
Error:(5, 0) Gradle DSL method not found: 'dataBinding()'
Possible causes:
.The project 'exampleDatabinding' may be using a version of Gradle that does
not contain the method.
Gradle settings
.The build file may be missing a Gradle plugin.
Apply Gradle plugin
Run Code Online (Sandbox Code Playgroud)
2)然后我补充说:
apply plugin: "com.android.databinding"(项目build.gradle)
和classpath "com.android.databinding:dataBinder:1.0-rc1" (项目build.gradle)
但是同样的错误发生了.
Android的DataBinding库是否与Transitions框架一起使用?
Scene scene = Scene.getSceneForLayout(this, R.layout.creditcardentryview_scene2_expanded, this.getContext());
TransitionManager.go(scene);
scene2Binding = CreditcardentryviewScene2ExpandedBinding.bind(this);
Run Code Online (Sandbox Code Playgroud)
尝试上面的代码会引发此错误: view tag isn't correct on view:null
data-binding android android-transitions android-databinding
我正在尝试RecyclerView使用DataBinding 来创建Integer数据,我已经通过string-array在中声明来尝试使用String数据string.xml,这是我参考的答案。
现在,我正在尝试integer-array使用xml 来实现它,但是无法从xml访问它。
这是我的integer.xml
<integer-array name="hours">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
<item>11</item>
<item>12</item>
</integer-array>
Run Code Online (Sandbox Code Playgroud)
这是我的xml文件
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:entries="@{@integerArray/hours}">
Run Code Online (Sandbox Code Playgroud)
和我的自定义绑定适配器
@BindingAdapter("entries")
public static void entries(RecyclerView recyclerView, Integer[] array) {
recyclerView.setAdapter(new SimpleArrayAdapter(array));
}
Run Code Online (Sandbox Code Playgroud)
但这显示了我的错误 line 1:0 token recognition error at: '@integerArray/'
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
> java.lang.RuntimeException: Found data binding errors.
****/ data binding error ****msg:Identifiers must have user defined types from the XML …Run Code Online (Sandbox Code Playgroud) 我试图绑定:
@Bindable
public float getRoundInEditAmount()
{
return roundInEdit.getAmount();
}
@Bindable
public void setRoundInEditAmount(float amount)
{
roundInEdit.setAmount(amount);
notifyPropertyChanged(BR.roundInEditAmount);
}
Run Code Online (Sandbox Code Playgroud)
至
<EditText
android:layout_width="100dp"
android:layout_height="50dp"
android:inputType="numberDecimal"
android:text="@={`` + weightSet.roundInEditAmount}"
></EditText>
Run Code Online (Sandbox Code Playgroud)
但是,在单击EditText时,我会看到一个文本输入而不是数字键盘。如果再次单击此EditText,则会显示数字键盘。如果该字段默认为50.0或其他值,则无法删除这些金额。虽然我可以输入文字,但它确实存在。
还有其他人遇到这种行为的原因是第一次单击时输入的文本是数字输入吗?EditText上的两种方式绑定是否也按我期望的方式工作。我已经编写了自己的Binding和InverseBinding适配器,它们的行为方式相同->第一次单击时是TextInput,然后在第二次单击时是数字键盘,但是您不能删除开头的数字。
我想用绑定创建一个电影海报图像的网格视图.
我的viewmodel看起来像这样:
public class PopularMoviesViewModel extends BaseObservable {
Movie movie;
Context context;
MovieServiceComponent movieServiceComponent = DaggerMovieServiceComponent.builder()
.contextModule(new ContextModule(context))
.build();
Picasso getPicasso = movieServiceComponent.getPicasso();
public PopularMoviesViewModel(Movie movie, Context context) {
this.movie = movie;
this.context = context;
}
@Bindable
public String getImageUrl(){
return movie.posterPath();
}
@Bindable
public String getTitle(){
return movie.originalTitle();
}
@BindingAdapter({"imageUrl"})
public void setImageUrl(ImageView view, String poserPath){
getPicasso.with(view.getContext()).load("http://image.tmdb.org/t/p/w185"+ poserPath).into(view);
}
}
Run Code Online (Sandbox Code Playgroud)
布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<data class="PopularMoviesBinding">
<variable
name="pmvm"
type="com.hartyandi.oviesm.modelviews.PopularMoviesViewModel"></variable>
</data>
<LinearLayout
android:id="@+id/row"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:paddingBottom="0dp" …Run Code Online (Sandbox Code Playgroud) 通过示例,我看到了两种使用Android架构组件的MVVM方法.
第一种方法:
ViewModel 提供 LiveDataActivity 订阅 LiveDataActivity正在设置数据时ViewModel ObservableField.ViewModel传递给绑定.在xml你刚刚设置ObservableField为值
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:visibleGone="@{viewmodel.listLoading}"/>
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swiperefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:refreshing="@{viewmodel.listRefreshing}"
app:onRefreshListener="@{() -> viewmodel.refreshList()}"
app:visibleGone="@{!viewmodel.listLoading}">
Run Code Online (Sandbox Code Playgroud)优点:我并不需要通过状态(例如"加载"),因为我更新listLoading ObservableField在ViewModel为这样的:
val listLoading = ObservableBoolean(false)
/** other observable fields go here **/
val list: MutableLiveData<List<Item>> = MutableLiveData()
fun loadList() {
listLoading.set(true)
repo.getList { items ->
list.value = items
listLoading.set(false)
}
}
Run Code Online (Sandbox Code Playgroud)
缺点:这种方法有任何缺点吗?
第二种方法:
ViewModel 提供 LiveDataActivity …android design-patterns android-databinding android-mvvm android-architecture-components
我正在尝试消除我的Android应用程序的所有警告,其中之一就是:
viewModel.value是一个盒装字段,但需要取消装箱才能执行android:checked.这可能会导致NPE,因此数据绑定将安全地取消它.您可以更改表达式并使用safeUnbox()显式包装viewModel.value以防止出现警告
其中value是ObservableField来自超类的泛型:
public abstract class BaseDataTypeViewModel<T> extends BaseObservable {
public final ObservableField<T> value = new ObservableField<>();
...
}
Run Code Online (Sandbox Code Playgroud)
并在某处作为一个扩展Boolean:
public class CheckBooleanDataTypeViewModel extends BaseDataTypeViewModel<Boolean> {
...
}
Run Code Online (Sandbox Code Playgroud)
我看到数据绑定 - safeUnbox警告发生警告,因为这是一个Boolean而不是一个boolean,所以我试图添加这个:android:checked="@={safeUnbox(viewModel.value)}"而不是android:checked="@={viewModel.value}"然后我得到一个错误说我无法反转safeUnbox()方法.
****/数据绑定错误****msg:表达式android.databinding.DynamicUtil.safeUnbox(viewModelValue)无法反转:方法safeUnbox没有反转,必须在方法中添加@InverseMethod注释才能指示在双向绑定表达式中使用它时应该使用哪个方法
我正确地理解了2个分离的问题,但是我必须忍受警告以避免错误,或者他们是避免警告和错误的解决方案吗?这@InverseMethod是怎么回事?我没有设法添加这个注释,因为该方法来自android包.
我开始使用DataBinding,而onClick出了点问题。
GameViewModel.java
public void onClickItem(int row, int col){
Log.d("click","row: "+row+" col: "+col);
}
@BindingAdapter("load_image")
public static void loadImage(ImageView view,int imageId) {
view.setImageResource(getDrawable(imageId));
}
Run Code Online (Sandbox Code Playgroud)
GameFragment.java
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//View view=inflater.inflate(R.layout.fragment_game, container, false);
FragmentGameBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_game, container, false);
View view = binding.getRoot();
ButterKnife.bind(this,view);
binding.setGameViewModel(gameViewModel);
gameViewModel= ViewModelProviders.of(getActivity()).get(GameViewModel.class);
gameViewModel.init();
return view;
}
Run Code Online (Sandbox Code Playgroud)
fragment_game.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"
tools:context=".view.GameFragment">
<data>
<import type="android.support.v4.app.Fragment"/>
<import type="android.view.View"/>
<variable
name="gameViewModel"
type="harkor.addus.viewmodel.GameViewModel" />
</data>
<android.support.constraint.ConstraintLayout
(...)>
<TextView
(...)> …Run Code Online (Sandbox Code Playgroud) 将Android Studio从3.1.2更新到3.2.0,并将gradle插件更新到3.2.0之后,我遇到了生成的数据绑定类的问题,该类抱怨不存在的软件包名称,但确实存在存在。该软件包属于项目中的一个模块。
这是尝试运行应用程序时遇到的错误:
错误:找不到符号类助手
错误:软件包助手不存在
这是我的项目级别build.gradle文件:
buildscript {
repositories {
google()
jcenter()
mavenCentral()
maven {
url 'https://maven.fabric.io/public'
}
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath "com.google.gms:google-services:4.0.1"
classpath 'io.fabric.tools:gradle:1.25.4'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
maven {
url 'https://jitpack.io'
}
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Run Code Online (Sandbox Code Playgroud)
这是build.gradle有问题的模块的:
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
buildToolsVersion …Run Code Online (Sandbox Code Playgroud) 我有一个类型的变量,(position: Int) -> Unit我想像这样从xml调用方法android:onClick="@{theMethod.invoke(someInt)}
有可能吗,那我该怎么办呢?
android ×10
data-binding ×3
android-architecture-components ×1
android-mvvm ×1
gradle ×1
onclick ×1
picasso ×1
warnings ×1