我有簡單的佈局和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