Created
August 11, 2015 14:04
-
-
Save alexfacciorusso/8ce8370c95f9743cb12f to your computer and use it in GitHub Desktop.
Some utility classes for Data Binding.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.databinding.DataBindingUtil; | |
import android.databinding.ViewDataBinding; | |
import android.support.v7.app.AppCompatActivity; | |
import android.view.View; | |
import android.view.ViewGroup; | |
/** | |
* @author alexfacciorusso | |
* @since 11/08/15. | |
*/ | |
public class BindingCompatActivity<BT extends ViewDataBinding> extends AppCompatActivity { | |
private BT mBinding; | |
@Override | |
public void setContentView(int layoutResID) { | |
mBinding = DataBindingUtil.setContentView(this, layoutResID); | |
} | |
@Override | |
public void setContentView(View view) { | |
super.setContentView(view); | |
mBinding = DataBindingUtil.getBinding(view); | |
} | |
@Override | |
public void setContentView(View view, ViewGroup.LayoutParams params) { | |
super.setContentView(view, params); | |
mBinding = DataBindingUtil.getBinding(view); | |
} | |
public BT getBinding() { | |
return mBinding; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.databinding.DataBindingUtil; | |
import android.databinding.ViewDataBinding; | |
import android.support.annotation.LayoutRes; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.ViewGroup; | |
public class BindingHolder<BT extends ViewDataBinding> extends RecyclerView.ViewHolder { | |
private BT mBinding; | |
public BindingHolder(BT binding) { | |
super(binding.getRoot()); | |
mBinding = binding; | |
} | |
public static <BT extends ViewDataBinding> BindingHolder<BT> newInstanceFrom(ViewGroup parent, @LayoutRes int layout) { | |
return new BindingHolder<>( | |
(BT) DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()), | |
layout, parent, false)); | |
} | |
public BT getBinding() { | |
return mBinding; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment