Skip to content

Instantly share code, notes, and snippets.

@zmdominguez
Created November 9, 2017 02:12
Show Gist options
  • Save zmdominguez/c809ee4ee8d8b45ea5faa3fee7cb9141 to your computer and use it in GitHub Desktop.
Save zmdominguez/c809ee4ee8d8b45ea5faa3fee7cb9141 to your computer and use it in GitHub Desktop.
ViewPager OnPageChangeListener Databinding
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="currentItemSelectedListener" type="id" />
</resources>
import android.databinding.BindingAdapter;
import android.databinding.InverseBindingAdapter;
import android.databinding.InverseBindingListener;
import android.databinding.InverseBindingMethod;
import android.databinding.InverseBindingMethods;
import android.databinding.adapters.ListenerUtil;
import android.support.v4.view.ViewPager;
import com.zdominguez.sdksandbox.R;
@InverseBindingMethods({
@InverseBindingMethod(type = ViewPager.class,
attribute = "currentItem",
event = "currentItemAttrChanged",
method = "getCurrentItem")
})
public class ViewPagerBindingAdapter {
@InverseBindingAdapter(attribute = "currentItem", event = "currentItemAttrChanged")
public static int getCurrentItem(ViewPager view) {
return view.getCurrentItem();
}
@BindingAdapter(value = {"onPageScrolled", "onPageSelected",
"onPageScrollStateChanged", "currentItemAttrChanged"},
requireAll = false)
public static void updateCurrentItem(ViewPager view, final OnPageScrolledListener onPageScrolledListener,
final OnPageSelectedListener onPageSelectedListener,
final OnPageScrollStateChangedListener onPageScrollStateChangedListener,
final InverseBindingListener currentItemAttrChangedListener) {
ViewPager.OnPageChangeListener onPageChangeListener;
if (onPageScrolledListener == null && onPageSelectedListener == null && onPageScrollStateChangedListener == null
&& currentItemAttrChangedListener == null) {
onPageChangeListener = null;
} else {
onPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
if (onPageScrolledListener != null) {
onPageScrolledListener.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
}
@Override
public void onPageSelected(int position) {
if (onPageSelectedListener != null) {
onPageSelectedListener.onPageSelected(position);
}
if (currentItemAttrChangedListener != null) {
currentItemAttrChangedListener.onChange();
}
}
@Override
public void onPageScrollStateChanged(int state) {
if (onPageScrollStateChangedListener != null) {
onPageScrollStateChangedListener.onPageScrollStateChanged(state);
}
}
};
}
final ViewPager.OnPageChangeListener oldValue = ListenerUtil.trackListener(view,
onPageChangeListener, R.id.currentItemSelectedListener);
if (oldValue != null) {
view.removeOnPageChangeListener(oldValue);
}
if (onPageChangeListener != null) {
view.addOnPageChangeListener(onPageChangeListener);
}
}
public interface OnPageScrolledListener {
void onPageScrolled(int position, float positionOffset, int positionOffsetPixels);
}
public interface OnPageSelectedListener {
void onPageSelected(int position);
}
public interface OnPageScrollStateChangedListener {
void onPageScrollStateChanged(int state);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment