我发现很难发现android.databinding.ObservableList作为数据绑定功能的真正存在理由.
起初它看起来像一个很酷的工具来显示列表,通过数据绑定,将它们添加xml到一个RecyclerView.为此,我制作了一个像这样的BindingAdapter:
@BindingAdapter(value = {"items"}, requireAll = false)
public static void setMyAdapterItems(RecyclerView view, ObservableList <T> items) {
if(items != null && (view.getAdapter() instanceof MyAdapter)) {
((GenericAdapter<T>) view.getAdapter()).setItems(items);
}
}
Run Code Online (Sandbox Code Playgroud)
这样一来,我可以使用该属性app:items的RecyclerView一个MyAdapter集来,以更新其项目.
现在最好的功能ObservableList是你可以添加一个OnListChangedCallback,它可以处理RecyclerView添加/移动/删除/更改项目中可用的相同事件,而无需实际重新加载整个列表.
所以我想要实现的逻辑是以下:
MyAdapterObservableArrayList包装它们并将它传递给bindingBindingAdapter传递的项目MyAdapterMyAdapter接收到新的项目,它会清除其旧的并添加OnListChangedCallback到ObservableList接收处理微变ObservableList,MyAdapter将相应更改而不会完全刷新binding变量,这样BindingAdapter …android observablelist android-recyclerview android-databinding android-binding-adapter
我很难@BindingAdapter在我的项目中工作.
@BindingAdapter("imageUrl")
public static void setImageUrl(ImageView imageView, String url) {
Log.d("TEST","URL: " + url);
}
Run Code Online (Sandbox Code Playgroud)
上面的代码显示了它在我的ViewModel中的实现方式.没什么特别的.
<ImageView
android:id="@+id/image_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:layout_below="@id/profile_container"
app:imageUrl="@{item.imageUrl}"
tools:src="@drawable/placeholder_image"/>
Run Code Online (Sandbox Code Playgroud)
这不起作用.命名空间应用程序未绑定.所以我错过了什么.我尝试按照 https://medium.com/google-developers/android-data-binding-custom-setters-55a25a7aea47#.6ygaiwooh 查看他们如何设置bindingAdapter.但有一些我错过了
data-binding android android-databinding android-binding-adapter
我需要了解数据绑定库如何确定其BindingAdapter的执行顺序.如果我有一个View的两个BindingAdapter,如果View有两个属性对应于那些BindingAdapters,它将如何确定首先执行哪个适配器?我问,因为执行顺序在我的情况下很重要.
我有以下BindingAdapter/s:
public class SpinnerBindingAdapter {
@BindingAdapter(value = {"entries"})
public static void setEntries(Spinner spinner, List<? extends SpinnerItem> spinnerItems) {
for (int i = 0; i < spinnerItems.size(); i++) {
spinnerItems.get(i).setIndex(i);
}
ArrayAdapter<? extends SpinnerItem> adapter =
new ArrayAdapter<>(spinner.getContext(), R.layout.spinner_item, spinnerItems);
spinner.setAdapter(adapter);
}
@InverseBindingAdapter(attribute = "selectedItem", event = "selectedItemAttrChanged")
public static Object getSelectedItem(Spinner spinner) {
Object selectedItem = spinner.getSelectedItem();
return selectedItem;
}
@BindingAdapter(value = {"selectedItem"})
public static void setSelectedItem(Spinner spinner, SpinnerItem spinnerItem) {
if (spinner.getAdapter() == null) {
return;
}
// …Run Code Online (Sandbox Code Playgroud) 我正在研究一种绑定适配器方法来在TextView.
@BindingAdapter("foregroundColorSpan", "start", "end", requireAll = false)
fun TextView.setForegroundColorSpan(color: Int, start: Int = 0, end: Int = text.length - 1) {
val spanBuilder = SpannableStringBuilder(text)
spanBuilder.setSpan(ForegroundColorSpan(color), start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
text = spanBuilder
}
Run Code Online (Sandbox Code Playgroud)
这是我如何使用它
<TextView
android:id="@+id/loginLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/narrow"
android:text="@string/already_have_account_login"
android:textColor="@color/grey_aee4e4e4"
android:textSize="@dimen/text_size_small"
app:foregroundColorSpan="@{@color/blue_ae12235b}"
app:start="@{25}" />
Run Code Online (Sandbox Code Playgroud)
我似乎无法获得end指定为文本的最后一个索引的as 参数默认值TextView
是否有任何解决方法可以用来从参数中获取默认值,而无需检查该值是否为 0?
android android-layout kotlin android-databinding android-binding-adapter
我的目标是从我的视图模型将 2 路数据绑定 material.Slider 视图到 MutableLiveData:
<com.google.android.material.slider.Slider
...
android:value="@={viewmodel.fps}"
...
/>
Run Code Online (Sandbox Code Playgroud)
当然,这是行不通的,因为 androidx.databinding 库中没有用于 Slider 的数据绑定适配器
[databinding] Cannot find a getter for <com.google.android.material.slider.Slider android:value> that accepts parameter type <java.lang.Integer>. If a binding adapter provides the getter, check that the adapter is annotated correctly and that the parameter type matches.
Run Code Online (Sandbox Code Playgroud)
但是,他们有一个 SeekBar:/androidx/databinding/adapters/SeekBarBindingAdapter.java
据我了解,2路数据绑定应该只适用于“progress”属性,而1路数据绑定需要两个属性:“onChanged”和“progress”
我尝试为 Slider 调整 SeekBarBindingAdapter:
@InverseBindingMethods({
@InverseBindingMethod(type = Slider.class, attribute = "android:value"),
})
public class SliderBindingAdapter {
@BindingAdapter("android:value")
public static void setValue(Slider view, int value) {
if (value …Run Code Online (Sandbox Code Playgroud) 我已经创建了一个绑定适配器来显示picasso的图片,但它不起作用.我有以下错误:
发现数据绑定错误.****/数据绑定错误****msg:在android.widget.ImageView上找不到参数类型为java.lang.String的属性'app:loadPicture'的setter.file:/home/groupevsc.com/mathieu_labar/Documents/Projects/android-jetpack/app/src/main/res/layout/activity_detail_movie.xml loc:27:31 - 27:52****\data binding error****
这是我的绑定适配器:
object CommonBindingUtil {
@JvmStatic
@BindingAdapter("loadPicture")
fun loadPicture(view: ImageView, text: String) {
Picasso.with(view.context)
.load(text)
.error(R.drawable.ic_movie_24)
.fit()
.placeholder(R.drawable.ic_movie_24)
.into(view)
}
}
Run Code Online (Sandbox Code Playgroud)
我的XML有属性"app:loadPicture":
<ImageView
android:id="@+id/picture"
android:layout_width="@dimen/material_image_simple_width"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="@drawable/ic_movie_24"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:loadPicture="@{viewModel.movie.Poster}"/>
Run Code Online (Sandbox Code Playgroud)
这是我的GitHub存储库:https: //github.com/mlabar/android-jetpack/tree/tech_ajout_databinding
有人有想法解决我的问题吗?
谢谢!
我正在使用android数据绑定库和MVVM体系结构。在xml布局中,我定义了一个名为myModel的viewModel变量。该布局有几个TextInputEditText,我使用了以下自定义绑定适配器:
//makes the drawable_right of the TextView clickable
@SuppressLint("ClickableViewAccessibility")
@BindingAdapter("onDrawableRightClick")
inline fun TextView.setOnDrawableRightClick(crossinline f: () -> Unit) {
this.setOnTouchListener(View.OnTouchListener { _, event ->
if (event.action == MotionEvent.ACTION_UP) {
if (event.rawX >= this.right - this.paddingRight - this.compoundDrawables[DRAWABLE_RIGHT].bounds.width()) {
f()
return@OnTouchListener true
}
}
false
})
}
Run Code Online (Sandbox Code Playgroud)
在布局中,我app:onDrawableRightClick="@{() -> viewModel.doThing()}"仅添加到TextInputEditText之一,然后单击运行。一切正常,没问题。
现在,我返回并添加app:onDrawableRightClick="@{() -> viewModel.doOtherThing()}"到第二个TextInputEditText。这次编译失败,显示为error: missing return statement。
该代码块中的错误在MyFragmentBindingImpl(生成)中:
public final kotlin.Unit _internalCallbackInvoke(int sourceId ) {
switch(sourceId) {
case 1: {
// localize variables for thread safety
// …Run Code Online (Sandbox Code Playgroud) java android kotlin android-databinding android-binding-adapter
I'm trying to handle a LiveData value (profilePicture: Bitmap) within a BindingAdapter function but the first value is always null. The binding adapter is a ViewSwitcher with ProgressBar and ImageView.
Getting profile picture from firebase like this:
val downloadPictureResult = MutableLiveData<Bitmap>()
// ...
fun downloadProfilePic(): LiveData<Bitmap?> {
val imageRef = storage.getReference("images/$uid/profile/profile_picture.jpg")
imageRef.getBytes(ONE_MEGABYTE).addOnCompleteListener { task ->
if (task.isSuccessful) {
//...
downloadPictureResult.value = responseBitmap
//...
} else {
downloadPictureResult.value = null
Log.d(TAG, task.exception?.localizedMessage)
}
}
return downloadPictureResult;
} …Run Code Online (Sandbox Code Playgroud) 我正在创建一个 Android 应用程序,我想使用一个功能,在该功能中,我们输入到 editText 字段中的文本可以在运行时仅在该特定的 editText 字段中转换为大写。
我尝试过使用这段代码
editText.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable?) {
}
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
this.text.toString().uppercase()
}
})
Run Code Online (Sandbox Code Playgroud)
但是通过android中的Inverse Binding Adapter的概念就可以轻松完成。我尝试参考 https://developer.android.com/reference/android/databinding/InverseBindingAdapter来实现它
它在我的项目中对我不起作用。你能一步步解释一下吗?
我在 Android 中使用 databindig 时遇到了一些问题。我想通过 databindig 设置 ImageView 的可见性,并且我认为我已经完成了 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="mapViewModel"
type="neptun.jxy1vz.cluedo.ui.map.MapViewModel" />
</data>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/mapLayout"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="@+id/ivMap"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/map"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:contentDescription="@string/map_description" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/ivDoor"
android:src="@drawable/door_left"
app:layout_constraintStart_toStartOf="@id/guidelineCol7"
app:layout_constraintTop_toTopOf="@id/guidelineRow1"
android:visibility="@={mapViewModel.doorVisibility}"/>
<!-- Guidelines... they are not interesting -->
</androidx.constraintlayout.widget.ConstraintLayout>
</HorizontalScrollView>
</ScrollView>
</layout>
Run Code Online (Sandbox Code Playgroud)
我的 ViewModel 类:
package neptun.jxy1vz.cluedo.ui.map
import android.view.View
import androidx.databinding.BaseObservable
import androidx.databinding.Bindable
import …Run Code Online (Sandbox Code Playgroud) android visibility android-imageview android-databinding android-binding-adapter