由于Recyclerview是在 android.support.v7.widget.包 RecyclerView,所以需要导Recycler库:

导Recycler库:

选择项目,右键--> 

点击+图标:

选择 Library dependency:

输入 com.android.support:recyclerview ,进行搜索,看有什么RecyclerView版本可以用

敲回车搜索之后的结果是:com.android.support:recyclerview-v7 ............

点击OK即可:

就导入成功了,然后在点击即可:



RecyclerView默认是没有分割线的:

给RecyclerView增加 第一种(默认)分割线:

  1.      /**
  2. * DividerItemDecoration是android.support.v7.widget包中的
  3. * new DividerItemDecoration(this, DividerItemDecoration.VERTICAL); 这样写有一个默认的分割线
  4. */
  5. DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
  6. recyclerView.addItemDecoration(dividerItemDecoration);

DividerItemDecoration 默认分割线的效果:


给RecyclerView增加 第二种 自定义分割线

添加自定义的分割线,需要在drawable文件夹下/添加divider_bg.xml:

divider_bg.xml:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle" >
  4.  
  5. <gradient
  6. android:centerColor="#ff00ff00"
  7. android:endColor="#ff0000ff"
  8. android:startColor="#ffff0000"
  9. android:type="linear" />
  10. <size android:height="2dp"/>
  11.  
  12. </shape>

 在Activity>>recyclerView中设置引入divider_bg.xml

  1.      /**
  2. * DividerItemDecoration是android.support.v7.widget包中的
  3. * new DividerItemDecoration(this, DividerItemDecoration.VERTICAL); 这样写有一个默认的分割线 第一种分割线
  4. */
  5. DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
  6. /**
  7. * 第二种分割线 自定义 divider_bg
  8. */
  9. dividerItemDecoration.setDrawable(getResources().getDrawable(R.drawable.divider_bg));
  10. recyclerView.addItemDecoration(dividerItemDecoration);

 显示效果:


完整代码:

1.Activity:

  1. package com.example.myapplication.recycler3;
  2.  
  3. import android.app.Activity;
  4. import android.os.Bundle;
  5. import android.support.v7.widget.DividerItemDecoration;
  6. import android.support.v7.widget.LinearLayoutManager;
  7. import android.support.v7.widget.RecyclerView;
  8.  
  9. import com.example.myapplication.R;
  10.  
  11. import java.util.ArrayList;
  12. import java.util.List;
  13.  
  14. public class MyRecyclerActiviy extends Activity {
  15.  
  16. @Override
  17. protected void onCreate(Bundle savedInstanceState) {
  18. super.onCreate(savedInstanceState);
  19. setContentView(R.layout.activity_recycler);
  20.  
  21. // 引入布局的RecyclerView,初始化RecyclerView 可以理解为ListView
  22. RecyclerView recyclerView = findViewById(R.id.recycler_view);
  23.  
  24. /**
  25. * 实例化适配器
  26. * RecyclerView需要的适配器是RecyclerView.Adapter adapter
  27. */
  28. MyRecyclerAdapter myRecyclerAdapter = new MyRecyclerAdapter(this, getPersons());
  29.  
  30. /**
  31. * 设置适配器,可以理解为ListView设置适配器
  32. * 不同的是 RecyclerView需要的适配器是RecyclerView.Adapter adapter
  33. */
  34. recyclerView.setAdapter(myRecyclerAdapter);
  35.  
  36. /**
  37. * 注意:以上步骤 还不能显示数据,还是一片显示空白 空白,为何空白 因为都没有去执行MyRecyclerAdpater的代码
  38. * 需要设置以下代码setLayoutManager,才能显示
  39. */
  40. LinearLayoutManager manager =
  41. new LinearLayoutManager(MyRecyclerActiviy.this, // 参数一:上下文环境
  42. LinearLayoutManager.VERTICAL, // 参数二:显示方向 垂直
  43. false); // 参数三:是否倒序
  44. recyclerView.setLayoutManager(manager);
  45.  
  46. /**
  47. * DividerItemDecoration是android.support.v7.widget包中的
  48. * new DividerItemDecoration(this, DividerItemDecoration.VERTICAL); 这样写有一个默认的分割线 第一种分割线
  49. */
  50. DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(this, DividerItemDecoration.VERTICAL);
  51. /**
  52. * 第二种分割线 自定义 divider_bg
  53. */
  54. dividerItemDecoration.setDrawable(getResources().getDrawable(R.drawable.divider_bg));
  55. recyclerView.addItemDecoration(dividerItemDecoration);
  56. }
  57.  
  58. /**
  59. * 此方法模拟数据
  60. * @return
  61. */
  62. private List<Person> getPersons() {
  63. List<Person> persons = new ArrayList<>();
  64. for (int i = 0; i < 100; i++) {
  65. persons.add(new Person("李元霸" + i, "先锋" + i, 18 + i));
  66. }
  67. return persons;
  68. }
  69. }

2.Activity的布局文件:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent">
  5.  
  6. <android.support.v7.widget.RecyclerView
  7. android:id="@+id/recycler_view"
  8. android:layout_width="match_parent"
  9. android:layout_height="match_parent" />
  10.  
  11. </RelativeLayout>

3.自定义分割线的 drawable/divider_bg.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <shape xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle" >
  4.  
  5. <gradient
  6. android:centerColor="#ff00ff00"
  7. android:endColor="#ff0000ff"
  8. android:startColor="#ffff0000"
  9. android:type="linear" />
  10. <size android:height="2dp"/>
  11.  
  12. </shape>

4.RecyclerView需要的适配器,MyRecyclerAdapter:

  1. package com.example.myapplication.recycler3;
  2.  
  3. import android.content.Context;
  4. import android.support.annotation.NonNull;
  5. import android.support.v7.widget.RecyclerView;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.TextView;
  10.  
  11. import com.example.myapplication.R;
  12.  
  13. import java.util.List;
  14.  
  15. /**
  16. * RecyclerView需要的适配器 >>> RecyclerView.Adapter<VH>
  17. * VH就是:RecyclerView.ViewHolder
  18. * VH全程:View-Holder
  19. */
  20. public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter.MyViewHolder> {
  21.  
  22. private LayoutInflater layoutInflater; // 布局加载器 用于加载 Item Layout
  23. private List<Person> persons; // 需要显示的数据
  24.  
  25. /**
  26. * 定义此构造方法 是为了接收数据 和 Context
  27. */
  28. public MyRecyclerAdapter(Context context, List<Person> persons) {
  29. layoutInflater = LayoutInflater.from(context);
  30. this.persons = persons;
  31. }
  32.  
  33. /**
  34. * 此方法相当于 ListView-MyBaseAdapte中的getView()方法的 创建View 、 创建ViewHolder
  35. * @param viewGroup
  36. * @param i
  37. * @return
  38. */
  39. @NonNull
  40. @Override
  41. public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
  42. View itemView = layoutInflater.inflate(R.layout.recycler_item_layout, null);
  43. return new MyViewHolder(itemView);
  44. }
  45.  
  46. /**
  47. * 此方法作用是:ItemLayout里面的View 和数据 进行绑定操作
  48. * 可以理解为:以前ListView-MyBaseAdapter-getView()方法里面的--ItemLayout里面的View 和数据 进行绑定操作
  49. * @param myViewHolder
  50. * @param index
  51. */
  52. @Override
  53. public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int index) {
  54. /**
  55. * 第一步:得到数据,数据集合.get(i、position、index ...);
  56. */
  57. Person person = persons.get(index);
  58.  
  59. /**
  60. * 第二步:把数据设置到>>>> MyViewHolder 的 Item Layout 控件中去
  61. */
  62. myViewHolder.tvName.setText(person.getName());
  63. myViewHolder.tvWork.setText(person.getWork());
  64. myViewHolder.tvAge.setText(person.getAge() + "");
  65. }
  66.  
  67. /**
  68. * 此方法是返回Item的总数
  69. * 可以理解为ListView-MyBaseAdapter 中的 public int getCount() {} 方法
  70. * @return
  71. */
  72. @Override
  73. public int getItemCount() {
  74. return persons.size(); // 返回数据的总大小
  75. }
  76.  
  77. /**
  78. * 描述MyViewHolder,可以想象ListView-MyBaseAdapter中也有ViewHolder、
  79. * 此MyViewHolder,是RecyclerView.ViewHolder的子类
  80. */
  81. class MyViewHolder extends RecyclerView.ViewHolder {
  82.  
  83. private TextView tvName;
  84. private TextView tvWork;
  85. private TextView tvAge;
  86.  
  87. /**
  88. * 此构造方法接收一个View,此View是 onCreateViewHolder方法中传递过来的View->Item
  89. * @param itemView
  90. */
  91. public MyViewHolder(@NonNull View itemView) {
  92. super(itemView);
  93.  
  94. tvName = itemView.findViewById(R.id.tv_name);
  95. tvWork = itemView.findViewById(R.id.tv_work);
  96. tvAge = itemView.findViewById(R.id.tv_age);
  97. }
  98. }
  99. }

5.MyRecyclerViewAdapter适配器的 Item Layout 文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:padding="10dp">
  7.  
  8. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:padding="10dp">
  12.  
  13. <TextView
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="姓名"
  17. android:layout_marginLeft="20dp"
  18. />
  19.  
  20. <TextView
  21. android:id="@+id/tv_name"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"
  24. android:layout_marginLeft="20dp"
  25. android:textColor="@android:color/black"
  26. />
  27.  
  28. </LinearLayout>
  29.  
  30. <LinearLayout
  31. android:layout_width="match_parent"
  32. android:layout_height="wrap_content"
  33. android:padding="10dp">
  34.  
  35. <TextView
  36. android:layout_width="wrap_content"
  37. android:layout_height="wrap_content"
  38. android:text="职位"
  39. android:layout_marginLeft="20dp"
  40. />
  41.  
  42. <TextView
  43. android:id="@+id/tv_work"
  44. android:layout_width="wrap_content"
  45. android:layout_height="wrap_content"
  46. android:layout_marginLeft="20dp"
  47. android:textColor="@android:color/black"
  48. />
  49.  
  50. </LinearLayout>
  51.  
  52. <LinearLayout
  53. android:layout_width="match_parent"
  54. android:layout_height="wrap_content"
  55. android:padding="10dp">
  56.  
  57. <TextView
  58. android:layout_width="wrap_content"
  59. android:layout_height="wrap_content"
  60. android:layout_marginLeft="20dp"
  61. android:text="年龄" />
  62.  
  63. <TextView
  64. android:id="@+id/tv_age"
  65. android:layout_width="wrap_content"
  66. android:layout_height="wrap_content"
  67. android:layout_marginLeft="20dp"
  68. android:textColor="@android:color/black" />
  69.  
  70. </LinearLayout>
  71.  
  72. </LinearLayout>

6.数据 Bean Person:

  1. package com.example.myapplication.recycler3;
  2.  
  3. public class Person {
  4.  
  5. private String name;
  6. private String work;
  7. private int age;
  8.  
  9. public Person(String name, String work, int age) {
  10. this.name = name;
  11. this.work = work;
  12. this.age = age;
  13. }
  14.  
  15. public Person() {
  16. }
  17.  
  18. public String getName() {
  19. return name;
  20. }
  21.  
  22. public void setName(String name) {
  23. this.name = name;
  24. }
  25.  
  26. public String getWork() {
  27. return work;
  28. }
  29.  
  30. public void setWork(String work) {
  31. this.work = work;
  32. }
  33.  
  34. public int getAge() {
  35. return age;
  36. }
  37.  
  38. public void setAge(int age) {
  39. this.age = age;
  40. }
  41.  
  42. @Override
  43. public String toString() {
  44. return "Person{" +
  45. "name='" + name + '\'' +
  46. ", work='" + work + '\'' +
  47. ", age=" + age +
  48. '}';
  49. }
  50. }

Android-Recyclerview-使用分割线的更多相关文章

  1. Android RecyclerView(瀑布流)水平/垂直方向分割线

     Android RecyclerView(瀑布流)水平/垂直方向分割线 Android RecyclerView不像过去的ListView那样随意的设置水平方向的分割线,如果要实现Recycle ...

  2. 自定义RecyclerView.ItemDecoration,实现RecyclerView的分割线效果

    [转] 原文 自定义RecyclerView.ItemDecoration,实现RecyclerView的分割线效果 字数1598 阅读302 评论2 喜欢23 1.背景   RecyclerView ...

  3. Android RecyclerView 使用完全解析 体验艺术般的控件

    概述 RecyclerView出现已经有一段时间了,相信大家肯定不陌生了,大家可以通过导入support-v7对其进行使用. 据官方的介绍,该控件用于在有限的窗口中展示大量数据集,其实这样功能的控件我 ...

  4. Android RecyclerView的基本使用

    Android RecyclerView 在去年的Google I/O大会上就推出来了,以前经常使用的ListView 继承的是AbsListView,而RecyclerView则直接继承 ViewG ...

  5. Android RecyclerView 使用完全解析

    概述 RecyclerView出现已经有一段时间了,相信大家肯定不陌生了,大家可以通过导入support-v7对其进行使用. 据官方的介绍,该控件用于在有限的窗口中展示大量数据集,其实这样功能的控件我 ...

  6. 【转载】Android RecyclerView 使用完全解析 体验艺术般的控件

    崇拜下鸿洋大神,原文地址:http://blog.csdn.net/lmj623565791/article/details/45059587 概述 RecyclerView出现已经有一段时间了,相信 ...

  7. Android RecyclerView (一) 使用完全解析

    转载请标明出处: http://blog.csdn.net/lmj623565791/article/details/45059587: 本文出自:[张鸿洋的博客] 概述 RecyclerView出现 ...

  8. Android RecyclerView组件和 Spinner(下拉列表框)

    1.RecyclerView <1>知识点介绍 RecyclerView 比 ListView 更高级且更具灵活性. 它是一个用于显示庞大数据集的容器,可通过保持有限数量的视图进行非常有效 ...

  9. (转载) Android RecyclerView 使用完全解析 体验艺术般的控件

    Android RecyclerView 使用完全解析 体验艺术般的控件 标签: Recyclerviewpager瀑布流 2015-04-16 09:07 721474人阅读 评论(458) 收藏  ...

  10. Android RecyclerView 实现支付宝首页效果

    Android RecyclerView 实现支付宝首页效果 [TOC] 虽然我本人不喜欢支付宝的,但是这个网格本身其实还是不错的,项目更新中更改了一个布局为网格模式,类似支付宝.(估计是产品抄袭的= ...

随机推荐

  1. Ubuntu下安装VS code

    sudo add-apt-repository ppa:ubuntu-desktop/ubuntu-make sudo apt-get update sudo apt-get install ubun ...

  2. 20172306《Java程序设计与数据结构》第九周学习总结

    20172306<Java程序设计>第九周学习总结 教材学习内容总结 第十一章: try-catch语句.其中还有finally语句.try是进行某些操作,catch是捕获异常,并通过某些 ...

  3. Codeforces 782C. Andryusha and Colored Balloons 搜索

    C. Andryusha and Colored Balloons time limit per test:2 seconds memory limit per test:256 megabytes ...

  4. MVC 模式和模型 2

    MVC框架 一个实现 MVC 模式的应用包含模型.视图.控制器 3 个模块: 模型:封装了应用的数据和业务逻辑,负责管理系统业务数据 视图:负责应用的展示 控制器:负责与用户进行交互,接收用户输入.改 ...

  5. Mac OS X Git安装教程

    http://code.google.com/p/git-osx-installer上也提供了一个Git的图形化客户端:OpenInGitGui,可以从这里获得,OpenInGitGui十分小巧,下载 ...

  6. http://itellyou.cn/

    http://itellyou.cn/ 这里提供了微软MSDN上所有能下载的软件. 下载完记得检验. 这是更详细的介绍:http://wenku.baidu.com/link?url=_dZ0mYvl ...

  7. spring学习 五 依赖注入的方式

    依赖注入有两种方式: 1 构造注入,如果<bean>标签下使用<contructor-arg>,则是构造注入 2 setter注入,就是调用setter方法注入,如果<b ...

  8. spring学习 四 对象的创建

    spring中,有三种创建对象的方式 (1)构造创建 (2)实例工厂构造 (3)静态工厂构造 一  构造器创建 在构造器创建对象时,有无参构造和有参构造 两种 (1)在spring中,默认的是无参构造 ...

  9. Mathematics | Mean, Variance and Standard Deviation

    Mean is average of a given set of data. Let us consider below example These eight data points have t ...

  10. 2019.01.21 bzoj3674: 可持久化并查集加强版(主席树+并查集)

    传送门 题意:维护可持久化并查集,支持在某个版本连边,回到某个版本,在某个版本 询问连通性. 思路: 我们用主席树维护并查集fafafa数组,由于要查询历史版本,因此不能够用路径压缩. 可以考虑另外一 ...