android - DataBinding Android,自定义 setter ,不起作用?

标签 android mvvm data-binding android-databinding

我有简单的布局和 viewModel。我想将它们相互连接,但它们没有连接。上述问题的问题是在日志中没有错误,我的应用程序也没有崩溃。

这是我的布局:

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

<data>

    <variable
        name="progressView"
        type="uz.iutlab.ictnews.viewModel.ProgressViewModel" />

    <variable
        name="viewModel"
        type="uz.iutlab.ictnews.viewModel.DetailFragmentViewModel" />
</data>

<RelativeLayout
    android:id="@+id/activity_detail"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context="uz.iutlab.ictnews.view.fragment.DetailFragment">

    <RelativeLayout
        android:id="@+id/fragment_detail_post_root"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="@dimen/collapsing_image_height">

            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <com.makeramen.roundedimageview.RoundedImageView
                    android:id="@+id/fragment_detail_image_post"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:adjustViewBounds="true"
                    android:scaleType="centerCrop"
                    android:src="@{viewModel.image}"
                    app:layout_collapseMode="parallax"
                    app:setContext="@{viewModel.context}" />

                <android.support.v7.widget.Toolbar
                    android:id="@+id/fragment_detail_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_collapseMode="pin" />

                <TextView
                    android:id="@+id/fragment_detail_title_post"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:padding="@dimen/spacing_view_large"
                    android:text="@{viewModel.title}"
                    android:textColor="@android:color/white"
                    android:textSize="@dimen/font_size_title_huge" />
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>
    </RelativeLayout>
</RelativeLayout>

这是我的 ViewModel 类
public class DetailFragmentViewModel extends BaseObservable {

    private Post mPost;
    private Context mContext;

    public DetailFragmentViewModel(Post mPost,Context mContext) {
        this.mPost = mPost;
        this.mContext = mContext;
    }

    public String getTitle() {
        return mPost.getTitle().getRendered();
    }

    public Context getContext () {
        return mContext;
    }

    public String getImage() {
        if (mPost.getMedia().getSource_url() != null) {
            return mPost.getMedia().getSource_url();
        } else {
            return null;
        }
    }

    @BindingAdapter({"android:src","setContext"})
    public static void downloadImage (RoundedImageView imageView,String url,Context context) {
        if (url!=null) {
            Picasso.with(context).load(url).into(imageView);
        } else {
            imageView.setImageResource(R.drawable.placeholder);
        }
    }
}

没有错误,没有崩溃。应用程序正常工作,但没有任何标题,任何图像。我尝试了这个,而不是覆盖其编写自己的方法,但不起作用。
@BindingAdapter({"bind:imageUrl","setContext"})
public static void downloadImage (RoundedImageView imageView,String url,Context context) {
    if (url!=null) {
        Picasso.with(context).load(url).into(imageView);
    } else {
        imageView.setImageResource(R.drawable.placeholder);
    }
}

除了以上。我也用调试检查它,上面的方法没有被调用。

最佳答案

您最好删除 app:setContext="@{viewModel.context}"并从适配器中的 View 中获取它。您还需要使用没有命名空间的属性名称;所以而不是bind:imageUrl仅使用 imageUrl .

@BindingAdapter("imageUrl")
public static void downloadImage (RoundedImageView imageView, String url) {
    if (url != null) {
        Picasso.with(imageView.getContext()).load(url).into(imageView);
    } else {
        imageView.setImageResource(R.drawable.placeholder);
    }
}

但是自从Picasso异步工作,您可能会在将其设置为 R.drawable.placeholder 后得到一个图像再次。

最后,您还可以查看为绑定(bind)生成的 java 源代码,看看您的 BindingAdapter在某处被调用。

关于android - DataBinding Android,自定义 setter ,不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42466165/

相关文章:

wpf - prism5 regionManager.RequestNavigate 显示 System.object

c# mvvm绑定(bind)按钮命令参数来控制compositeCollection

c# - DataBindXY 为 X 值赋零

java - 使用 Volley 通过 GSON 解析 JSON 时出错

java - 保存大文件时出现内存不足错误

wpf - 在 WPF 中使用命令绑定(bind)时处理异常的最佳做法是什么?

javascript - AngularJS 变量之间的绑定(bind)

android - 扩展的 SurfaceView 的 onDraw() 方法从未被调用

java - OutputStreamWriter.append 不将文本附加到文本文件 Android 编程

asp.net - 删除一些 ListItems 后,在 GridView 内的 DropDownList 中维护所选项目