2017-02-26 74 views
0

我有簡單的佈局和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來檢查它,上面的方法不會被調用。

+0

你是否在你的'DetailFragment'中爲每個viewmodel的實例設置了充氣的綁定? – yennsarah

+0

是的,我做過,對於他們每個人 –

+0

正如Path Dave指出的那樣,我還建議使用'Views' Context。我也會用你的第二個'BindingAdapter',但省略'bind'。確保你在xml中使用'app:imageUrl'或者你的情況'bind:'(而不是''android:')。畢加索有一個設置佔位符('.placeHolder(id)')的方法。你有沒有嘗試通過在調試模式下設置斷點來查看每種方法?你的viewmodels中的值是什麼?它是否進入'BindingAdapter'?你能發佈你的代碼片段嗎? – yennsarah

回答

0

這個問題是不是在我的ViewModel或在我的xml文件中一切正確沒有語法錯誤。問題是這裏這是片段,我已經把它們連接在一起我的片段與我的viewModel.I在這裏創建綁定時出錯你可以看到。這是我的工作不是一個

FragmentStudentLifeBinding binding = DataBindingUtil.inflate(inflater,R.layout.fragment_student_life,container,false); 

這是正確的

FragmentStudentLifeBinding binding = DataBindingUtil.inflate(inflater,R.layout.fragment_student_life,container,true); 

我錯過片段連接到活動,這就是爲什麼我綁定沒有工作了21個days.Hope這將有助於有人。

0

嘗試這樣的:

<layout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:bind="http://schemas.android.com/apk/res-auto" 
> 
    ..... 
     <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" 
       bind:src="@{viewModel.image}" 
       app:layout_collapseMode="parallax" 
       bind:setContext="@{viewModel.context}" /> 
    ..... 
</layout> 

@BindingAdapter({"bind:loadUrl"}) 
public static void downloadImage(RoundedImageViewimageView, String url) { 
    if (url != null) { 
     Picasso.with(imageView.getContext()).load(url).into(imageView); 
    } else { 
     imageView.setImageResource(R.mipmap.ic_launcher); 
    } 
} 
+0

對不起,他沒有工作 –

2

你應該更好地刪除app:setContext="@{viewModel.context}"和在適配器的觀點得到它。你也需要使用沒有命名空間的屬性名稱;因此而不是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後再次,你可能最終得到的圖像。

最終,您還可以查看生成的綁定的java源代碼,並查看您的BindingAdapter是否在某處被調用。