当前位置:   article > 正文

RecyclerView实现复杂多类型布局,再也不用嵌套了_recyclerview复杂布局

recyclerview复杂布局

1、RecyclerView 控件通过 setLayoutManager() 方法来设置布局管理器,这个布局管理器分三种 LinearLayoutManagerGridLayoutManagerStaggeredGridLayoutManager

spanCount:在创建 GridLayoutManager 对象的时候构造方法需要传入这个参数,也就是设置每行排列 item 个数。

spanSize:在 setSpanSizeLookup() 方法中,这个方法返回的是当前位置的 item 跨度大小,总跨度(也就是总列数)可以设置,假设总列数设置为6,当getSpanSize设置为2,说明这个item占了6份中的两份,说明有3列, 如果getSpanSize返回3,那么一个item占6份中的3份,说明有两列,如下图:

  

2、有了上面概念, 那我们来实现一个如下图的列表:

首先我们要明确,总列数为4,此列表分为3种类型,这三种类型分别占一列、两列、4列,我们要写3种布局和3个ViewHolder,那么“为你推荐”和"便民生活"这四个字的一行getSpanSize应该返回4,他要把4列都占完,也就是它占整个一列,下面生活缴费、蚂蚁森林等要返回2, 每个item跨度为2,也就是只有两列, 最下面充值中心、信用卡还款等getSpanSize应该返回1,每个item跨度只有1列,因此它有4列。

  1. mLayoutManager = new GridLayoutManager(this, 4);
  2. mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
  3. @Override
  4. public int getSpanSize(int position) {
  5. ApplicationBean applicationBean = mDataList.get(position);
  6. if (applicationBean.getType() == ApplicationBean.Type.TypeOne) {
  7. return 4;
  8. } else if (applicationBean.getType() == ApplicationBean.Type.TypeTwo) {
  9. return 2;
  10. } else if (applicationBean.getType() == ApplicationBean.Type.TypeThree) {
  11. return 1;
  12. } else {
  13. return 0;
  14. }
  15. }
  16. });
  17. mRecyclerView.setLayoutManager(mLayoutManager);
  18. mAdapter = new ApplicationAdapter(mDataList, this);
  19. mRecyclerView.setAdapter(mAdapter);
  1. public class ApplicationAdapter extends RecyclerView.Adapter {
  2. //布局类型
  3. private static final int VIEW_TYPE_ONE = 1;
  4. private static final int VIEW_TYPE_TWO = 2;
  5. private static final int VIEW_TYPE_THREE = 3;
  6. private LayoutInflater inflater;
  7. private Context mContext;
  8. private ArrayList<ApplicationBean> mDataList;
  9. public ApplicationAdapter(ArrayList<ApplicationBean> mDataList, Context context) {
  10. this.mDataList = mDataList;
  11. mContext = context;
  12. inflater = LayoutInflater.from(mContext);
  13. }
  14. @Override
  15. public int getItemViewType(int position) {
  16. if (mDataList.get(position).getType() == ApplicationBean.Type.TypeOne) {
  17. return VIEW_TYPE_ONE;
  18. } else if (mDataList.get(position).getType() == ApplicationBean.Type.TypeTwo) {
  19. return VIEW_TYPE_TWO;
  20. } else if (mDataList.get(position).getType() == ApplicationBean.Type.TypeThree) {
  21. return VIEW_TYPE_THREE;
  22. } else {
  23. return 0;
  24. }
  25. }
  26. @Override
  27. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  28. RecyclerView.ViewHolder viewHolder = null;
  29. switch (viewType) {
  30. case VIEW_TYPE_ONE:
  31. viewHolder = new ViewHolderOne(inflater.inflate(R.layout.item_one, parent, false));
  32. break;
  33. case VIEW_TYPE_TWO:
  34. viewHolder = new ViewHolderTwo(inflater.inflate(R.layout.item_two, parent, false));
  35. break;
  36. case VIEW_TYPE_THREE:
  37. viewHolder = new ViewHolderThree(inflater.inflate(R.layout.item_three, parent, false));
  38. break;
  39. }
  40. return viewHolder;
  41. }
  42. @Override
  43. public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  44. switch (getItemViewType(position)) {
  45. case VIEW_TYPE_ONE:
  46. Log.e("huangjie", mDataList.get(position).getTitle());
  47. ((ViewHolderOne) holder).title.setText(mDataList.get(position).getTitle());
  48. break;
  49. case VIEW_TYPE_TWO:
  50. ((ViewHolderTwo) holder).title.setText(mDataList.get(position).getName());
  51. ((ViewHolderTwo) holder).information.setText(mDataList.get(position).getInformation());
  52. ((ViewHolderTwo) holder).icon.setImageDrawable(
  53. mContext.getResources().getDrawable(mDataList.get(position).getIcon()));
  54. break;
  55. case VIEW_TYPE_THREE:
  56. ((ViewHolderThree) holder).title.setText(mDataList.get(position).getName());
  57. ((ViewHolderThree) holder).icon.setImageDrawable(
  58. mContext.getResources().getDrawable(mDataList.get(position).getIcon()));
  59. break;
  60. }
  61. }
  62. @Override
  63. public int getItemCount() {
  64. return mDataList.size();
  65. }
  66. /**
  67. * 第一种布局类型ViewHolder
  68. */
  69. public static class ViewHolderOne extends RecyclerView.ViewHolder {
  70. private TextView title;
  71. public ViewHolderOne(View itemView) {
  72. super(itemView);
  73. title = (TextView) itemView.findViewById(R.id.text);
  74. }
  75. }
  76. /**
  77. * 第二种布局类型ViewHolder
  78. */
  79. public static class ViewHolderTwo extends RecyclerView.ViewHolder {
  80. private ImageView icon;
  81. private TextView title;
  82. private TextView information;
  83. public ViewHolderTwo(View itemView) {
  84. super(itemView);
  85. icon = (ImageView) itemView.findViewById(R.id.image);
  86. title = (TextView) itemView.findViewById(R.id.title);
  87. information = (TextView) itemView.findViewById(R.id.infor);
  88. }
  89. }
  90. /**
  91. * 第三种布局类型viewholder
  92. */
  93. public static class ViewHolderThree extends RecyclerView.ViewHolder {
  94. private ImageView icon;
  95. private TextView title;
  96. public ViewHolderThree(View itemView) {
  97. super(itemView);
  98. icon = (ImageView) itemView.findViewById(R.id.image);
  99. title = (TextView) itemView.findViewById(R.id.title);
  100. }
  101. }
  102. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/174791
推荐阅读
相关标签
  

闽ICP备14008679号