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

  1. //重写getItemViewType方法 根据条件返回条目的类型
  2. @Override
  3. public int getItemViewType(int position) {
  4. MoreTypeBean moreTypeBean = mData.get(position);
  5. if (moreTypeBean.type == 0) {
  6. return TYPE_PULL_IMAGE;
  7. } else if (moreTypeBean.type == 1) {
  8. return TYPE_RIGHT_IMAGE;
  9. } else {
  10. return TYPE_THREE_IMAGE;
  11. }
  12. }

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

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

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

  1. @Override
  2. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  3. //创建不同的 ViewHolder
  4. View view;
  5. //根据viewtype来判断
  6. if (viewType == TYPE_PULL_IMAGE) {
  7. view =View.inflate(parent.getContext(),R.layout.item_pull_img,null);
  8. return new PullImageHolder(view);
  9. } else if (viewType == TYPE_RIGHT_IMAGE) {
  10. view =View.inflate(parent.getContext(),R.layout.item_right_img,null);
  11. return new RightImageHolder(view);
  12. } else {
  13. view =View.inflate(parent.getContext(),R.layout.item_three_img,null);
  14. return new ThreeImageHolder(view);
  15. }
  16. }

创建三种不同的ViewHolder


  1. private class PullImageHolder extends RecyclerView.ViewHolder {
  2. public PullImageHolder(View itemView) {
  3. super(itemView);
  4. }
  5. }
  6. private class RightImageHolder extends RecyclerView.ViewHolder {
  7. public RightImageHolder(View itemView) {
  8. super(itemView);
  9. }
  10. }
  11. private class ThreeImageHolder extends RecyclerView.ViewHolder {
  12. public ThreeImageHolder(View itemView) {
  13. super(itemView);
  14. }
  15. }

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

  1. public class Recycler_variety_Activity extends Activity {
  2. private int[] icons = {R.drawable.test, R.drawable.test1, R.drawable.test2, R.drawable.test3, R.drawable.test4, R.drawable.test5, R.drawable.test6};
  3. private RecyclerView mRecy;
  4. private List<MoreTypeBean> mData;
  5. @Override
  6. protected void onCreate(@Nullable Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.recycler_variety_activity);
  9. initView();
  10. initData();
  11. initViewOper();
  12. }
  13. private void initView() {
  14. mRecy = (RecyclerView) findViewById(R.id.act_recycler_variety_recycler);
  15. }
  16. private void initData() {
  17. mData = new ArrayList<>();
  18. // 随机数 用来标记item界面的类型
  19. Random random = new Random();
  20. for (int i = 0; i < icons.length; i++) {
  21. MoreTypeBean more = new MoreTypeBean();
  22. more.pic = icons[i];
  23. more.type = random.nextInt(3);
  24. mData.add(more);
  25. }
  26. }
  27. private void initViewOper() {
  28. LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
  29. mRecy.setLayoutManager(linearLayoutManager);
  30. Recycler_variety_Adapter adapter = new Recycler_variety_Adapter(mData);
  31. mRecy.setAdapter(adapter);
  32. }
  33. }

recyclerview_test_layout的布局就是只有一个RecyclerView

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

RecyclerView的Adapter

  1. public class Recycler_variety_Adapter extends RecyclerView.Adapter {
  2. //定义三种常量 表示三种条目类型
  3. public static final int TYPE_PULL_IMAGE = 0;
  4. public static final int TYPE_RIGHT_IMAGE = 1;
  5. public static final int TYPE_THREE_IMAGE = 2;
  6. private List<MoreTypeBean> mData;
  7. public Recycler_variety_Adapter(List<MoreTypeBean> data) {
  8. this.mData = data;
  9. }
  10. @Override
  11. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  12. //创建不同的 ViewHolder
  13. View view;
  14. //根据viewtype来创建条目
  15. if (viewType == TYPE_PULL_IMAGE) {
  16. view =View.inflate(parent.getContext(),R.layout.item_pull_img,null);
  17. return new PullImageHolder(view);
  18. } else if (viewType == TYPE_RIGHT_IMAGE) {
  19. view =View.inflate(parent.getContext(),R.layout.item_right_img,null);
  20. return new RightImageHolder(view);
  21. } else {
  22. view =View.inflate(parent.getContext(),R.layout.item_three_img,null);
  23. return new ThreeImageHolder(view);
  24. }
  25. }
  26. @Override
  27. public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  28. }
  29. @Override
  30. public int getItemCount() {
  31. if (mData != null) {
  32. return mData.size();
  33. }
  34. return 0;
  35. }
  36. //根据条件返回条目的类型
  37. @Override
  38. public int getItemViewType(int position) {
  39. MoreTypeBean moreTypeBean = mData.get(position);
  40. if (moreTypeBean.type == 0) {
  41. return TYPE_PULL_IMAGE;
  42. } else if (moreTypeBean.type == 1) {
  43. return TYPE_RIGHT_IMAGE;
  44. } else {
  45. return TYPE_THREE_IMAGE;
  46. }
  47. }
  48. /**
  49. * 创建三种ViewHolder
  50. */
  51. private class PullImageHolder extends RecyclerView.ViewHolder {
  52. public PullImageHolder(View itemView) {
  53. super(itemView);
  54. }
  55. }
  56. private class RightImageHolder extends RecyclerView.ViewHolder {
  57. public RightImageHolder(View itemView) {
  58. super(itemView);
  59. }
  60. }
  61. private class ThreeImageHolder extends RecyclerView.ViewHolder {
  62. public ThreeImageHolder(View itemView) {
  63. super(itemView);
  64. }
  65. }
  66. }

item_pull_img布局

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="vertical"
  5. >
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:padding="7dp"
  9. android:background="#fff"
  10. android:orientation="vertical"
  11. android:layout_height="wrap_content">
  12. <TextView
  13. android:layout_width="wrap_content"
  14. android:layout_height="wrap_content"
  15. android:textColor="#000"
  16. android:text="多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型"
  17. android:textSize="16sp"
  18. />
  19. <ImageView
  20. android:layout_width="match_parent"
  21. android:layout_height="150dp"
  22. android:background="@drawable/sucai6"
  23. android:scaleType="fitXY"
  24. />
  25. </LinearLayout>
  26. <View
  27. android:layout_marginTop="3dp"
  28. android:layout_width="match_parent"
  29. android:layout_height="1dp"
  30. android:background="#d3d3d3"
  31. />
  32. </LinearLayout>

item_right_img布局

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="100dp"
  4. android:orientation="vertical"
  5. >
  6. <LinearLayout
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. android:background="#fff"
  10. android:orientation="horizontal"
  11. android:padding="7dp">
  12. <TextView
  13. android:layout_width="0dp"
  14. android:layout_height="wrap_content"
  15. android:layout_weight="1"
  16. android:text="多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型"
  17. android:textColor="#000"
  18. android:textSize="16sp" />
  19. <ImageView
  20. android:layout_width="120dp"
  21. android:layout_height="90dp"
  22. android:background="@drawable/sucai" />
  23. </LinearLayout>
  24. <View
  25. android:layout_marginTop="3dp"
  26. android:layout_width="match_parent"
  27. android:layout_height="1dp"
  28. android:background="#d3d3d3"
  29. />
  30. </LinearLayout>

item_three_img布局

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:orientation="vertical"
  5. >
  6. <LinearLayout
  7. android:orientation="vertical"
  8. android:layout_width="match_parent"
  9. android:background="#fff"
  10. android:padding="7sp"
  11. android:layout_height="wrap_content">
  12. <TextView
  13. android:layout_width="match_parent"
  14. android:layout_height="wrap_content"
  15. android:text="多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型,多种条目类型多种条目类型"
  16. android:textColor="#000"
  17. android:textSize="16sp" />
  18. <LinearLayout
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:orientation="horizontal">
  22. <ImageView
  23. android:layout_width="0dp"
  24. android:layout_height="80dp"
  25. android:layout_weight="1"
  26. android:scaleType="fitXY"
  27. android:src="@drawable/sucai3" />
  28. <View
  29. android:layout_width="6dp"
  30. android:layout_height="0dp"/>
  31. <ImageView
  32. android:layout_width="0dp"
  33. android:layout_height="80dp"
  34. android:layout_weight="1"
  35. android:scaleType="fitXY"
  36. android:src="@drawable/sucai4" />
  37. <View
  38. android:layout_width="6dp"
  39. android:layout_height="0dp"/>
  40. <ImageView
  41. android:layout_width="0dp"
  42. android:layout_height="80dp"
  43. android:layout_weight="1"
  44. android:scaleType="fitXY"
  45. android:src="@drawable/sucai5" />
  46. </LinearLayout>
  47. </LinearLayout>
  48. <View
  49. android:layout_marginTop="3dp"
  50. android:background="#d3d3d3"
  51. android:layout_width="match_parent"
  52. android:layout_height="1dp"/>
  53. </LinearLayout>

MoreTypeBean

  1. public class MoreTypeBean {
  2. public int type;
  3. public int pic;
  4. }

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. time 时间模块的函数调用

    时间模块 time 此模块提供了时间相关的函数,且一直可用 时间简介 公元纪年是从公元 0000年1月1日0时开始的 计算机元年是从1970年1月1日0时开始的,此时时间为0,之后每过一秒时间+1 U ...

  2. 【JOISC2012】fish

    Description 有 \(n\) 条鱼,第 \(i\) 条鱼的长度为 \(L_i\),颜色是 \(C_i\)(\(C_i\) 只能是 'R','G','B'). 你需要从中挑出至少一条鱼,要求挑 ...

  3. MySQL--------SQL优化审核工具实战

    1. 背景 SQLAdvisor是由美团点评公司技术工程部DBA团队(北京)开发维护的一个分析SQL给出索引优化建议的工具.它基于MySQL原生态词法解析,结合分析SQL中的where条件.聚合条件. ...

  4. python 判断数据类型及释疑

    Python 判断数据类型有type和isinstance 基本区别在于: type():不会认为子类是父类 isinstance():会认为子类是父类类型 class Color(object): ...

  5. JAVA遇见HTML——JSP篇(案例项目)

  6. u-boot器件驱动模型(Device&Drivers)之uclass (转)

    一.剧情回顾 在上一篇链接器的秘密里面我们讲到我们用一些特殊的宏让链接器帮我们把一些初始化好的结构体列好队并安排在程序的某一个段里面,这里我例举出了三个和我们主题相关段的分布情况,它们大概如下图所示: ...

  7. 在js中获取 input checkbox里选中的多个值

    思路:利用name属性值获取checkbox对象,然后循环判断checked属性(true表示被选中,false表示未选中).下面进行实例演示: 1.html中展示: <input type=& ...

  8. Educational Codeforces Round 50 (Rated for Div. 2) F - Relatively Prime Powers(数学+容斥)

    题目链接:http://codeforces.com/contest/1036/problem/F 题意: 题解:求在[2,n]中,x != a ^ b(b >= 2 即为gcd)的个数,那么实 ...

  9. 题解 noip2019模拟赛Day1T3

    题面 运河计划 问题描述 水运在人类的交通运输史中一直扮演着重要的角色.借助河流.的便利,人们得以把大量的货物输送到天南海北不仅仅是自然界现成的河流,人工开凿的运河(如苏伊士运河.巴拿马运河.我国的京 ...

  10. Educational Codeforces Round 33 (Rated for Div. 2) A题

    A. Chess For Three Alex, Bob and Carl will soon participate in a team chess tournament. Since they a ...