一、使用com.youth.banner报错
报错:llegalStateException: Pages must fill the whole ViewPager2 (use match_parent)
292dd935fbd1b3e3fcbb4583c8e7592.png
二、解决:
1、解决:必须指定view 的宽和高是MATCH_PARENT
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_custom_banner_view, null, false);
//注意,必须设置为match_parent,这个是viewpager2强制要求的
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
view.setLayoutParams(params);
注意,必须设置为match_parent,这个是viewpager2强制要求的.
2、完整代码
/**
* 自定义布局,图片
*/
public class ImageAdapter extends BannerAdapter<BannerBean, ImageHolder> {
MyChangeInterface myChangeInterface;
public ImageAdapter(Context context, List<BannerBean> mDatas) {
//设置数据,也可以调用banner提供的方法,或者自己在adapter中实现
super(mDatas);
}
/**
* 创建ViewHolder,可以用viewType这个字段来区分不同的ViewHolder
*
* @param parent
* @param viewType
* @return
*/
@Override
public ImageHolder onCreateHolder(ViewGroup parent, int viewType) {
// ImageView imageView = new ImageView(parent.getContext());
// //注意,必须设置为match_parent,这个是viewpager2强制要求的
// ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) new ViewGroup.LayoutParams(
// ViewGroup.LayoutParams.MATCH_PARENT,
// ViewGroup.LayoutParams.MATCH_PARENT);
// imageView.setLayoutParams(params);
// //设置左右间距
// imageView.setPadding(15, 0, 15, 0);
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
// return new ImageHolder(imageView);
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_custom_banner_view, null, false);
ViewGroup.LayoutParams params = (ViewGroup.LayoutParams) new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
view.setLayoutParams(params);
return new ImageHolder(view);
}
@Override
public void onBindView(ImageHolder holder, BannerBean data, int position, int size) {
ImageView ivPic = holder.view.findViewById(R.id.iv_pic);
GlideUtils.INSTANCE.loadRoundedRectangAllUrl(holder.view.getContext(), data.getUrl(), ivPic, 6);
}
public interface MyChangeInterface {
public void MyChangeInterface(int position);
}
}
三、com.youth.banner.Banner的下方替换图片的指示器有bug,自己重写了一个
//依赖的版本
implementation 'io.github.youth5201314:banner:2.2.1'
1、指示器的xml
<com.youth.banner.Banner
android:id="@+id/banner"
android:layout_width="match_parent"
android:layout_height="@dimen/ui_dp_308"
android:layout_marginTop="@dimen/ui_dp_20"
app:banner_indicator_normal_color="@android:color/white"
app:banner_indicator_selected_color="@color/colorPrimary"
app:banner_radius="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_banner_title"
app:layout_goneMarginTop="@dimen/ui_dp_40" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_indicator"
android:layout_width="wrap_content"
android:layout_height="@dimen/ui_dp_10"
android:layout_marginTop="@dimen/ui_dp_15"
app:layout_constraintEnd_toEndOf="@+id/banner"
app:layout_constraintStart_toStartOf="@+id/banner"
app:layout_constraintTop_toBottomOf="@+id/banner" />
2、指示器的代码
/**
* 初始化banner
*/
private fun initBanner() {
var url1 = "https://pic.lvmama.com/uploads/pc/place2/2017-11-24/19e96e3c-ba34-4dc4-8b17-62b3aa595cf9.jpg"
var url2 = "https://img0.baidu.com/it/u=3358848204,1936258606&fm=253&fmt=auto&app=120&f=JPEG?w=1421&h=800"
var list = mutableListOf<BannerBean>()
list.add(BannerBean(url1,true))
list.add(BannerBean(url2,false))
list.add(BannerBean(url1,false))
list.add(BannerBean(url2,false))
list.add(BannerBean(url1,false))
mViewBind.banner.setAdapter(ImageAdapter(context, list))
//添加魅族效果
mViewBind.banner.setBannerGalleryMZ(70)
mViewBind.banner.setOnBannerListener { any, position ->
MyToash.Log("lyy", "-----点击事件:" + position)
}
//banner的事件监听
mViewBind.banner.addOnPageChangeListener(object : OnPageChangeListener {
override fun onPageScrolled(
position: Int,
positionOffset: Float,
positionOffsetPixels: Int
) {
}
override fun onPageSelected(position: Int) {
//更新下方指示器的状态
list?.forEach {
it.isSelect = false
}
val bean = list.get(position)
bean.isSelect = true
//更新下方指示器
indicatorAdapter.notifyDataSetChanged()
}
override fun onPageScrollStateChanged(state: Int) {
//MyToash.Log("lyy", "-----onPageScrollStateChanged:" + state)
}
})
//指示器的adapter
mViewBind.rvIndicator.apply {
var linearLayoutManager = LinearLayoutManager(context)
linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
layoutManager = linearLayoutManager
adapter = indicatorAdapter
}
indicatorAdapter.setList(list)
}
3、IndicatorAdapter的代码:
class IndicatorAdapter:BaseQuickAdapter<BannerBean,BaseDataBindingHolder<ItemIndicatorBinding>>(R.layout.item_indicator) {
override fun convert(holder: BaseDataBindingHolder<ItemIndicatorBinding>, item: BannerBean) {
holder.dataBinding?.apply{
if (item.isSelect){
ivDot.setImageResource(R.drawable.white_radius_new)
}else{
ivDot.setImageResource(R.drawable.gray_radius_new)
}
}
}
}