RecycleView混合布局

本来想把公司的UI图放上来,考虑到版权等未知因素,就拿网上的图来说了:

类似的这种布局,有的一行只有一张图片,有的一行有两个元素,有个一行有三个元素。。就是混合的布局方式

参考文献:

https://github.com/CymChad/BaseRecyclerViewAdapterHelper/wiki/%E8%87%AA%E5%AE%9A%E4%B9%89%E4%B8%8D%E5%90%8C%E7%9A%84item%E7%B1%BB%E5%9E%8B

http://blog.csdn.net/walterzhoukick/article/details/52687957

原理:

BaseRecyclerViewAdapterHelper是封装好的Adapter框架,使用网格布局,每行所占格子数是1/2/3的最小公倍数,如果一张图占6格,就是一行,如果一张图占2格,就是一行3张图,如果一张图占3格,就是一行两张图;

代码:(未完成,待续)

public class First3Fragment extends BaseFragment {

    @BindView(R.id.search)
    ImageView search;
    @BindView(R.id.recyclerView)
    XRecyclerView recyclerView;

    private Fp3Adapter adapter;
    private List<String> list;

    @Override
    public int getContentId() {
        return R.layout.fragment_first3;
    }

    @Override
    public void initData() {
        super.initData();
        list = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            list.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1492579882060&di=1c2b75e87149909884f75ad013ceb4c0&imgtype=0&src=http%3A%2F%2Fimg.sj33.cn%2Fuploads%2Fallimg%2F201302%2F1-130201105204.jpg");
        }
        recyclerView.setLayoutManager(new GridLayoutManager(recyclerView.getContext(), 6, GridLayoutManager.VERTICAL, false));
        recyclerView.setAdapter(adapter = new Fp3Adapter(list, getActivity()));
    }
}
public class Fp3Adapter extends BaseMultiItemQuickAdapter {

    private Context context;
    private List<String> list;

    public static final int TYPE_BANNER = 0xff01;//banner
    public static final int TYPE_TOP_ENTER = 0xff02;//顶部导航
    public static final int TYPE_TODAY_LIVE = 0xff03;//一张图片
    public static final int TYPE_TITLE = 0xff04;//标题
    public static final int TYPE_HOT_POINT = 0xff05;//热点培训item
    public static final int TYPE_ASK_ADVICE = 0xff06;//问道求教item
    public static final int TYPE_MORE_CHANGE = 0xff07;//查看更多和换一批
    public static final int TYPE_MIX_LIGHT_COURSE = 0xff08;//混学课和轻学课item
    public static final int TYPE_MIN_COURSE = 0xff09;//微训课item
    public static final int TYPE_SOLUTION = 0xff0a;//解决方案item
    public static final int TYPE_CLASSIC_REVIEW = 0xff0b;//经典回顾item
    public static final int TYPE_SALON_ACTIVE = 0xff0c;//沙龙活动item
    public static final int TYPE_CLASSIC_TOOL = 0xff0d;//经典工具item
    public static final int TYPE_BUTTOM_ADS = 0xff0e;//底部广告

    public Fp3Adapter(List list, Context context) {
        super(list);
        this.context = context;
        addItemLayout();
    }

    @Override
    protected int getDefItemViewType(int position) {
        if (position == 0) {
            return TYPE_BANNER;
        } else if (position == 1) {
            return TYPE_TOP_ENTER;
        } else if (position == 2) {
            return TYPE_TODAY_LIVE;
        } else if (position == 3 || position == 9 || position == 14 || position == 18 || position == 22 || position == 26 || position == 31 || position == 34 || position == 38) {
            return TYPE_TITLE;
        } else if (position >= 4 && position <= 8) {
            return TYPE_HOT_POINT;
        } else if (position >= 10 && position <= 12) {
            return TYPE_ASK_ADVICE;
        } else if (position == 13 || position == 17 || position == 21 || position == 25 || position == 30 || position == 37) {
            return TYPE_MORE_CHANGE;
        } else if ((position >= 15 && position <= 16) || (position >= 19 && position <= 20)) {
            return TYPE_MIX_LIGHT_COURSE;
        } else if (position >= 23 && position <= 24) {
            return TYPE_MIN_COURSE;
        } else if (position >= 27 && position <= 29) {
            return TYPE_SOLUTION;
        } else if (position >= 32 && position <= 33) {
            return TYPE_CLASSIC_REVIEW;
        } else if (position >= 35 && position <= 36) {
            return TYPE_SALON_ACTIVE;
        } else if (position >= 39 && position <= 41) {
            return TYPE_CLASSIC_TOOL;
        } else {
            return TYPE_BUTTOM_ADS;
        }
    }

    @Override
    protected void convert(BaseViewHolder holder, MultiItemEntity item) {
        switch (holder.getItemViewType()) {
            case TYPE_BANNER:
                setBanner(holder);
                break;
            case TYPE_TOP_ENTER:
                setTopEnter(holder);
                break;
            case TYPE_TODAY_LIVE:
                setTodayLive(holder, (MultipleItem) item);
                break;
            case TYPE_TITLE:
                setTitle(holder);
                break;
            case TYPE_HOT_POINT:
                setHotPoint(holder, (MultipleItem) item);
                break;
            case TYPE_ASK_ADVICE:
                setAskAdvice(holder, (MultipleItem) item);
                break;
            case TYPE_MORE_CHANGE:
                setMoreChange(holder);
                break;
            case TYPE_MIX_LIGHT_COURSE:
                break;
            case TYPE_MIN_COURSE:
                break;
            case TYPE_SOLUTION:
                break;
            case TYPE_CLASSIC_REVIEW:
                break;
            case TYPE_SALON_ACTIVE:
                break;
            case TYPE_CLASSIC_TOOL:
                break;
            case TYPE_BUTTOM_ADS:
                break;
        }
    }

    /**
     * 更多和换一批
     */
    private void setMoreChange(BaseViewHolder holder) {
        final int position = holder.getAdapterPosition();
        holder.setOnClickListener(R.id.more, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (position) {
                    case 13:
                        break;
                    case 17:
                        break;
                    case 21:
                        break;
                    case 25:
                        break;
                    case 30:
                        break;
                    case 37:
                        break;
                }
            }
        });
        holder.setOnClickListener(R.id.change, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (position) {
                    case 13:
                        break;
                    case 17:
                        break;
                    case 21:
                        break;
                    case 25:
                        break;
                    case 30:
                        break;
                    case 37:
                        break;
                }
            }
        });

    }

    /**
     * 问道求教
     */
    private void setAskAdvice(BaseViewHolder holder, MultipleItem item) {
        holder.setOnClickListener(R.id.ask_layout, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    /**
     * 培训热点
     */
    private void setHotPoint(BaseViewHolder holder, MultipleItem item) {
        holder.setText(R.id.title, "");// TODO: 2017/4/21

    }

    /**
     * 标题
     */
    private void setTitle(BaseViewHolder holder) {
        int position = holder.getAdapterPosition();
        switch (position) {
            case 3:
                holder.setImageResource(R.id.title, R.drawable.index_icon_title_news);
                holder.setImageResource(R.id.operate, R.drawable.index_btn_more);
                holder.setVisible(R.id.operate, true);
                holder.setOnClickListener(R.id.operate, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                break;
            case 9:
                holder.setImageResource(R.id.title, R.drawable.index_icon_title_wendao);
                holder.setVisible(R.id.operate, false);
                break;
            case 14:
                holder.setImageResource(R.id.title, R.drawable.index_icon_title_hunxue);
                holder.setVisible(R.id.operate, false);
                break;
            case 18:
                holder.setImageResource(R.id.title, R.drawable.index_icon_title_qinxue);
                holder.setVisible(R.id.operate, false);
                break;
            case 22:
                holder.setImageResource(R.id.title, R.drawable.index_icon_title_weixun);
                holder.setVisible(R.id.operate, false);
                break;
            case 26:
                holder.setImageResource(R.id.title, R.drawable.index_icon_title_solution);
                holder.setVisible(R.id.operate, false);
                break;
            case 31:
                holder.setImageResource(R.id.title, R.drawable.index_icon_title_classic);
                holder.setVisible(R.id.operate, false);
                break;
            case 34:
                holder.setImageResource(R.id.title, R.drawable.index_icon_title_salon);
                holder.setVisible(R.id.operate, false);
                break;
            case 38:
                holder.setImageResource(R.id.title, R.drawable.index_icon_title_tool);
                holder.setImageResource(R.id.operate, R.drawable.index_btn_change);
                holder.setVisible(R.id.operate, true);
                holder.setOnClickListener(R.id.operate, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
                break;
        }
    }

    /**
     * 今日直播
     */
    private void setTodayLive(BaseViewHolder holder, MultipleItem item) {
        Picasso.with(context).load(item.getUrl()).into((ImageView) holder.getView(R.id.pic));
    }

    /**
     * 顶部导航
     */
    private void setTopEnter(BaseViewHolder holder) {
        holder.setOnClickListener(R.id.hr, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        holder.setOnClickListener(R.id.leaderShip, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        holder.setOnClickListener(R.id.marketing, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        holder.setOnClickListener(R.id.finance, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        holder.setOnClickListener(R.id.all, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    /**
     * 轮播图
     */
    private void setBanner(BaseViewHolder holder) {
        List<String> images = initImages();
        Banner banner = holder.getView(R.id.banner);
        //设置banner样式
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);
        //设置图片加载器
        banner.setImageLoader(new GlideImageLoader());
        //设置图片集合
        banner.setImages(images);
        //设置banner动画效果
        banner.setBannerAnimation(Transformer.ZoomOutSlide);
        //设置自动轮播,默认为true
        banner.isAutoPlay(true);
        //设置轮播时间
        banner.setDelayTime(1500);
        banner.setOnBannerClickListener(new OnBannerClickListener() {
            @Override
            public void OnBannerClick(int position) {
            }
        });
        //设置指示器位置(当banner模式中有指示器时)
        banner.setIndicatorGravity(BannerConfig.CENTER);
        //banner设置方法全部调用完毕时最后调用
        banner.start();
    }

    private List<String> initImages() {
        List<String> images = new ArrayList<>();
        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1492579882061&di=8cb3791dc56819697520bd12a10e248a&imgtype=0&src=http%3A%2F%2Fs8.sinaimg.cn%2Flarge%2F001fLzaQzy7aa4u3tSR85");
        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1492579882060&di=1c2b75e87149909884f75ad013ceb4c0&imgtype=0&src=http%3A%2F%2Fimg.sj33.cn%2Fuploads%2Fallimg%2F201302%2F1-130201105204.jpg");
        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1492579882058&di=33baccb62904b5f556770bf90ec9a1c8&imgtype=0&src=http%3A%2F%2Fimg.sj33.cn%2Fuploads%2Fallimg%2F201302%2F1-130201105055.jpg");
        images.add("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1492579882054&di=cfe48b38f5c1ead1f8e9a3c9ccaa5299&imgtype=0&src=http%3A%2F%2Fpic.58pic.com%2F58pic%2F15%2F39%2F85%2F74C58PICgy4_1024.jpg");
        return images;
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        final RecyclerView.LayoutManager manager = recyclerView.getLayoutManager();
        if (manager instanceof GridLayoutManager) {
            final GridLayoutManager gridManager = (GridLayoutManager) manager;
            gridManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {

                @Override
                public int getSpanSize(int position) {
                    int type = getItemViewType(position);
                    switch (type) {
                        case TYPE_BANNER:
                        case TYPE_TOP_ENTER:
                        case TYPE_TODAY_LIVE:
                        case TYPE_TITLE:
                        case TYPE_HOT_POINT:
                        case TYPE_MIX_LIGHT_COURSE:
                        case TYPE_MORE_CHANGE:
                        case TYPE_MIN_COURSE:
                        case TYPE_SALON_ACTIVE:
                            return gridManager.getSpanCount();
                        case TYPE_ASK_ADVICE:
                        case TYPE_SOLUTION:
                        case TYPE_CLASSIC_TOOL:
                            return 2;
                        case TYPE_CLASSIC_REVIEW:
                        case TYPE_BUTTOM_ADS:
                            return 3;
                        default:
                            return 6;
                    }
                }
            });
        }
    }

    /**
     * 绑定不同布局
     */
    private void addItemLayout() {
        addItemType(TYPE_BANNER, R.layout.fp3_banner);
        addItemType(TYPE_TOP_ENTER, R.layout.fp3_top_enters);
        addItemType(TYPE_TODAY_LIVE, R.layout.fp3_image);
        addItemType(TYPE_TITLE, R.layout.fp3_title);
        addItemType(TYPE_HOT_POINT, R.layout.fp3_hot_point_item);
        addItemType(TYPE_ASK_ADVICE, R.layout.fp3_ask_item);
        addItemType(TYPE_MORE_CHANGE, R.layout.fp3_more_change);
        addItemType(TYPE_MIX_LIGHT_COURSE, R.layout.fp3_mix_light_course_item);
        addItemType(TYPE_MIN_COURSE, R.layout.fp3_min_course_item);
        addItemType(TYPE_SOLUTION, R.layout.fp3_solution_item);
        addItemType(TYPE_CLASSIC_REVIEW, R.layout.fp3_classic_review_item);
        addItemType(TYPE_SALON_ACTIVE, R.layout.fp3_salon_item);
        addItemType(TYPE_CLASSIC_TOOL, R.layout.fp3_classic_tool_item);
        addItemType(TYPE_BUTTOM_ADS, R.layout.fp3_image);
    }
}
时间: 2024-10-15 05:18:59

RecycleView混合布局的相关文章

慕课笔记利用css进行布局【混合布局】

<html> <head> <title>混合布局学习</title> <style type="text/css"> body{margin:0;padding:0;text-align:center} .top{height:50px;background:#00f;margin:0 auto} .head{height:50px;width:700px;background:#f96;margin:0 auto} .ma

Android RecycleView自定义布局的使用

自定义布局的RecycleView需要自己实现Adapter,ViewHolder和布局: 自定义Adapter继承RecycleView.Adapter,重写getItemCount(),onBindViewHolder()和onCreateViewHolder(): 自定义ViewHolder继承于RecycleView.ViewHolder: getItemCount()返回RecycleView中Item的个数,onBindViewHolder()主要实现数据和布局的绑定,onCreat

慕课笔记利用css进行布局【混合布局练习】

通过学习div的布局,以一个简单的内容管理网站的布局为例子,用div+css进行简单的网页布局,加深学印象: <html> <head> <title>CSS+div布局学习</title> <style type="text/css"> body{margin:0;padding:0;text-align:center} .head{height:230px;width:960px;background:#f96;margi

Android Studio第三十二期 - RecycleView不同布局适配器写法

新需求,如图,代码已经整理好,效果如下图: 单个: 多个: demo效果图: 地址:https://github.com/geeklx/MyApplication/tree/master/p010_recycleviewall/src/main/java/com/example/p010_recycleviewall/recycleviewdifferentitem

Android开发游记:RecycleView 实现复杂首页布局三种方式

做过电商类应用的朋友可能都会遇到一个比较头疼的问题:复杂的首页布局如何实现.参考百度糯米,美团,bilibili等应用,都会发现其首页的布局相对复杂,例如下图bilibili的首页(第二张是demo实现的效果图),可以看到在同一个页面中先是有列表布局出现,然后出现了2列的网格布局,接着3列的网格布局,最后还出现了瀑布流式布局: 这样的效果该怎么做呢?是使用LinearLayoutManager.GridLayoutManager还是StaggeredGridLayoutManager?还是根本不

响应式布局(收藏)

在谈响应式布局前,我们先梳理下网页设计中整体页面排版布局,常见的主要有如下几种类型: 布局类型 布局实现 采用何种方式实现布局设计,也有不同的方式,这里基于页面的实现单位而言,分为四种类型:固定布局.可切换的固定布局.弹性布局.混合布局. 固定布局:以像素作为页面的基本单位,不管设备屏幕及浏览器宽度,只设计一套尺寸: 可切换的固定布局:同样以像素作为页面单位,参考主流设备尺寸,设计几套不同宽度的布局.通过设别的屏幕尺寸或浏览器宽度,选择最合适的那套宽度布局: 弹性布局:以百分比作为页面的基本单位

关于响应式布局

在谈响应式布局前,我们先梳理下网页设计中整体页面排版布局,常见的主要有如下几种类型: 布局类型 布局实现 采用何种方式实现布局设计,也有不同的方式,这里基于页面的实现单位而言,分为四种类型:固定布局.可切换的固定布局.弹性布局.混合布局. 固定布局:以像素作为页面的基本单位,不管设备屏幕及浏览器宽度,只设计一套尺寸: 可切换的固定布局:同样以像素作为页面单位,参考主流设备尺寸,设计几套不同宽度的布局.通过设别的屏幕尺寸或浏览器宽度,选择最合适的那套宽度布局: 弹性布局:以百分比作为页面的基本单位

CSS对 网页进行布局

一.单列布局:  margin:0 auto:将div模块居中 二.两列布局:(float标签将div进行浮动)   float:left; float:right; 三.三列布局:  ①.利用float直接对三个div模块进行浮动: ②.左右两边div固定,中间自适应(利用position:absolute;上 右 下 左)绝对定位来进行固定       左边:position:absolute;left:0:top: 0; 右边:position:absolute;right:0:top:

Android基础_2 Activity线性布局和表格布局

在activity的布局中,线性布局和表格布局是最简单的,这次分别从线性布局,表格布局以及线性布局和表格混合布局做了实验,实验中只需要编写 相应的xml的代码,java代码不需要更改,因为我们这里只是练习android的界面设计.参考的资料为mars老师的教程. 线性布局: 线性布局就是将各种控件按照行或者列依次进行排列. 其中本实验用到的各控件的属性解释如下: android:layout_weight属性是指不同的控件在activity中占有体积大小的比例. android:paddingL