您当前的位置:首页 > 例子源码

例子源码- Android复杂首页布局RecyclerViwe多类型Item

时间:2022-02-04 14:20:05 阅读数:56,005人阅读
版权声明:转载请注明出处,谢谢!
—— 世界上那些最容易的事情中,拖延时间最不费力。

效果说明

在开发过程中,页面中有列表很常见,列表上下一般都还有别的元素,比如最上面是一个轮播图,中间的金刚按钮区,下面是横向滑动列表,再下面是列表效果。如果让你实现上面的效果,你会怎样布局?是不是ViewPager+LinearLayout+RecyclerView+RecyclerView,当然这样也可以,但是会出现很多滑动冲突性的问题。这里我们只用一个RecyclerView解决,效果如下图所示:

实现原理

使用开源库BaseRecyclerViewAdapterHelper,主要代码如下:

			public MultipleItemQuickAdapter(List data) {
				super(data);
				addItemType(QuickMultipleEntity.BANNER, R.layout.banner_layout);
				addItemType(QuickMultipleEntity.TITLE, R.layout.title_layout);
				addItemType(QuickMultipleEntity.IMG_TEXT_RECT_H, R.layout.image_text_rect_h_layout);
				addItemType(QuickMultipleEntity.IMG_TEXT_RECT_V, R.layout.image_text_rect_v_layout);
				resId = new int[]{R.drawable.a0, R.drawable.a1, R.drawable.a2, R.drawable.a3, R.drawable.a4, R.drawable.a5};
			}
			
			@Override
			protected void convert(BaseViewHolder baseViewHolder, QuickMultipleEntity o) {
				switch (baseViewHolder.getItemViewType()) {
					case QuickMultipleEntity.BANNER:
						List _bannerStringList = new ArrayList<>();
						_bannerStringList.add("0");
						_bannerStringList.add("1");
						_bannerStringList.add("2");
						com.youth.banner.Banner banner = baseViewHolder.getView(R.id.banner);
						banner.setAdapter(new BannerImageAdapter(_bannerStringList) {
							@Override
							public void onBindView(com.youth.banner.holder.BannerImageHolder holder, String data, int position, int size) {
								holder.imageView.setImageResource(R.drawable.b);
							}
						}).setIndicator(new CircleIndicator(baseViewHolder.itemView.getContext()));
						break;
					case QuickMultipleEntity.TITLE:
						TextView textView = baseViewHolder.getView(R.id.tv_title);
						textView.getPaint().setFakeBoldText(true);
						break;
					case QuickMultipleEntity.IMG_TEXT_RECT_H:
						ImageView imageView = baseViewHolder.getView(R.id.img_cover);
						RequestOptions options = new RequestOptions()
								.transform(new MultiTransformation<>(new RoundedCorners(12)));
						Glide.with(imageView).load(resId[new Random().nextInt(6)]).apply(options).into(imageView);
						break;
					case QuickMultipleEntity.IMG_TEXT_RECT_V:
						ImageView imageView1 = baseViewHolder.getView(R.id.img_cover);
						RequestOptions options1 = new RequestOptions()
								.transform(new  MultiTransformation<>(new RoundedCorners(24)));
						Glide.with(imageView1).load(resId[new Random().nextInt(6)]).apply(options1).into(imageView1);
						break;
					default:
						break;
				}
			}
	  
	    entityList = new ArrayList<>();
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.BANNER, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.TITLE, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_H, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_H, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_H, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.TITLE, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_V, 1));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_V, 1));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_V, 1));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.TITLE, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_H, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_H, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_H, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.TITLE, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_V, 1));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_V, 1));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_V, 1));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.TITLE, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_H, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_H, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_H, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.TITLE, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_V, 1));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_V, 1));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_V, 1));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.TITLE, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_H, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_H, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_H, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.TITLE, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_V, 1));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_V, 1));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_V, 1));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.TITLE, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_H, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_H, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_H, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.TITLE, 3));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_V, 1));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_V, 1));
        entityList.add(new QuickMultipleEntity(QuickMultipleEntity.IMG_TEXT_RECT_V, 1));

        multipleItemQuickAdapter = new MultipleItemQuickAdapter(entityList);
        final GridLayoutManager _manager = new GridLayoutManager(this, 3);
        viewBinding.recyclerviewMain.setLayoutManager(_manager);
        multipleItemQuickAdapter.setGridSpanSizeLookup(new GridSpanSizeLookup() {
            @Override
            public int getSpanSize(GridLayoutManager gridLayoutManager, int viewType, int position) {
                return entityList.get(position).getSpanSize();
            }
        });
        viewBinding.recyclerviewMain.setAdapter(multipleItemQuickAdapter);
	  

源码下载地址

------转载请注明出处,感谢您对原创作者的支持------

有偿提供技术支持、Bug修复、项目外包、毕业设计、大小作业

Android学习小站

Q Q:1095817610

微信:jx-helu

邮箱:1095817610@qq.com

添加请备注"Android学习小站"