Android 多布局仿今日头条完美加载多种条目类型(RecyclerView)

这个功能在新闻类的APP经常会用到过,这个效果大家都知道,废话不多说,来看我下实现后的效果图跟别的app是否一样:

实现的效果图:


请先看这篇博客,我是用RecyclerView实现的这些功能

根据上一篇博客稍微改动了一下代码:

1.重写getItemViewType方法 根据条件返回条目的类型

    @Override
    public int getItemViewType(int position) {
    //求末取宇替换成了list集合存储一个标签
        int type = mList.get(position).type;
        if ( type== 0 ) {
            return TYPE_1;
        } else if (type == 1 ) {
            return TYPE_2;
        } else  {
            return TYPE_3;
        }

    }

2.多了一个集合存储Bean类

public class Bean {
    public int type;

}

3.MainActivity 初始化数据的时候,给每一个条目设置一个标签

    private void initList() {
        //随机数 用来标记item界面的类型
        Random random = new Random();

        for (int i = 0; i < 100; i++) {
            int nextInt = random.nextInt(3);
            Bean bean = new Bean();
            //nexInte(3) 随机 生成的0,1,2
            //每循环一次 就给当前位置的条目设置一个标签类型
            bean.type = nextInt;
            mList.add(bean);
            Log.e("随机数",nextInt+"");
        }
    }

代码就不贴了,Github地址已经上传

你可能感兴趣的:(Android)