gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:03:31 24 4
gpt4 key购买 nike

我有简单的布局和 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com