Created
November 9, 2017 02:12
-
-
Save zmdominguez/c809ee4ee8d8b45ea5faa3fee7cb9141 to your computer and use it in GitHub Desktop.
ViewPager OnPageChangeListener Databinding
This file contains 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
<?xml version="1.0" encoding="utf-8"?> | |
<resources> | |
<item name="currentItemSelectedListener" type="id" /> | |
</resources> |
This file contains 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.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