当前位置:   article > 正文

RecyclerView包含多种布局实现_recyclerview多种布局

recyclerview多种布局

内容:同一个RecyclerView包含多种布局。本例设置了三个不同的布局样式,都在同一个RecyclerView中展示出来。

尾末简要介绍了下GridLayoutManager的setSpanSizeLookup()方法

效果如下:

步骤:

1、定义三种不同布局,用于填充到RecyclerView中

2、加入RecyclerView控件,设置好adapter和数据

3、新建类继承RecyclerView.Adapter<RecyclerView.ViewHolder>,注意:<RecyclerView.ViewHolder>,此处不是具体的viewHoler

4、重写getItemViewType(int position),这里设置好视图类型,后面会根据此处的返回类型去加载对应的布局

5、onCreateViewHolder(ViewGroup parent, int viewType)根据viewType填充不同布局

6、onBindViewHolder(RecyclerView.ViewHolder holder, final int position)根据holder类型设置数据填充

代码如下:

1、填充布局不贴出,就一个TextView宽高颜色不一样

2、加入RecyclerView控件,设置好adapter和数据

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:id="@+id/linearLayout"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical">
  7. <android.support.v7.widget.RecyclerView
  8. android:id="@+id/recyclerView"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"/>
  11. </LinearLayout>
  1. public class MainActivity extends AppCompatActivity{
  2. private String[] mStrings = {"1", "2", "3", "3", "2", "1", "1", "3", "2", "3", "1", "3"};
  3. private List<String> data = new ArrayList<>();
  4. @Override
  5. protected void onCreate(Bundle savedInstanceState) {
  6. super.onCreate(savedInstanceState);
  7. setContentView(R.layout.activity_main);
  8. data = Arrays.asList(mStrings);
  9. RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
  10. recyclerView.setLayoutManager(new LinearLayoutManager(this));
  11. recyclerView.setAdapter(new MyAdapter(this, data));
  12. }
  13. }

3、4、5、6

  1. public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  2. private Context mContext;
  3. private List<String> content;
  4. public MyAdapter(List<String> content, Context context) {
  5. this.content = content;
  6. mContext = context;
  7. }
  8. public MyAdapter(Context context, List<String> content) {
  9. mContext = context;
  10. this.content = content;
  11. }
  12. /**
  13. * 根据position位置(或者自己需求)设置不同视图类型
  14. */
  15. @Override
  16. public int getItemViewType(int position) {
  17. switch (content.get(position)) {
  18. case "1":
  19. return 1;
  20. case "2":
  21. return 2;
  22. default:
  23. return 3;
  24. }
  25. }
  26. /**
  27. * @param parent
  28. * @param viewType
  29. * @return 根据viewType设置不同的布局 从而实现多布局
  30. */
  31. @Override
  32. public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  33. RecyclerView.ViewHolder viewHolder;
  34. View view;
  35. //根据不同的类型加载对应的布局
  36. switch (viewType) {
  37. case 1:
  38. view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_1, parent, false);
  39. viewHolder = new FirstHolder(view);
  40. return viewHolder;
  41. case 2:
  42. view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_2, parent,false);
  43. viewHolder = new SecondHolder(view);
  44. return viewHolder;
  45. case 3:
  46. view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_3, parent,false);
  47. viewHolder = new ThirdHolder(view);
  48. return viewHolder;
  49. default:
  50. return null;
  51. }
  52. }
  53. /**
  54. * @param holder
  55. * @param position 根据holder设置不同的数据
  56. */
  57. @Override
  58. public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
  59. if (holder instanceof FirstHolder) {
  60. ((FirstHolder) holder).mTextView.setText("FirstHolder");
  61. ((FirstHolder) holder).mTextView.setOnClickListener(new View.OnClickListener() {
  62. @Override
  63. public void onClick(View v) {
  64. toast("FirstHolder pos:" + position);
  65. }
  66. });
  67. } else if (holder instanceof SecondHolder) {
  68. ((SecondHolder) holder).mTextView.setText("SecondHolder");
  69. ((SecondHolder) holder).mTextView.setOnClickListener(new View.OnClickListener() {
  70. @Override
  71. public void onClick(View v) {
  72. toast("SecondHolder pos:" + position);
  73. }
  74. });
  75. }else if (holder instanceof ThirdHolder) {
  76. ((ThirdHolder) holder).mTextView.setText("ThirdHolder");
  77. ((ThirdHolder) holder).mTextView.setOnClickListener(new View.OnClickListener() {
  78. @Override
  79. public void onClick(View v) {
  80. toast("ThirdHolder pos:" + position);
  81. }
  82. });
  83. }
  84. }
  85. @Override
  86. public int getItemCount() {
  87. return content.size();
  88. }
  89. private void toast(String string) {
  90. Toast.makeText(mContext, string, Toast.LENGTH_SHORT).show();
  91. }
  92. public static class FirstHolder extends RecyclerView.ViewHolder {
  93. public TextView mTextView;
  94. public FirstHolder(View itemView) {
  95. super(itemView);
  96. mTextView = itemView.findViewById(R.id.tv_1);
  97. }
  98. }
  99. public static class SecondHolder extends RecyclerView.ViewHolder{
  100. public TextView mTextView;
  101. public SecondHolder(View itemView) {
  102. super(itemView);
  103. mTextView = itemView.findViewById(R.id.tv_2);
  104. }
  105. }
  106. public static class ThirdHolder extends RecyclerView.ViewHolder{
  107. public TextView mTextView;
  108. public ThirdHolder(View itemView) {
  109. super(itemView);
  110. mTextView = itemView.findViewById(R.id.tv_3);
  111. }
  112. }
  113. }

Kotlin代码如下:

  1. class MainActivity : AppCompatActivity() {
  2. override fun onCreate(savedInstanceState: Bundle?) {
  3. super.onCreate(savedInstanceState)
  4. setContentView(R.layout.activity_main)
  5. val data = listOf<String>("1", "2", "3", "3", "2", "1", "1", "3", "2", "3", "1", "3")
  6. recyclerView.layoutManager = LinearLayoutManager(this)
  7. recyclerView.adapter = MyAdapter(this, data)
  8. }
  9. }
  1. class MyAdapter(private val context: Context, private val data: List<String>): RecyclerView.Adapter<RecyclerView.ViewHolder>() {
  2. override fun getItemViewType(position: Int): Int {
  3. when (data[position]) {
  4. "1" -> return 1
  5. "2" -> return 2
  6. else -> return 3
  7. }
  8. }
  9. override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): RecyclerView.ViewHolder? {
  10. val viewHolder: RecyclerView.ViewHolder
  11. val view: View
  12. //根据不同的类型加载对应的布局
  13. when (viewType) {
  14. 1 -> {
  15. view = LayoutInflater.from(parent?.getContext()).inflate(R.layout.item_1, parent, false)
  16. viewHolder = FirstHolder(view)
  17. return viewHolder
  18. }
  19. 2 -> {
  20. view = LayoutInflater.from(parent?.getContext()).inflate(R.layout.item_2, parent, false)
  21. viewHolder = SecondHolder(view)
  22. return viewHolder
  23. }
  24. 3 -> {
  25. view = LayoutInflater.from(parent?.getContext()).inflate(R.layout.item_3, parent, false)
  26. viewHolder = ThirdHolder(view)
  27. return viewHolder
  28. }
  29. else -> return null
  30. }
  31. }
  32. override fun getItemCount(): Int {
  33. return data.size
  34. }
  35. override fun onBindViewHolder(holder: RecyclerView.ViewHolder?, position: Int) {
  36. if (holder is FirstHolder) {
  37. holder.mTextView.text = "FirstHolder"
  38. holder.mTextView.setOnClickListener { toast("FirstHolder pos:$position") }
  39. } else if (holder is SecondHolder) {
  40. holder.mTextView.text = "SecondHolder"
  41. holder.mTextView.setOnClickListener { toast("SecondHolder pos:$position") }
  42. } else if (holder is ThirdHolder) {
  43. holder.mTextView.text = "ThirdHolder"
  44. holder.mTextView.setOnClickListener { toast("ThirdHolder pos:$position") }
  45. }
  46. }
  47. private fun toast(string: String) {
  48. Toast.makeText(context, string, Toast.LENGTH_SHORT).show()
  49. }
  50. class FirstHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
  51. var mTextView: TextView
  52. init {
  53. mTextView = itemView.findViewById(R.id.tv_1)
  54. }
  55. }
  56. class SecondHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
  57. var mTextView: TextView
  58. init {
  59. mTextView = itemView.findViewById(R.id.tv_2)
  60. }
  61. }
  62. class ThirdHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
  63. var mTextView: TextView
  64. init {
  65. mTextView = itemView.findViewById(R.id.tv_3)
  66. }
  67. }
  68. }

另一种是可以快速修改GridLayoutManager 布局 图片展示个数,通过设置setSpanSizeLookup()

代码如下:

  1. RecyclerView recylerview = (RecyclerView) findViewById(R.id.recylerview);
  2. GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
  3. gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
  4. @Override
  5. public int getSpanSize(int i) {
  6. if (i == 0 || i == 3 ){
  7. return 2;
  8. }else {
  9. return 1;
  10. }
  11. }
  12. });
  13. recylerview.setLayoutManager(gridLayoutManager);
  14. GridAdapter adapter = new GridAdapter();
  15. recylerview.setAdapter(adapter);

注意:getSpanSize return 的数量不能大于 你 new GridLayoutManager(this, 2);的数量

否则会报错

java.lang.IllegalArgumentException: Item at position 0 requires 3 spans but GridLayoutManager has only 2 spans.

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

闽ICP备14008679号