当前位置:   article > 正文

Android Recyclerview多布局

android recyclerview多布局

今天给大家看一个recyclerview多布局的写法,是tablayout是的写法在上一篇文章中,感兴趣的兄弟可以去看看学一学

 这里是动图的地址http://qcloudcos.xunjiepdf.com/xunjievideo/temp/202205111401/45fc6ee39cf44fd5a6adb7e34b489835/video1_1.gif

我们废话不多说,直接上我们最喜欢的代码

添加一下我们的依赖

implementation 'androidx.recyclerview:recyclerview:1.1.0'
implementation 'com.github.bumptech.glide:glide:4.8.0'//glide第三方图片加载所需要的包

然后写我们的recyclerview布局

  1. <androidx.recyclerview.widget.RecyclerView
  2. android:id="@+id/recyclerview"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content" />

然后在我们的Activity或者Fragment中拿到我们的控件,我这边是在fragment中写的

  1. package com.example.map.ui.fragment;
  2. import android.content.Context;
  3. import android.graphics.Rect;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.Toast;
  10. import androidx.annotation.NonNull;
  11. import androidx.annotation.Nullable;
  12. import androidx.appcompat.app.ActionBarDrawerToggle;
  13. import androidx.appcompat.widget.Toolbar;
  14. import androidx.fragment.app.Fragment;
  15. import androidx.recyclerview.widget.DividerItemDecoration;
  16. import androidx.recyclerview.widget.LinearLayoutManager;
  17. import androidx.recyclerview.widget.RecyclerView;
  18. import androidx.recyclerview.widget.StaggeredGridLayoutManager;
  19. import com.example.map.R;
  20. import com.example.map.adapter.MyAdapter;
  21. import com.example.map.api.ApiService;
  22. import com.example.map.bean.WallPaperResponse;
  23. import com.llw.network.NetworkApi;
  24. import com.llw.network.observer.BaseObserver;
  25. import com.llw.network.utils.KLog;
  26. import com.yalantis.phoenix.PullToRefreshView;
  27. import java.util.ArrayList;
  28. import java.util.List;
  29. public class Fragment1 extends Fragment {
  30. private View view;
  31. private RecyclerView mRecyclerView;
  32. private List<WallPaperResponse.ResBean.VerticalBean> list;
  33. private MyAdapter adapter;
  34. @Override
  35. public void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. }
  38. @Nullable
  39. @Override
  40. public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
  41. view = inflater.inflate(R.layout.frament_1, container, false);
  42. initView();
  43. return view;
  44. }
  45. private void initView() {
  46. mPullToRefresh = view.findViewById(R.id.pull_to_refresh);
  47. mRecyclerView = view.findViewById(R.id.recyclerview);
  48. //创建集合
  49. list = new ArrayList<>();
  50. //创建适配器
  51. adapter=new MyAdapter(list,getActivity());
  52. //创建布局管理器
  53. LinearLayoutManager LayoutManager= new LinearLayoutManager(getActivity(),LinearLayoutManager.VERTICAL, false);
  54. mRecyclerView.setLayoutManager(LayoutManager);
  55. //添加适配器
  56. mRecyclerView.setAdapter(adapter);
  57. //访问网络
  58. requestNetwork();
  59. }
  60. private void requestNetwork() {
  61. /**
  62. * OkHttp+Retrofit+Rxjava 网络框架
  63. */
  64. NetworkApi.createService(ApiService.class)
  65. .getWallPaper()
  66. .compose(NetworkApi.applySchedulers(new BaseObserver<WallPaperResponse>() {
  67. @Override
  68. public void onSuccess(WallPaperResponse wallPaperResponse) {
  69. List<WallPaperResponse.ResBean.VerticalBean> vertical = wallPaperResponse.getRes().getVertical();
  70. if (vertical != null && vertical.size() > 0) {
  71. //把请求的数据添加到我们的集合中然后刷新适配器
  72. list.addAll(vertical);
  73. adapter.notifyDataSetChanged();
  74. } else {
  75. Toast.makeText(getActivity(), "数据为空", Toast.LENGTH_SHORT).show();
  76. }
  77. }
  78. @Override
  79. public void onFailure(Throwable e) {
  80. KLog.e("MainActivity", e.toString());
  81. Toast.makeText(getActivity(), "访问失败", Toast.LENGTH_SHORT).show();
  82. }
  83. }));
  84. }
  85. @Override
  86. public void onActivityCreated(Bundle savedInstanceState) {
  87. super.onActivityCreated(savedInstanceState);
  88. }
  89. @Override
  90. public void onAttach(Context context) {
  91. super.onAttach(context);
  92. }
  93. public static Fragment1 newInstance() {
  94. Fragment1 fragment1 = new Fragment1();
  95. return fragment1;
  96. }
  97. }

接下来是我们的适配器

  1. package com.example.map.adapter;
  2. import android.content.Context;
  3. import android.view.LayoutInflater;
  4. import android.view.View;
  5. import android.view.ViewGroup;
  6. import android.widget.ImageView;
  7. import androidx.annotation.NonNull;
  8. import androidx.recyclerview.widget.RecyclerView;
  9. import com.bumptech.glide.Glide;
  10. import com.example.map.R;
  11. import com.example.map.bean.WallPaperResponse;
  12. import java.util.List;
  13. public class MyAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
  14. private List<WallPaperResponse.ResBean.VerticalBean> list;
  15. private Context context;
  16. //三个final分别代表三个不同的布局
  17. public static final int ITEMONE = 1;
  18. public static final int ITEMTWO = 2;
  19. public static final int ITEMTHREE = 3;
  20. public MyAdapter(List<WallPaperResponse.ResBean.VerticalBean> list, Context context) {
  21. this.list = list;
  22. this.context = context;
  23. }
  24. @NonNull
  25. @Override
  26. public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
  27. //这时候就要根据这个i来判断加哪一个布局了
  28. View inflate = null;
  29. RecyclerView.ViewHolder viewHolder = null;
  30. switch (i) {
  31. case ITEMONE:
  32. inflate = LayoutInflater.from(context).inflate(R.layout.rv_item, parent, false);
  33. viewHolder = new OneItemHolder(inflate);
  34. break;
  35. case ITEMTWO:
  36. inflate = LayoutInflater.from(context).inflate(R.layout.item_two, parent, false);
  37. viewHolder = new TwoItemHolder(inflate);
  38. break;
  39. case ITEMTHREE:
  40. inflate = LayoutInflater.from(context).inflate(R.layout.item_three, parent, false);
  41. viewHolder = new ThreeItemHolder(inflate);
  42. break;
  43. }
  44. return viewHolder;
  45. }
  46. /**
  47. * 绑定控件,这里可以写一些事件方法等
  48. *
  49. * @param holder
  50. * @param position
  51. */
  52. @Override
  53. public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
  54. //如果当前的 holder 属于 OneItemHolder 则执行
  55. if (holder instanceof OneItemHolder){
  56. Glide.with(context).load(list.get(position).getImg()).into(((OneItemHolder) holder).image);
  57. }else if(holder instanceof TwoItemHolder){
  58. Glide.with(context).load(list.get(position).getImg()).into(((TwoItemHolder) holder).image1);
  59. Glide.with(context).load(list.get(position).getImg()).into(((TwoItemHolder) holder).image2);
  60. }else if (holder instanceof ThreeItemHolder){
  61. Glide.with(context).load(list.get(position).getImg()).into(((ThreeItemHolder) holder).image1);
  62. Glide.with(context).load(list.get(position).getImg()).into(((ThreeItemHolder) holder).image2);
  63. Glide.with(context).load(list.get(position).getImg()).into(((ThreeItemHolder) holder).image3);
  64. }
  65. }
  66. @Override
  67. public int getItemCount() {
  68. return list.size();
  69. }
  70. /**
  71. * 返回条目类型(这里就做个简单的判断区分)
  72. *
  73. * @param position 代表第几个条目
  74. * @return
  75. */
  76. @Override
  77. public int getItemViewType(int position) {
  78. if (position % 3 == 0) {
  79. return ITEMTHREE;
  80. } else if (position % 2 == 0) {
  81. return ITEMTWO;
  82. } else {
  83. return ITEMONE;
  84. }
  85. }
  86. /**
  87. * 第一个布局的Holder,要继承自RecyclerView.ViewHolder,这里你可以绑定控件
  88. */
  89. private class OneItemHolder extends RecyclerView.ViewHolder {
  90. private ImageView image;
  91. public OneItemHolder(View itemView) {
  92. super(itemView);
  93. image = itemView.findViewById(R.id.iv_img);
  94. }
  95. }
  96. /**
  97. * 第二个布局的Holder,要继承自RecyclerView.ViewHolder,这里你可以绑定控件
  98. */
  99. private class TwoItemHolder extends RecyclerView.ViewHolder {
  100. private ImageView image1;
  101. private ImageView image2;
  102. public TwoItemHolder(View itemView) {
  103. super(itemView);
  104. image1 = itemView.findViewById(R.id.iv_img1);
  105. image2 = itemView.findViewById(R.id.iv_img2);
  106. }
  107. }
  108. /**
  109. * 第三个布局的Holder,要继承自RecyclerView.ViewHolder,这里你可以绑定控件
  110. */
  111. private class ThreeItemHolder extends RecyclerView.ViewHolder {
  112. private ImageView image1;
  113. private ImageView image2;
  114. private ImageView image3;
  115. public ThreeItemHolder(View itemView) {
  116. super(itemView);
  117. image1 = itemView.findViewById(R.id.iv_img1);
  118. image2 = itemView.findViewById(R.id.iv_img2);
  119. image3 = itemView.findViewById(R.id.iv_img3);
  120. }
  121. }
  122. }

然后是适配器中的三种布局样式

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content"
  5. android:orientation="vertical">
  6. <ImageView
  7. android:id="@+id/iv_img"
  8. android:layout_width="150dp"
  9. android:layout_height="200dp"
  10. android:layout_margin="5dp"
  11. android:scaleType="centerCrop"
  12. android:src="@mipmap/bg_fragment_1" />
  13. </LinearLayout>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3. xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
  4. android:layout_height="wrap_content">
  5. <ImageView
  6. android:id="@+id/iv_img1"
  7. android:src="@mipmap/bg_fragment_1"
  8. android:layout_width="150dp"
  9. android:scaleType="centerCrop"
  10. android:layout_marginTop="5dp"
  11. android:layout_height="200dp" />
  12. <ImageView
  13. android:id="@+id/iv_img2"
  14. android:src="@mipmap/bg_fragment_1"
  15. android:layout_width="150dp"
  16. android:scaleType="centerCrop"
  17. android:layout_marginTop="5dp"
  18. android:layout_height="200dp" />
  19. </LinearLayout>
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="wrap_content">
  5. <ImageView
  6. android:id="@+id/iv_img1"
  7. android:layout_width="137dp"
  8. android:layout_height="200dp"
  9. android:layout_marginTop="5dp"
  10. android:scaleType="centerCrop"
  11. android:src="@mipmap/bg_fragment_1" />
  12. <ImageView
  13. android:id="@+id/iv_img2"
  14. android:layout_width="137dp"
  15. android:layout_height="200dp"
  16. android:layout_marginTop="5dp"
  17. android:scaleType="centerCrop"
  18. android:src="@mipmap/bg_fragment_1" />
  19. <ImageView
  20. android:id="@+id/iv_img3"
  21. android:layout_width="137dp"
  22. android:layout_height="200dp"
  23. android:layout_marginTop="5dp"
  24. android:scaleType="centerCrop"
  25. android:src="@mipmap/bg_fragment_1" />
  26. </LinearLayout>

然后这个就是我们的多布局的样式了项目中的网络请求是我自己封装的一个请求,到时候可以给大家发出来

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

闽ICP备14008679号