Skip to content

01.Quick Starts

zhangpan edited this page Jun 26, 2022 · 18 revisions

(1)Gradle dependency

Add it in your root build.gradle at the end of repositories:

allprojects {
    repositories {
	...
	maven { url 'https://www.jitpack.io' }
    }
}

Then Add the dependency

implementation 'com.github.zhpanvip:bannerviewpager:{$latestVersion}'

latestVersion:latestVersion

(2)Add BannerViewPager in layout.xml

    <com.zhpan.bannerview.BannerViewPager
            android:id="@+id/banner_view"
            android:layout_width="match_parent"
            android:layout_margin="10dp"
            android:layout_height="160dp" />

(3)The item layout of banner:

Attention:The layout width and height required "match_parent".Otherwise,the ViewPager2 will throws a IllegalStateException.

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/banner_image"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="center_vertical">

            <TextView
                android:id="@+id/tv_describe"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="15dp"
                android:gravity="center_vertical"
                android:paddingTop="5dp"
                android:paddingBottom="5dp"
                android:textColor="#FFFFFF"
                android:textSize="16sp" />
        </LinearLayout>

    </RelativeLayout>

(4)Extends BaseBannerAdapter,and override methods

class SimpleAdapter : BaseBannerAdapter<CustomBean>() {

    override fun bindData(holder: BaseViewHolder<CustomBean>, data: CustomBean?, position: Int, pageSize: Int) {
        val imageStart: ImageView = holder.findViewById(R.id.iv_logo)
        holder.setImageResource(R.id.banner_image, data!!.imageRes)
    }

    override fun getLayoutId(viewType: Int): Int {
        return R.layout.item_custom_view;
    }
}

(5)Create BannerViewPager in Activity or Fragment:

You can call create() method with no parameter,If fetching data asynchronously(for example,The data is from remote server or database):

    private lateinit var mViewPager: BannerViewPager<CustomBean>
    ...

    private fun setupViewPager() {
            mViewPager = findViewById(R.id.banner_view)
            mViewPager.apply {
                adapter = SimpleAdapter()
                setLifecycleRegistry(lifecycle)
            }.create()
        }

While fetch data successfully,just need call refreshData() method to refresh:

    mViewPager.refreshData(data)

If fetching data synchronously,you can call create(List) method with parameters.

     private lateinit var mViewPager: BannerViewPager<CustomBean>
     ...

     private fun setupViewPager() {
             mViewPager = findViewById(R.id.banner_view)
             mViewPager.apply {
                 adapter = SimpleAdapter()
                 setLifecycleRegistry(lifecycle)
             }.create(data)
         }

(6)startLoop and stopLoop

Use setLifecycleRegistry(Lifecycle) method to instead of call stopLoop and startLoop in Activity or Fragment

    mViewPager.setLifecycleRegistry(getLifecycle())

    // setLifecycleRegistry Equals the follwoing code.

    @Override
    protected void onPause() {
        if (mViewPager != null)
                mViewPager.stopLoop();
        super.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mViewPager != null)
            mViewPager.startLoop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mViewPager != null)
            mViewPager.stopLoop();
    }

(7)Proguard

you must add proguard rules,If you have called setScrollDuration method in your project:

    -keep class androidx.recyclerview.widget.**{*;}
    -keep class androidx.viewpager2.widget.**{*;}