今天咱们是用RecyclerView来实现这个多种Item的加载. 
其实最关键的是要复写RecyclerView的Adapter中的getItemViewType()方法 这个方法就根据条件返回条目的类型 这个MoreTypeBean 是用来传数据的 没必要跟我写的一样, 
其实就是从activity中传到adapter中的数据中必须要有一个type字段来判断这个item对象需要那种视图,然后return出一个标记,在onCreateViewHolder中在引用所对应的item布局.

//重写getItemViewType方法 根据条件返回条目的类型
@Override
public int getItemViewType(int position) { MoreTypeBean moreTypeBean = mData.get(position);
if (moreTypeBean.type == 0) {
return TYPE_PULL_IMAGE;
} else if (moreTypeBean.type == 1) {
return TYPE_RIGHT_IMAGE;
} else {
return TYPE_THREE_IMAGE;
}
}

一般有多少种类型我们定义多少种常量.(我今天写了三种布局 所以我定义了三个)

  //定义三种常量  表示三种条目类型
public static final int TYPE_PULL_IMAGE = 0;
public static final int TYPE_RIGHT_IMAGE = 1;
public static final int TYPE_THREE_IMAGE = 2;

有了上面定义的常量之后 在onCreateViewHolder里根据viewtype来判断 所引用的item布局类型 这样每一条item就会不一样了

    @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//创建不同的 ViewHolder
View view;
//根据viewtype来判断 if (viewType == TYPE_PULL_IMAGE) {
view =View.inflate(parent.getContext(),R.layout.item_pull_img,null);
return new PullImageHolder(view);
} else if (viewType == TYPE_RIGHT_IMAGE) {
view =View.inflate(parent.getContext(),R.layout.item_right_img,null);
return new RightImageHolder(view);
} else {
view =View.inflate(parent.getContext(),R.layout.item_three_img,null);
return new ThreeImageHolder(view);
}
}

创建三种不同的ViewHolder


private class PullImageHolder extends RecyclerView.ViewHolder { public PullImageHolder(View itemView) {
super(itemView);
}
} private class RightImageHolder extends RecyclerView.ViewHolder { public RightImageHolder(View itemView) {
super(itemView);
}
} private class ThreeImageHolder extends RecyclerView.ViewHolder { public ThreeImageHolder(View itemView) {
super(itemView);
}
}

下面把所有的代码都给大家: 
Activity中的代码

public class Recycler_variety_Activity extends Activity {

    private int[] icons = {R.drawable.test, R.drawable.test1, R.drawable.test2, R.drawable.test3, R.drawable.test4, R.drawable.test5, R.drawable.test6};
private RecyclerView mRecy;
private List<MoreTypeBean> mData; @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.recycler_variety_activity);
initView();
initData();
initViewOper();
} private void initView() {
mRecy = (RecyclerView) findViewById(R.id.act_recycler_variety_recycler);
} private void initData() {
mData = new ArrayList<>();
// 随机数 用来标记item界面的类型
Random random = new Random(); for (int i = 0; i < icons.length; i++) {
MoreTypeBean more = new MoreTypeBean(); more.pic = icons[i];
more.type = random.nextInt(3);
mData.add(more);
}
} private void initViewOper() {
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mRecy.setLayoutManager(linearLayoutManager);
Recycler_variety_Adapter adapter = new Recycler_variety_Adapter(mData);
mRecy.setAdapter(adapter);
} }

recyclerview_test_layout的布局就是只有一个RecyclerView

   <android.support.v7.widget.RecyclerView
android:id="@+id/act_recycler_variety_recycler"
android:layout_width="match_parent"
android:background="#d3d3d3"
android:layout_height="match_parent"/>

RecyclerView的Adapter

public class Recycler_variety_Adapter extends RecyclerView.Adapter {

    //定义三种常量  表示三种条目类型
public static final int TYPE_PULL_IMAGE = 0;
public static final int TYPE_RIGHT_IMAGE = 1;
public static final int TYPE_THREE_IMAGE = 2;
private List<MoreTypeBean> mData; public Recycler_variety_Adapter(List<MoreTypeBean> data) {
this.mData = data;
} @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//创建不同的 ViewHolder
View view;
//根据viewtype来创建条目 if (viewType == TYPE_PULL_IMAGE) {
view =View.inflate(parent.getContext(),R.layout.item_pull_img,null);
return new PullImageHolder(view);
} else if (viewType == TYPE_RIGHT_IMAGE) {
view =View.inflate(parent.getContext(),R.layout.item_right_img,null);
return new RightImageHolder(view);
} else {
view =View.inflate(parent.getContext(),R.layout.item_three_img,null);
return new ThreeImageHolder(view);
}
} @Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { } @Override
public int getItemCount() {
if (mData != null) {
return mData.size();
}
return 0;
} //根据条件返回条目的类型
@Override
public int getItemViewType(int position) { MoreTypeBean moreTypeBean = mData.get(position);
if (moreTypeBean.type == 0) {
return TYPE_PULL_IMAGE;
} else if (moreTypeBean.type == 1) {
return TYPE_RIGHT_IMAGE;
} else {
return TYPE_THREE_IMAGE;
} } /**
* 创建三种ViewHolder
*/
private class PullImageHolder extends RecyclerView.ViewHolder { public PullImageHolder(View itemView) {
super(itemView);
}
} private class RightImageHolder extends RecyclerView.ViewHolder { public RightImageHolder(View itemView) {
super(itemView);
}
} private class ThreeImageHolder extends RecyclerView.ViewHolder { public ThreeImageHolder(View itemView) {
super(itemView);
}
}
}

item_pull_img布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:padding="7dp"
android:background="#fff"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型"
android:textSize="16sp"
/>
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@drawable/sucai6"
android:scaleType="fitXY"
/>
</LinearLayout>
<View
android:layout_marginTop="3dp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d3d3d3"
/>
</LinearLayout>

item_right_img布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" > <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal"
android:padding="7dp"> <TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型"
android:textColor="#000"
android:textSize="16sp" /> <ImageView
android:layout_width="120dp"
android:layout_height="90dp"
android:background="@drawable/sucai" />
</LinearLayout>
<View
android:layout_marginTop="3dp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#d3d3d3"
/>
</LinearLayout>

item_three_img布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
> <LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:background="#fff"
android:padding="7sp"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型多种条目类型"
android:textColor="#000"
android:textSize="16sp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <ImageView
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="1"
android:scaleType="fitXY" android:src="@drawable/sucai3" />
<View
android:layout_width="6dp"
android:layout_height="0dp"/> <ImageView
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/sucai4" />
<View
android:layout_width="6dp"
android:layout_height="0dp"/>
<ImageView
android:layout_width="0dp"
android:layout_height="80dp"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="@drawable/sucai5" /> </LinearLayout> </LinearLayout>
<View
android:layout_marginTop="3dp"
android:background="#d3d3d3"
android:layout_width="match_parent"
android:layout_height="1dp"/>
</LinearLayout>

MoreTypeBean

public class MoreTypeBean {
public int type;
public int pic;
}

Android RecyclerView实现加载多种条目类型的更多相关文章

  1. Android图片异步加载之Android-Universal-Image-Loader

    将近一个月没有更新博客了,由于这段时间以来准备毕业论文等各种事务缠身,一直没有时间和精力沉下来继续学习和整理一些东西.最近刚刚恢复到正轨,正好这两天看了下Android上关于图片异步加载的开源项目,就 ...

  2. Android图片异步加载之Android-Universal-Image-Loader(转)

    今天要介绍的是Github上一个使用非常广泛的图片异步加载库Android-Universal-Image-Loader,该项目的功能十分强大,可以说是我见过的目前功能最全.性能最优的图片异步加载解决 ...

  3. 实现Android ListView 自动加载更多内容

    研究了几个小时终于实现了Android ListView 自动加载的效果. 说说我是怎样实现的.分享给大家. 1.给ListView增加一个FooterView,调用addFooterView(foo ...

  4. [Android] Android 用于异步加载 ContentProvider 中的内容的机制 -- Loader 机制 (LoaderManager + CursorLoader + LoaderManager.LoaderCallbacks)

    Android 用于异步加载 ContentProvider 中的内容的机制 -- Loader 机制 (LoaderManager + CursorLoader + LoaderManager.Lo ...

  5. Android 高清加载巨图方案 拒绝压缩图片

    Android 高清加载巨图方案 拒绝压缩图片 转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/49300989: 本文出自:[张 ...

  6. Android 图片异步加载的体会,SoftReference已经不再适用

      在网络上搜索Android图片异步加载的相关文章,目前大部分提到的解决方案,都是采用Map<String, SoftReference<Drawable>>  这样软引用的 ...

  7. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(二)

    接上文: https://www.cnblogs.com/wukong1688/p/10693338.html Android ViewPager 中加载 Fragmenet的两种方式 方式(一) 二 ...

  8. [Android] Android ViewPager 中加载 Fragment的两种方式 方式(一)

    Android ViewPager 中加载 Fragmenet的两种方式 一.当fragment里面的内容较少时,直接 使用fragment xml布局文件填充 文件总数 布局文件:view_one. ...

  9. Android开发 - ImageView加载Base64编码的图片

    在我们开发应用的过程中,并不是所有情况下都请求图片的URL或者加载本地图片,有时我们需要加载Base64编码的图片.这种情况出现在服务端需要动态生成的图片,比如: 二维码 图形验证码 ... 这些应用 ...

随机推荐

  1. python面向对象:组合、封装、property装饰器、多态

    一.组合二.封装三.property装饰器四.多态 一.组合 ''' 1. 什么是组合 一个对象的属性是来自于另外一个类的对象,称之为组合 2. 为何用组合 组合也是用来解决类与类代码冗余的问题 3. ...

  2. idou老师教你学Istio05: 如何用Isito实现智能路由配置

    要介绍istio请求路由,我们不由得先从pilot 和 envoy开始谈起. 在服务网格中,Pilot管理和配置所有的envoy实例.在pilot中,你几乎可以配置所有的关于流量导向规则及其他故障恢复 ...

  3. ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer (最大生成树+LCA求节点距离)

    ACM-ICPC 2018 徐州赛区网络预赛 J. Maze Designer J. Maze Designer After the long vacation, the maze designer ...

  4. JavaScript教程——函数(arguments 对象)

    arguments 对象 定义 由于 JavaScript 允许函数有不定数目的参数,所以需要一种机制,可以在函数体内部读取所有参数.这就是arguments对象的由来. arguments对象包含了 ...

  5. zabbix内存溢出解决方法

    1406:20180802:183248.783 __mem_malloc: skipped 0 asked 48 skip_min 4294967295 skip_max 0 1406:201808 ...

  6. C# 数组(5) 持续更新

    同一类型和不同类型的多个对象 使用同一类型的多个对象,使用集合和数组. 使用不同类型的多个对象,使用Tuple(元组). 初始化数组 ]; myArray 存放在栈中,而 myArray 的内容 放在 ...

  7. np中的温故知新

    1.一维数组中寻找与某个数最近的数 # 一维数组中寻找与某个数最近的数 Z=np.random.uniform(0,1,20) print("随机数组:\n",Z) z=0.5 m ...

  8. shell编程expr表达式----传智播客的书linux编程基础中出现的问题

    首先声明:本人是传智播客的粉丝,拥有他出的多本编程书籍,此文绝无诋毁抹黑之意. 但在linux系统编程第88页给出的while循环范例中,代码运行无法得到预期结果 原代码如下 #!/bin/sh su ...

  9. PHP mysqli_fetch_field_direct() 函数

    返回结果集中某个单一字段(列)的 meta-data,并输出字段名称.表格和最大长度: mysqli_fetch_field_direct(result,fieldnr); 参数 描述 result ...

  10. springbooot+restful目录规则

    dao是访问数据层,dto是数据传出层,po实体类