当前位置:   article > 正文

RecyclerView实现多种item布局_recyclerview多种item

recyclerview多种item

在项目中列表是基本都会用到的,然而在显示列表时,我们需要的数据可能需要不止一种item显示,对于复杂的数据就需要多种item,以不同的样式显示出来,这样效果是很棒的,我们先看一下效果


我们可以看到,这个RecyclerView中有多种item显示出来,那么具体怎么实现呢,其实在RecyclerView中,我们可以重写方法getItemViewType(),这个方法会传进一个参数position表示当前是第几个Item,然后我们可以通过position拿到当前的Item对象,然后判断这个item对象需要那种视图,返回一个int类型的视图标志,然后在onCreatViewHolder方法中给引入布局,这样就能够实现多种item显示了,讲了这么多我们看一下具体的例子

  1. @Override
  2. public int getItemViewType(int position) {
  3. if(list.size() == 0){
  4. return EMPTY_VIEW;
  5. } else if(list.get(position) == null){
  6. return PROGRESS_VIEW;
  7. } else if(list.get(position).getType().equals(News.IMAGE_NEWS)){
  8. return IMAGE_VIEW;
  9. } else {
  10. return super.getItemViewType(position);
  11. }
  12. }
首先我们重写了getItemViewType这个方法,在这个方法中根据position对item对象做了一些判断,如果存储item对象的集合大小为空,返回空view标识(这里为1),如果item对象为null,返回进度条标识,这个主要是用于实现下拉加载更多,如果item对象类型属于图片类型,就返回图片类型对应的Item,这个就是效果图中的第一个Item类型,否则就是其它类型,也就是效果图中的另一种item布局,然后我们在onCreatViewHolder中具体的为每一种类型引入其布局

  1. @Override
  2. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  3. View view;
  4. if(viewType == PROGRESS_VIEW){
  5. view = LayoutInflater.from(parent.getContext()).inflate(R.layout.progressbar_item, parent, false);
  6. return new ProgressViewHolder(view);
  7. } else if(viewType == EMPTY_VIEW){
  8. return null;
  9. } else if(viewType == IMAGE_VIEW){
  10. view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_news_item, parent, false);
  11. return new ImageViewHolder(view);
  12. } else {
  13. view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
  14. return new NewsViewHolder(view);
  15. }
  16. }
上面的代码就是具体为每种viewType引入其对应的布局,这样就基本实现了多种item布局,但是仅仅是这些还不够,因为我们还要对每种item设置数据,所以还要对每种item写一个VIewHolder来为item显示数据

  1. class NewsViewHolder extends RecyclerView.ViewHolder{
  2. @BindView(R.id.news_title)TextView title;
  3. @BindView(R.id.news_digest)TextView digest;
  4. @BindView(R.id.news_time)TextView time;
  5. @BindView(R.id.news_src)ImageView image;
  6. public NewsViewHolder(View itemView) {
  7. super(itemView);
  8. ButterKnife.bind(this, itemView);
  9. }
  10. }
  11. class ImageViewHolder extends RecyclerView.ViewHolder{
  12. @BindView(R.id.news_title) TextView title;
  13. @BindView(R.id.image_left) ImageView imageLeft;
  14. @BindView(R.id.image_right) ImageView imageRight;
  15. @BindView(R.id.image_middle) ImageView imageMiddle;
  16. @BindView(R.id.news_time) TextView time;
  17. public ImageViewHolder(View itemView) {
  18. super(itemView);
  19. ButterKnife.bind(this, itemView);
  20. }
  21. }
  22. class ProgressViewHolder extends RecyclerView.ViewHolder {
  23. @BindView(R.id.progressBar) ProgressBar progressBar;
  24. @BindView(R.id.textView) TextView textView;
  25. public ProgressViewHolder(View itemView) {
  26. super(itemView);
  27. ButterKnife.bind(this, itemView);
  28. }
  29. }
上面就是item对应的几个ViewHolder,判断viewHolder属于那种对象,然后在onBindViewHolder中根据对应的ViewHolder对其控件设置数据并显示

  1. @Override
  2. public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
  3. holder.itemView.setOnClickListener(new View.OnClickListener() {
  4. @Override
  5. public void onClick(View v) {
  6. clickListener.onItemClick(v, position);
  7. }
  8. });
  9. if(holder instanceof NewsViewHolder){
  10. NewsViewHolder viewHolder = (NewsViewHolder)holder;
  11. viewHolder.title.setText(list.get(position).getTitle());
  12. viewHolder.time.setText(list.get(position).getTime());
  13. /**
  14. * Glide加载图片
  15. */
  16. Glide.with(context).load(list.get(position).getImageUrl().get(0))
  17. .override(dpToPx(72), dpToPx(72)).centerCrop().into(viewHolder.image);
  18. if(list.get(position).getType().equals(News.TEXT_NEWS)){
  19. viewHolder.digest.setText(list.get(position).getDigest());
  20. } else {
  21. viewHolder.digest.setText("");
  22. }
  23. } else if(holder instanceof ImageViewHolder){
  24. ImageViewHolder viewHolder = (ImageViewHolder)holder;
  25. viewHolder.title.setText(list.get(position).getTitle());
  26. viewHolder.time.setText(list.get(position).getTime());
  27. setItemImage(viewHolder, list, position);
  28. } else if(holder instanceof ProgressViewHolder){
  29. ProgressViewHolder viewHolder = (ProgressViewHolder)holder;
  30. viewHolder.progressBar.setIndeterminate(true);
  31. }
  32. }

整个过程基本就是这样,这种方式在项目中经常会用到,我们就可以这样去处理,下拉加载更多就可以这样实现,在加载完数据后再往对象集合中传入null,然后判断如果出现null就加载progressBar布局,再加上Google官方的SwipeRefreshLayout,下拉刷新,上拉加载就搞定了,其实很容易,而且也有点Material Design 的感觉~~~~~~

看下Adapter的全部代码

  1. package com.zmt.e_read.Adapter;
  2. import android.content.Context;
  3. import android.support.v7.widget.RecyclerView;
  4. import android.util.DisplayMetrics;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.widget.ImageView;
  9. import android.widget.TextView;
  10. import com.bumptech.glide.Glide;
  11. import com.zmt.e_read.Module.News;
  12. import com.zmt.e_read.Module.OnItemClickListener;
  13. import com.zmt.e_read.R;
  14. import com.zmt.e_read.Utils.ProgressViewHolder;
  15. import java.util.Collection;
  16. import java.util.Collections;
  17. import java.util.List;
  18. import butterknife.BindView;
  19. import butterknife.ButterKnife;
  20. /**
  21. * Created by Dangelo on 2016/9/27.
  22. */
  23. public class NewsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  24. private final int EMPTY_VIEW = 1;
  25. private final int PROGRESS_VIEW = 2;
  26. private final int IMAGE_VIEW = 3;
  27. private Context context;
  28. private List<News> list;
  29. private OnItemClickListener clickListener;
  30. public NewsAdapter(Context context, List<News> list, OnItemClickListener clickListener) {
  31. this.context = context;
  32. this.list = list;
  33. this.clickListener = clickListener;
  34. }
  35. public void addOnItemClickListener(OnItemClickListener clickListener){
  36. this.clickListener = clickListener;
  37. }
  38. @Override
  39. public int getItemViewType(int position) {
  40. if(list.size() == 0){
  41. return EMPTY_VIEW;
  42. } else if(list.get(position) == null){
  43. return PROGRESS_VIEW;
  44. } else if(list.get(position).getType().equals(News.IMAGE_NEWS)){
  45. return IMAGE_VIEW;
  46. } else {
  47. return super.getItemViewType(position);
  48. }
  49. }
  50. @Override
  51. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  52. View view;
  53. if(viewType == PROGRESS_VIEW){
  54. view = LayoutInflater.from(parent.getContext()).inflate(R.layout.progressbar_item, parent, false);
  55. return new ProgressViewHolder(view);
  56. } else if(viewType == EMPTY_VIEW){
  57. return null;
  58. } else if(viewType == IMAGE_VIEW){
  59. view = LayoutInflater.from(parent.getContext()).inflate(R.layout.image_news_item, parent, false);
  60. return new ImageViewHolder(view);
  61. } else {
  62. view = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_item, parent, false);
  63. return new NewsViewHolder(view);
  64. }
  65. }
  66. @Override
  67. public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
  68. holder.itemView.setOnClickListener(new View.OnClickListener() {
  69. @Override
  70. public void onClick(View v) {
  71. clickListener.onItemClick(v, position);
  72. }
  73. });
  74. if(holder instanceof NewsViewHolder){
  75. NewsViewHolder viewHolder = (NewsViewHolder)holder;
  76. viewHolder.title.setText(list.get(position).getTitle());
  77. viewHolder.time.setText(list.get(position).getTime());
  78. /**
  79. * Glide加载图片
  80. */
  81. Glide.with(context).load(list.get(position).getImageUrl().get(0))
  82. .override(dpToPx(72), dpToPx(72)).centerCrop().into(viewHolder.image);
  83. if(list.get(position).getType().equals(News.TEXT_NEWS)){
  84. viewHolder.digest.setText(list.get(position).getDigest());
  85. } else {
  86. viewHolder.digest.setText("");
  87. }
  88. } else if(holder instanceof ImageViewHolder){
  89. ImageViewHolder viewHolder = (ImageViewHolder)holder;
  90. viewHolder.title.setText(list.get(position).getTitle());
  91. viewHolder.time.setText(list.get(position).getTime());
  92. setItemImage(viewHolder, list, position);
  93. } else if(holder instanceof ProgressViewHolder){
  94. ProgressViewHolder viewHolder = (ProgressViewHolder)holder;
  95. viewHolder.progressBar.setIndeterminate(true);
  96. }
  97. }
  98. public void setItemImage(ImageViewHolder viewHolder, List<News> list, int position){
  99. viewHolder.imageMiddle.setVisibility(View.VISIBLE);
  100. viewHolder.imageRight.setVisibility(View.VISIBLE);
  101. DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
  102. if(list.get(position).getImageUrl().size() == 1){
  103. Glide.with(context).load(list.get(position).getImageUrl().get(0))
  104. .override(displayMetrics.widthPixels - dpToPx(10), dpToPx(90))
  105. .centerCrop().into(viewHolder.imageLeft);
  106. viewHolder.imageMiddle.setVisibility(View.GONE);
  107. viewHolder.imageRight.setVisibility(View.GONE);
  108. } else if(list.get(position).getImageUrl().size() == 2){
  109. int imageWidth = (displayMetrics.widthPixels - dpToPx(20)) / 2;
  110. Glide.with(context).load(list.get(position).getImageUrl().get(0))
  111. .override(imageWidth, dpToPx(90))
  112. .centerCrop().into(viewHolder.imageLeft);
  113. Glide.with(context).load(list.get(position).getImageUrl().get(1))
  114. .override(imageWidth, dpToPx(90))
  115. .centerCrop().into(viewHolder.imageMiddle);
  116. viewHolder.imageRight.setVisibility(View.GONE);
  117. } else if(list.get(position).getImageUrl().size() >= 3){
  118. int imageWidth = (displayMetrics.widthPixels - dpToPx(30)) / 3;
  119. Glide.with(context).load(list.get(position).getImageUrl().get(0))
  120. .override(imageWidth, dpToPx(90))
  121. .centerCrop().into(viewHolder.imageLeft);
  122. Glide.with(context).load(list.get(position).getImageUrl().get(1))
  123. .override(imageWidth, dpToPx(90))
  124. .centerCrop().into(viewHolder.imageMiddle);
  125. Glide.with(context).load(list.get(position).getImageUrl().get(2))
  126. .override(imageWidth, dpToPx(90))
  127. .centerCrop().into(viewHolder.imageRight);
  128. }
  129. }
  130. @Override
  131. public int getItemCount() {
  132. return list.size();
  133. }
  134. public int dpToPx(float dp){
  135. float px = context.getResources().getDisplayMetrics().density;
  136. return (int)(dp * px + 0.5f);
  137. }
  138. class NewsViewHolder extends RecyclerView.ViewHolder{
  139. @BindView(R.id.news_title)TextView title;
  140. @BindView(R.id.news_digest)TextView digest;
  141. @BindView(R.id.news_time)TextView time;
  142. @BindView(R.id.news_src)ImageView image;
  143. public NewsViewHolder(View itemView) {
  144. super(itemView);
  145. ButterKnife.bind(this, itemView);
  146. }
  147. }
  148. class ImageViewHolder extends RecyclerView.ViewHolder{
  149. @BindView(R.id.news_title) TextView title;
  150. @BindView(R.id.image_left) ImageView imageLeft;
  151. @BindView(R.id.image_right) ImageView imageRight;
  152. @BindView(R.id.image_middle) ImageView imageMiddle;
  153. @BindView(R.id.news_time) TextView time;
  154. public ImageViewHolder(View itemView) {
  155. super(itemView);
  156. ButterKnife.bind(this, itemView);
  157. }
  158. }
  159. <pre name="code" class="java"> class ProgressViewHolder extends RecyclerView.ViewHolder {
  160. @BindView(R.id.progressBar) ProgressBar progressBar;
  161. @BindView(R.id.textView) TextView textView;
  162. public ProgressViewHolder(View itemView) {
  163. super(itemView);
  164. ButterKnife.bind(this, itemView);
  165. }
  166. }
}

 

项目地址:https://github.com/xiyouZmt/E-Read

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

闽ICP备14008679号