zhpanvip / BannerViewPager

🚀 An awesome banner view for Android,Based on ViewPager2. 这可能是全网最好用的ViewPager轮播图。简单、高效,一行代码实现循环轮播,一屏三页任意变,指示器样式任你挑。
Apache License 2.0
3.62k stars 439 forks source link

BVP如何使用DataBinding #161

Closed zhpanvip closed 4 years ago

zhpanvip commented 4 years ago

BVP是可以支持使用DataBinding的,需要通过BindingAdapter:

  @BindingAdapter(value = {"binding:adapter", "binding:indicator", "binding:pageClick"}, requireAll = false)
    public static void
    bindBanner(BannerViewPager<BannerBean, BannerViewHolder> banner,
               BannerAdapter adapter, BaseIndicatorView indicatorView,
               BannerViewPager.OnPageClickListener pageClickListener) {
        Resources resources = banner.getContext().getResources();
        banner.setIndicatorSlideMode(IndicatorSlideMode.SCALE)
                .setIndicatorStyle(IndicatorStyle.ROUND_RECT)
                .setAdapter(adapter)
                .setAutoPlay(true)
                .setScrollDuration(700)
                .setIndicatorView(indicatorView)
                .setOnPageClickListener(pageClickListener)
                .setIndicatorSliderColor(resources.getColor(R.color.color_slider_normal),
                        resources.getColor(R.color.colorPrimary))
                .setPageStyle(PageStyle.MULTI_PAGE_SCALE)
                .setIndicatorSliderWidth(
                        resources.getDimensionPixelOffset(R.dimen.dp_4),
                        resources.getDimensionPixelOffset(R.dimen.dp_10))
                .setIndicatorSliderGap(resources.getDimensionPixelOffset(R.dimen.dp_4)).create();
    }

然后在布局文件中通过binding:adapter="@adapter"进行绑定Adapter:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:binding="http://schemas.android.com/tools">

    <data>

        <variable
            name="adapter"
            type="com.zhpan.oneandroid.adapter.BannerAdapter" />

        <variable
            name="pageClick"
            type="com.zhpan.bannerview.BannerViewPager.OnPageClickListener" />

    </data>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.zhpan.bannerview.BannerViewPager
            android:id="@+id/banner_view"
            android:layout_width="match_parent"
            android:layout_height="@dimen/dp_220"
            binding:adapter="@{adapter}"
            binding:indicator="@{indicator}"
            binding:pageClick="@{pageClick}" />
    </RelativeLayout>
</layout>

详细使用可以参考这里