当前位置:   article > 正文

【详细】实现多布局的recyclerview_recyclerview.getitemviewtype 例子

recyclerview.getitemviewtype 例子

前言

在项目中基本上都会用到的 多种item(条目)的加载 比如大家常见的app( <网易新闻>app的新闻的列表,<新闻头条>app的新闻列表) 都是采用了加载多种item的布局。例如下图:

 

        但是我们要如何实现这种功能呢?很多人说用recycleview嵌套recycleview,但是事实上,种方法并不理想,理由是一方面recycleview嵌套会产生冲突;另一方面作为前端有时候后端的接口传值并不能满足我们两个recycleview嵌套。因此我们需要采用recycleview的多布局实现这类功能。

           看了很多例子,说其实最关键的是要复写RecyclerView的Adapter中的getItemViewType()方法 这个方法就根据条件返回条目的类型,然后onCreateViewHolder里根据viewtype来判断 所引用的item布局类型 这样每一条item就会不一样了,但是种方法不好维护。我就用了另一种方法,使用自定义View实现。

 

第一层:显示页面

1. Bean存储类型(AdapterBean)

  1. public class AdapterBean implements MultiItemEntity {
  2. //第一个布局
  3. public static final int LEARNRECORD_ONE = 1;
  4. //第二个布局
  5. public static final int LEARNRECORD_TWO = 2;
  6. //第三个布局
  7. public static final int LEARNRECORD_Three = 3;
  8. //第四个布局
  9. public static final int LEARNRECORD_FLOUR = 4;
  10. public int itemType;
  11. public LearnRecordAdapterBean(int type) {
  12. itemType = type;
  13. }
  14. @Override
  15. public int getItemType() {
  16. return itemType;
  17. }
  18. }

2. 显示页面布局 

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:background="#FFFFFFF"
  7. android:orientation="vertical">
  8. <RelativeLayout
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content">
  11. <com.scwang.smartrefresh.layout.SmartRefreshLayout
  12. android:id="@+id/refresh_layout"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. app:srlEnableLoadMore="false">
  16. <android.support.v7.widget.RecyclerView
  17. android:id="@+id/rl_learnrecord"
  18. android:layout_width="match_parent"
  19. android:layout_height="wrap_content" />
  20. </com.scwang.smartrefresh.layout.SmartRefreshLayout>
  21. </RelativeLayout>
  22. </LinearLayout>

3. recyclerview的adapter

  1. public class LearnRecordAdapter extends BaseMultiItemQuickAdapter<LearnRecordAdapterBean, BaseViewHolder> {
  2. private LearnOneView OneView;
  3. private LearnTwoView TwoView;
  4. private LearnThreeView ThreeView;
  5. private LearnFourView FourView;
  6. public LearnRecordAdapter(List<AdapterBean> data) {
  7. super(data);
  8. addItemType(AdapterBean.LEARNRECORD_ONE, R.layout.openhome_learnrecord_item_head);
  9. addItemType(AdapterBean.LEARNRECORD_TWO, R.layout.openhome_learnrecord_item_learnweek);
  10. addItemType(AdapterBean.LEARNRECORD_THREE,
  11. R.layout.openhome_learnrecord_item_errorstop);
  12. addItemType(AdapterBean.LEARNRECORD_FOUR,
  13. R.layout.openhome_learnrecord_item_errorstop)
  14. }
  15. @Override
  16. protected void convert(BaseViewHolder helper, AdapterBean item) {
  17. switch (helper.getItemViewType()){
  18. case AdapterBean.LEARNRECORD_ONE:
  19. OneView = helper.getView(R.id.headview);
  20. OneView.setData();
  21. break;
  22. case AdapterBean.LEARNRECORD_TWO:
  23. TwoView = helper.getView(R.id.learnweekview);
  24. TwoView.setData();
  25. break;
  26. case AdapterBean.LEARNRECORD_THREE:
  27. ThreeView = helper.getView(R.id.errorstopview) ;
  28. ThreeView.setData();
  29. break;
  30. case AdapterBean.LEARNRECORD_FOUR:
  31. FourView = helper.getView(R.id.errorstopview) ;
  32. FourView.setData();
  33. break
  34. default:
  35. break;
  36. }
  37. }
  38. }

 4. 显示页面activity

  1. public class LearnRecordActivity extends BaseActivity {
  2. public LearnRecordAdapter learnRecordAdapter;
  3. private RecyclerView recyclerView;
  4. private List<AdapterBean> learnRecorddata = new ArrayList<>();
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.mainactivity);
  9. initData();
  10. initAdapter();
  11. }
  12. /**
  13. * 初始化数据
  14. */
  15. private void initData(){
  16. if (learnRecorddata!=null&&learnRecorddata.size()>0){
  17. learnRecorddata.clear();
  18. }
  19. // 绑定数据
  20. learnRecorddata.add(new AdapterBean(AdapterBean.LEARNRECORD_ONE));
  21. learnRecorddata.add(new AdapterBean(AdapterBean.LEARNRECORD_TWO));
  22. learnRecorddata.add(new AdapterBean(AdapterBean.LEARNRECORD_THREE));
  23. learnRecorddata.add(new AdapterBean(AdapterBean.LEARNRECORD_FOUR));
  24. if (learnRecordAdapter != null) {
  25. learnRecordAdapter.notifyDataSetChanged();
  26. }
  27. }
  28. /**
  29. * 初始化适配器
  30. */
  31. private void initAdapter() {
  32. // recyclerview绑定adapter
  33. LinearLayoutManager manager = new LinearLayoutManager(this);
  34. manager.setOrientation(LinearLayoutManager.VERTICAL);
  35. recyclerView.setLayoutManager(manager);
  36. learnRecordAdapter = new LearnRecordAdapter(learnRecorddata);
  37. recyclerView.setAdapter(learnRecordAdapter);
  38. }
  39. }

 

第二层:各个item的布局(自定义view)

由于四个item的应用大同小意,那么这里就仅仅介绍第一个item的实现。

  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. <com.xes.cloudlearning.openhome.view.LearnRecordHeadView
  6. android:id="@+id/headview"
  7. android:layout_width="wrap_content"
  8. android:layout_height="wrap_content" />
  9. </LinearLayout>

 在这里可以看到,这里是一个自定义view,点击LearnRecordHeadView可以进去看到这个自定义view的布局。

  1. public class LearnRecordHeadView extends RelativeLayout {
  2. @BindView(R2.id.tv_practice)
  3. TextView tvPractice;
  4. @BindView(R2.id.tv_areadykeep)
  5. TextView tvAreadykeep;
  6. @BindView(R2.id.tv_countnum)
  7. TextView tvCountnum;
  8. @BindView(R2.id.tv_count)
  9. TextView tvCount;
  10. @BindView(R2.id.tv_test)
  11. TextView tvTest;
  12. @BindView(R2.id.tv_practicenum)
  13. TextView tvPracticenum;
  14. @BindView(R2.id.tv_pk)
  15. TextView tvPk;
  16. @BindView(R2.id.tv_pknum)
  17. TextView tvPknum;
  18. private Context mContext;
  19. public LearnRecordHeadView(Context context) {
  20. super(context);
  21. mContext = context;
  22. initView();
  23. }
  24. public LearnRecordHeadView(Context context, @Nullable AttributeSet attrs) {
  25. super(context, attrs);
  26. mContext = context;
  27. initView();
  28. }
  29. public LearnRecordHeadView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  30. super(context, attrs, defStyleAttr);
  31. mContext = context;
  32. initView();
  33. }
  34. private void initView() {
  35. View.inflate(mContext, R.layout.openhome_title_item_layout, this);
  36. ButterKnife.bind(this);
  37. }
  38. @SuppressLint("SetTextI18n")
  39. public void setData() {
  40. tvPractice.setText("80");
  41. tvAreadykeep.setText("XXX");
  42. tvCountnum.setText("800");
  43. tvCount.setText("XXX");
  44. tvTest.setText("XXXX");
  45. tvPracticenum.setText("XX");
  46. tvPk.setText("XX");
  47. tvPknum.setText("XXX");
  48. }
  49. }

      在这里看到这里还有一个布局,也就是这个自定义view的布局openhome_title_item_layout。点击进去看。我们在这个view中处理这里的数据和逻辑。

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="wrap_content“>
  7. <LinearLayout
  8. android:id="@+id/ll_headView"
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content">
  11. <RelativeLayout
  12. android:layout_width="0dp"
  13. android:layout_height="wrap_content"
  14. android:layout_weight="1">
  15. <LinearLayout
  16. android:layout_width="match_parent"
  17. android:layout_height="wrap_content">
  18. <TextView
  19. android:layout_width="match_parent"
  20. android:layout_height="wrap_content"
  21. android:gravity="center" />
  22. </LinearLayout>
  23. <LinearLayout
  24. android:layout_width="match_parent"
  25. android:layout_height="wrap_content
  26. android:layout_marginTop="8dp">
  27. <TextView
  28. android:layout_width="match_parent"
  29. android:layout_height="wrap_content"
  30. android:gravity="center"/>
  31. </LinearLayout>
  32. </RelativeLayout>
  33. <View
  34. android:id="@+id/tv_right_line"
  35. android:layout_width="1dp"
  36. android:layout_height="33dp"
  37. android:layout_alignParentRight="true"
  38. android:layout_marginTop="40dp"
  39. android:background="#E6E6E6"
  40. app:layout_constraintBottom_toBottomOf="parent"
  41. app:layout_constraintRight_toRightOf="parent"
  42. app:layout_constraintTop_toTopOf="parent"
  43. app:layout_constraintVertical_bias="0.5" />
  44. <RelativeLayout
  45. android:layout_width="0dp"
  46. android:layout_height="wrap_content"
  47. android:layout_weight="1">
  48. <LinearLayout
  49. android:layout_width="match_parent"
  50. android:layout_height="wrap_content">
  51. <TextView
  52. android:layout_width="match_parent"
  53. android:layout_height="wrap_content"
  54. android:gravity="center"
  55. android:singleLine="true"/>
  56. </LinearLayout>
  57. </RelativeLayout>
  58. </LinearLayout>
  59. </RelativeLayout>

         这就是完整的流程,以后一个非常复杂的页面,完全可以用一个recycleview实现。

 

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

闽ICP备14008679号