我有简单的布局和viewModel。我想将它们相互连接,但它们不连接。上述问题的问题是在日志中没有错误,我的应用程序也不会崩溃。DataBinding Android,自定义setter,不工作?
这里我的布局:
<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);
}
}
除上述以外。我也用debug来检查它,上面的方法不会被调用。
你是否在你的'DetailFragment'中为每个viewmodel的实例设置了充气的绑定? – yennsarah
是的,我做过,对于他们每个人 –
正如Path Dave指出的那样,我还建议使用'Views' Context。我也会用你的第二个'BindingAdapter',但省略'bind'。确保你在xml中使用'app:imageUrl'或者你的情况'bind:'(而不是''android:')。毕加索有一个设置占位符('.placeHolder(id)')的方法。你有没有尝试通过在调试模式下设置断点来查看每种方法?你的viewmodels中的值是什么?它是否进入'BindingAdapter'?你能发布你的代码片段吗? – yennsarah