-
-
Notifications
You must be signed in to change notification settings - Fork 439
01.Quick Starts
zhangpan edited this page Jun 26, 2022
·
18 revisions
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}'
<com.zhpan.bannerview.BannerViewPager
android:id="@+id/banner_view"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="160dp" />
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>
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;
}
}
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)
}
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();
}
you must add proguard rules,If you have called setScrollDuration method in your project:
-keep class androidx.recyclerview.widget.**{*;}
-keep class androidx.viewpager2.widget.**{*;}