data-binding-support
oxoooo
34
Visit GitHub RepoData Binding

Data Binding Support Library

Provides useful subclasses from Support Library that work with Data Binding.

Download

repositories {
    jcenter()
    maven { url "https://jitpack.io" }
}

dependencies {
    compile 'ooo.oxo:data-binding-support:1.1.1'

    compile 'com.android.support:appcompat-v7:23.2.1'
    compile 'com.android.support:recyclerview-v7:23.2.1'
}

Usage

BindingAppCompatActivity and BindingFragment

public class MainActivity extends BindingAppCompatActivity<MainActivityBinding> {

    @Override
    protected int provideContentView(@Nullable Bundle savedInstanceState) {
        return R.layout.main_activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // do anything you want, for example:
        binding.setUser(user);
    }

}
public class MainFragment extends BindingFragment<ProfileFragmentBinding> {

    @Override
    public int provideContentView(@Nullable Bundle savedInstanceState) {
        return R.layout.profile_fragment;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        // do anything you want, for example:
        binding.setUser(user);
    }

}

BindingRecyclerView

public class PhotoAdapter extends BindingRecyclerView.ListAdapter<Photo, PhotoAdapter.ViewHolder> {

    public PhotoAdapter(Context context, ObservableList<Photo> data) {
        super(context, data);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(inflater, parent);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.binding.setItem(data.get(position));
    }

    public class ViewHolder extends BindingRecyclerView.ViewHolder<PhotoItemBinding> {

        public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
            super(inflater, R.layout.photo_item, parent);
        }

    }

}
private final ObservableList photos = new ObservableArrayList();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* ... */

    recyclerView.setAdapter(new PhotoAdapter(this, photos));

    // Just add your items in the ObservableList, BindingRecyclerView.ListAdapter will take care of the
    // binding for you.
    photos.addAll(/* ... */);
}

@Override
protected void onDestroy() {
    super.onDestroy();

    /* ... */

    // Remember to release the adapter, because it has a reference to the `photos` ObservableList.
    recyclerView.setAdapter(null);
}

License

MIT License

Become a better Android Developer
Millions of developers are learning at MindOrks

Online Training Program

Featured Blogs

Our Team

MindOrks is Certainly one of the best online blog to stay on top of all the Android development news, coding and design patterns. Finally a blog I can count on to keep myself updated with latest and greatest things happening in Android world.

Vipul Shah
Android Developer
Microsoft

Become Pro in Android by watching videos

OUR LEARNERS WORK AT