2017-02-26 76 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是否在某处被调用。