Skip to content

03.Multiple View Types

zhangpan edited this page Feb 27, 2022 · 5 revisions

(1)Eextends BaseBannerAdapter,Override getViewType method.

public class MultiViewTypesAdapter extends BaseBannerAdapter<BannerData> {

    @Override
    protected void bindData(BaseViewHolder<BannerData> holder, BannerData data, int position, int pageSize) {
        if (getViewType(position) == BannerData.TYPE_NEW) {
            holder.setImageResource(R.id.image_view, data.getDrawable());
        } else {
            ImageView imageView = holder.findViewById(R.id.banner_image);
            Glide.with(imageView).load(data.getImagePath()).placeholder(R.drawable.placeholder).into(imageView);
        }
    }

    @Override
    public int getLayoutId(int viewType) {
        if (viewType == BannerData.TYPE_NEW) {
            return R.layout.item_new_type;
        }
        return R.layout.item_net_image;
    }

    @Override
    public int getViewType(int position) {
        return mList.get(position).getType();
    }
}

(2)Create BannerViewPager,and set adapter.

       mViewPager
             .setLifecycleRegistry(getLifecycle())
             .setAdapter(new MultiViewTypesAdapter())
             .create(dataList);