dara
dara

Reputation: 763

DataBinding Android, custom setters, Doesnt work?

I have simple layout and viewModel. I want to connect them with each other but they dont connect.Problem of above problem is in logs there is no error and my app also doesn't crash.

Here my layout:

<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>

Here is my ViewModel class

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);
        }
    }
}

There is no error, There is no crashes. App works normally but there is no any title , any image.I tried this one instead overriding its method writing own but doesn't work.

@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);
    }
}

In addition to above . I also check it with debug, above methods are not called.

Upvotes: 0

Views: 1316

Answers (3)

dara
dara

Reputation: 763

The problem was not in my ViewModel or in my xml file everything is correct there is no syntax error.Problem is here this is fragment and I have connected them together my fragment with my viewModel.I made mistake in creating binding here you can see it. This is my not working one

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

Here is correct one

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

I missed to attach fragment to activity that is why my binding hadn't been working for 21 days.Hope it will help to someone.

Upvotes: 0

tynn
tynn

Reputation: 39873

You should better remove app:setContext="@{viewModel.context}" and get it from the view in your adapter. Also you need to use attribute names without a namespace; so instead of bind:imageUrl only use 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);
    }
}

But since Picasso works asynchronously, you might end up with an image after you already set it to R.drawable.placeholder again.

Eventually you could also have a look at the generated java sources for the binding and see if your BindingAdapter is called somewhere.

Upvotes: 2

Parth Dave
Parth Dave

Reputation: 2126

try like this:

<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);
    }
}

Upvotes: 0

Related Questions