<Button
android:id="@+id/onboard_next_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@color/white"
android:text="@string/next"
android:onClick="@{vm::onButtonNext}"
android:background="@color/customColor"
android:layout_alignParentBottom="true"/>
I am implementing a viewpager page change logic on a button. Which is super easy if it was standard MVC. All I have to do is to write something like
@Override
public void onButtonNext(View view) {
// viewPager does not exist in my onButtonNext function because this is the viewModel
viewPager.setCurrentItem(viewPager.getCurrentItem() + 1, true);
}
because viewPager instance exists in my fragment or activity.
However, because I am implementing this in MVVM, I have no access to viewPager. How do I implement a viewPager.setCurrentItem in my viewModel if I do not have access to viewPager instance?
However, because I am implementing this in MVVM, I have no access to viewPager. How do I implement a viewPager.setCurrentItem in my viewModel if I do not have access to viewPager instance?
you need a public static void
method annotated with @BindingAdapter.
Eg
@BindingAdapter("currentItem")
public static void setCurrentItem(ViewPager viewPager, int item) {
}
and in the layout with will use something like app:currentItem
. Of course your VM has to provide the int
property or a getter for the item. Android takes care of the rest. You can read more about BindingAdapter
here
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.