当前位置:   article > 正文

RecyclerView中的多种布局_recyclerview不同布局

recyclerview不同布局

概念

RecyclerView可以用于加载不同的视图类型,基于服务器的反馈,可能会要求加载不同的布局。我们需要在适配器中重写的方法:getItemViewType(),onCreatViewHolder(),onBindViewHolder()

实现

首先在上一节的基础上我们要替换掉基础的SimpleItemRecyclerViewAdapter 而是用ComplexRecyclerViewAdapter ,下面的例子将会包含两种布局Layout_viewHolder1.xml 用来存储User对象,而layout_viewHolder2.xml将会用来存储String对象。

  1. 在主活动中创建一个数据集合分别是文字和图像
  1. private ArrayList<Object> getSampleArrayList() {
  2. ArrayList<Object> items = new ArrayList<>();
  3. items.add(new User("Dany Targaryen", "Valyria"));
  4. items.add(new User("Rob Stark", "Winterfell"));
  5. items.add("image");
  6. items.add(new User("Jon Snow", "Castle Black"));
  7. items.add("image");
  8. items.add(new User("Tyrion Lanister", "King's Landing"));
  9. return items;
  10. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  1. 配置两种viewHolder用来展示数据
  1. public class ViewHolder1 extends RecyclerView.ViewHolder {
  2. private TextView label1, label2;
  3. public ViewHolder1(View v) {
  4. super(v);
  5. label1 = (TextView) v.findViewById(R.id.text1);
  6. label2 = (TextView) v.findViewById(R.id.text2);
  7. }
  8. public TextView getLabel1() {
  9. return label1;
  10. }
  11. public void setLabel1(TextView label1) {
  12. this.label1 = label1;
  13. }
  14. public TextView getLabel2() {
  15. return label2;
  16. }
  17. public void setLabel2(TextView label2) {
  18. this.label2 = label2;
  19. }
  20. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:id="@+id/llContainer"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="wrap_content"
  6. android:padding="5dp">
  7. <TextView
  8. android:id="@+id/text1"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:textStyle="bold"
  12. android:textAppearance="?android:attr/textAppearanceListItemSmall"
  13. android:gravity="center_vertical"/>
  14. <TextView
  15. android:id="@+id/text2"
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content"
  18. android:textAppearance="?android:attr/textAppearanceListItemSmall"
  19. android:gravity="center_vertical" />
  20. </LinearLayout>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

3.创建复杂的适配器ComplexRecyclerViewAdapter

  1. public class ComplexRecyclerViewAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  2. //在RecyclerView中展示的项
  3. private List<Object> items;
  4. //为了区分加入标签
  5. private final int USER = 0 , IMAGE = 1;
  6. public ComplexRecyclerViewAdapter(List<Object> items) {
  7. this.items = items;
  8. }
  9. //获得适配器中的项的数量和类型
  10. @Override
  11. public int getItemCount() {
  12. return this.items.size();
  13. }
  14. //获得项的类型
  15. @Override
  16. public int getItemViewType(int position) {
  17. if (items.get(position) instanceof User) {
  18. return USER;
  19. } else if (items.get(position) instanceof String) {
  20. return IMAGE;
  21. }
  22. return -1;
  23. }
  24. //将数据填充进viewHolder来展示
  25. @Override
  26. public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  27. switch (RecyclerView.ViewHolder.getItemViewType()) {
  28. case USER:
  29. ViewHolder1 vh1 = (ViewHolder1) holder;//转型
  30. configureViewHolder1(vh1, position);
  31. break;
  32. case IMAGE:
  33. ViewHolder2 vh2 = (ViewHolder2) holder;
  34. configureViewHolder2(vh2);
  35. break;
  36. default:
  37. RecyclerViewSimpleTextViewHolder vh = (RecyclerViewSimpleTextViewHolder) holder;
  38. confgureDefaultViewHolder(vh, position);
  39. break;
  40. }
  41. }
  42. //基于项不同的类型来获得不同的viewholder,
  43. @Override
  44. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  45. RecyclerView.ViewHolder viewHolder;
  46. LayoutInflater inflater = LayoutInflater.from(parent.getContext());
  47. switch (viewType) {
  48. case USER:
  49. View v1 =inflater.inflate(R.layout.layout_viewholder1, parent, false);
  50. viewHolder = new ViewHolder1(v1);
  51. break;
  52. case IMAGE:
  53. View v2 = inflater.inflate(R.layout.layout_viewholder2, parent, false);
  54. break;
  55. default:
  56. View v = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
  57. viewHolder = new RecyclerViewSimpleTextViewHolder(v);
  58. break;
  59. }
  60. return viewHolder;
  61. }
  62. //配置viewHolder展示数据
  63. private void configureDefaultViewHolder(RecyclerViewSimpleTextViewHolder vh, int position) {
  64. vh.getlabel().setText((CharSequence) items.get(position));
  65. }
  66. private void configureViewHolder1(ViewHolder1 vh1, int position) {
  67. User user = (User) items.get(position);
  68. if (user != null) {
  69. vh1.getLabel1().setText("Name:" + user.name);
  70. vh1.getLabel2().setText("Hometown" + user.hometown);
  71. }
  72. }
  73. private void configureViewHolder2(ViewHolder2 vh2) {
  74. vh2.getImagView().setImageResource(R.mipmap.ic_launcher);
  75. }
  76. }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82

最后在主活动中 
recyclerView.setAdapter(new ComplexRecyclerViewAdapter(getSampleArrayList()));

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

闽ICP备14008679号