Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data-Binding using LiveData #34

Closed
gaurav414u opened this issue May 28, 2017 · 15 comments

Comments

@gaurav414u
Copy link

commented May 28, 2017

in Basic Sample, ProductFragment.java

// Observe product data
model.getObservableProduct().observe(this, new Observer<ProductEntity>() {
    @Override
    public void onChanged(@Nullable ProductEntity productEntity) {
        model.setProduct(productEntity);
   }
});

We observe for a change in LiveData<ProductEntity> mObservableProduct. When a change is there we set ObservableField<ProductEntity> product so that the data-binding library updates the UI. It seems like too much work perhaps.

My understanding would be that since LiveData cares about the lifecycle owner and not the view, hence databinding doesn't support LiveData. Is that so? @yigit
If this is the right way of doing it, is there any better way of using Data-Binding with LiveData?

@iammert

This comment has been minimized.

Copy link
Contributor

commented Jun 2, 2017

@BindingAdapter will solve your problem @gaurav414u . In my case I need to update list on LiveData has changed. Here is how I achieve this by using databinding.

I created a BindingAdapter to update list.

public final class ListBindingAdapter{
    @BindingAdapter(value = "resource")
    public static void setResource(RecyclerView recyclerView, Resource resource){
        RecyclerView.Adapter adapter = recyclerView.getAdapter();
        if(adapter == null)
            return;

        if(resource == null || resource.data == null)
            return;

        if(adapter instanceof BaseAdapter){
            ((BaseAdapter)adapter).setData((List) resource.data);
        }
    }
}

Then I need to add list as variable to my xml.

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="resource"
            type="iammert.com.androidarchitecture.data.Resource"/>
    </data>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:resource="@{resource}"/>

</layout>

Then, all I need to do is setResource on live data change.

movieListViewModel
                .getPopularMovies()
                .observe(this, listResource -> binding.setResource(listResource));
@yigit

This comment has been minimized.

Copy link
Collaborator

commented Jun 3, 2017

We are going to make data binding lifecycle aware but for that to happens, lifecycle needs to hit 1.0 stable api.

@yigit yigit closed this Jun 3, 2017

@theanh0512

This comment has been minimized.

Copy link

commented Sep 28, 2017

@iammert ((BaseAdapter)adapter) how you cast RecyclerView adapter to BaseAdapter?

@rachitmishra

This comment has been minimized.

Copy link

commented Oct 17, 2017

@iammert how do we test that part :)

@addmoremobile

This comment has been minimized.

Copy link

commented Nov 7, 2017

@yigit as of 1.0.0 is out now, is it now possible to bind livedata?

@iammert

This comment has been minimized.

Copy link
Contributor

commented Nov 7, 2017

@theanh0512 I created BaseAdapter abstract class which has setData(List<T> data).

@iammert

This comment has been minimized.

Copy link
Contributor

commented Nov 7, 2017

@yigit @JoseAlcerreca Hey! Thanks for stable version! It is already hit to the stable version. Do you have any update about making databinding lifecycle aware?

@gaurav414u

This comment has been minimized.

Copy link
Author

commented Nov 7, 2017

@yigit @JoseAlcerreca Hi! I think, its time to reopen this issue as the lifecycle has already hit 1.0 stable api.
Thoughts?

@wsura

This comment has been minimized.

Copy link

commented Nov 29, 2017

LiveData without data binding kinda makes little sense, because you have to set/listen your way through again or manually throw data between observables and LiveData and that increases amount of vode required to present data, not decreases. @yigit, it definitely needs revising - especially, that you already hit 1.0 stable. Please reopen.

@a141199

This comment has been minimized.

Copy link

commented Nov 30, 2017

Keen to see this happen also.

@rhexgomez

This comment has been minimized.

Copy link

commented Dec 9, 2017

The above proposal is interesting and make sense so I really wanted this thread to be re-opened if possible.

@jereksel

This comment has been minimized.

Copy link

commented Dec 21, 2017

It seems like it's possible now with new Canary build: https://androidstudio.googleblog.com/2017/12/android-studio-31-canary-6-is-now.html

"You can now use a LiveData object as an observable field in data binding expressions. The ViewDataBinding class now includes a new setLifecycle method that you need to use to use to observe LiveData objects."

@erikjhordan-rey

This comment has been minimized.

Copy link

commented Jan 4, 2018

I created a project sample to show how works Live Data + Data Binding maybe can be useful for someone. https://github.com/erikcaffrey/LiveData-DataBinding-Kotlin

@stabib

This comment has been minimized.

Copy link

commented Jan 8, 2018

Here's another implementation by @pszklarska:
https://github.com/pszklarska/LiveDataBinding

@filipkowicz

This comment has been minimized.

Copy link

commented Aug 10, 2018

note that it's working for two-way data binding also!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
You can’t perform that action at this time.