由于recyclerview默认是没有分割线的,需要显示分割线的话,可以在布局里添加一条有背景色的View标签,或者通过ItemDecoration来实现,本文以后者为例。

ItemDecoration里有2个重要的方法,onDraw和getItemOffsets。

class MyItemDecoration extends RecyclerView.ItemDecoration {
private int mOrientation = LinearLayout.VERTICAL;
private Drawable mDivider; MyItemDecoration(Context context) {
int[] rids = new int[]{android.R.attr.listDivider};//系统自带的属性,宽高各2dp
TypedArray typedArray = context.obtainStyledAttributes(rids);
mDivider = typedArray.getDrawable(0);
typedArray.recycle();
} void setOrientation(int orientation) {
mOrientation = orientation;
} int getOrientation() {
return mOrientation;
} @Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
super.onDraw(c, parent, state);
((LinearLayoutManager) parent.getLayoutManager()).setOrientation(mOrientation);
if (mDivider != null) {
//需要传入画布和当前recyclerview
if (mOrientation == LinearLayout.HORIZONTAL) {
drawHorizontal(c, parent);
} else {
drawVertical(c, parent);
}
}
} //绘制竖直方向的横线
private void drawVertical(Canvas c, RecyclerView parent) {
int left = parent.getPaddingLeft();//横线左右随父容器
int right = parent.getWidth() - parent.getPaddingRight();
for (int i = 0; i < parent.getChildCount(); i++) {
//遍历recyclerview的每个子view
View child = parent.getChildAt(i);
//分割线的top,left,right,bottom
int top = child.getBottom();
int bottom = child.getBottom() + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);//设置分割线的区域
mDivider.draw(c);//绘制到画布
}
} //绘制水平方向的竖线
private void drawHorizontal(Canvas c, RecyclerView parent) {
int top = parent.getPaddingTop();//竖线上下随父容器
int bottom = parent.getBottom();
for (int i = 0; i < parent.getChildCount(); i++) {
//遍历recyclerview的每个子view
View child = parent.getChildAt(i);
//分割线的top,left,right,bottom
int left = child.getRight();
Log.d("TAG", "LEFT:" + left);
int right = left + mDivider.getIntrinsicWidth();
mDivider.setBounds(left, top, right, bottom);//设置分割线的区域
mDivider.draw(c);//绘制到画布
}
} @Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State
state) {
super.getItemOffsets(outRect, view, parent, state);
//item之间的位移量
if (mOrientation == LinearLayout.HORIZONTAL) {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
} else {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
}
}
}

布局文件activity_main

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.jogger.animatorpathdemo.MainActivity"> <Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"/> <Button
android:id="@+id/btn_reduce"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="移除"/> <Button
android:id="@+id/btn_change"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="切换orientation"/> <android.support.v7.widget.RecyclerView
android:id="@+id/rv_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private RecyclerView rv_content;
private MyAdapter mAdapter;
private Button btn_add;
private Button btn_reduce;
private Button btn_change;
private int mPos;
private MyItemDecoration mMyItemDecoration; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv_content = (RecyclerView) findViewById(R.id.rv_content);
mAdapter = new MyAdapter(this);
rv_content.setLayoutManager(new LinearLayoutManager(this));
rv_content.setAdapter(mAdapter);
rv_content.setItemAnimator(new DefaultItemAnimator());//默认添加动画
mMyItemDecoration = new MyItemDecoration(this);
mMyItemDecoration.setOrientation(LinearLayout.HORIZONTAL);//设置方向
rv_content.addItemDecoration(mMyItemDecoration);
btn_add = (Button) findViewById(R.id.btn_add);
btn_reduce = (Button) findViewById(R.id.btn_reduce);
btn_change = (Button) findViewById(R.id.btn_change);
btn_add.setOnClickListener(this);
btn_reduce.setOnClickListener(this);
btn_change.setOnClickListener(this);
} @Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_add:
mAdapter.addItem(mPos);
mPos++;
if (mPos > 50) {
mPos = 50;
}
break;
case R.id.btn_reduce:
mPos--;
if (mPos < 0) {
mPos = 0;
}
mAdapter.removeItem(mPos);
break;
case R.id.btn_change:
if (mMyItemDecoration.getOrientation() == LinearLayout.HORIZONTAL) {
mMyItemDecoration.setOrientation(LinearLayout.VERTICAL);//设置方向
} else {
mMyItemDecoration.setOrientation(LinearLayout.HORIZONTAL);//设置方向
}
rv_content.addItemDecoration(mMyItemDecoration);
break;
}
}
}

适配器MyAdapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private Context mContext;
private List<String> mList; public MyAdapter(Context context) {
mContext = context;
mList = new ArrayList<>();
} public void addItem(int pos) {
mList.add(pos, "android--->" + pos);
notifyItemInserted(pos);
} public void removeItem(int pos) {
mList.remove(pos);
notifyItemChanged(pos);
} @Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.rv_item, parent,
false));
} @Override
public void onBindViewHolder(MyViewHolder holder, int position) {
holder.tv_content.setText(mList.get(position));
} @Override
public int getItemCount() {
return mList.size();
} class MyViewHolder extends RecyclerView.ViewHolder {
TextView tv_content; MyViewHolder(View itemView) {
super(itemView);
tv_content = (TextView) itemView.findViewById(R.id.tv_content);
}
}
}

RecyclerView线性分割线的更多相关文章

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

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

  2. 实现RecyclerView下拉刷新和上拉加载更多以及RecyclerView线性、网格、瀑布流效果演示

    实现RecyclerView下拉刷新和上拉加载更多以及RecyclerView线性.网格.瀑布流效果演示 效果预览 实例APP 小米应用商店 使用方法 build.gradle文件 dependenc ...

  3. RecyclerView 添加自定义分割线

    默认的浅灰色的分割线在某些时候并不能满足我们的要求,这时就需要自定义分割线了. 我们可以通过两种方式来实现:调用 DividerItemDecoration.setDrawable 方法或者继承实现 ...

  4. RecyclerView添加分割线

    mRecyclerView = findView(R.id.id_recyclerview); //设置布局管理器 mRecyclerView.setLayoutManager(layout); // ...

  5. Android零基础入门第65节:RecyclerView分割线开发技巧

    在上一期通过简单学习,已经领略到了RecyclerView的灵活性,当然都是一些最基础的用法,那么本期一起来学习RecyclerView的分割线使用. 相信有的比较细心的同学已经发现了,使用Recyc ...

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

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

  7. Android 5.X新特性之RecyclerView基本解析及无限复用

    说到RecyclerView,相信大家都不陌生,它是我们经典级ListView的升级版,升级后的RecyclerView展现了极大的灵活性.同时内部直接封装了ViewHolder,不用我们自己定义Vi ...

  8. Android开发之漫漫长途 XIV——RecyclerView

    该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列.该系列引用了<Android开发艺术探索>以及<深入理解And ...

  9. 安卓高级3 RecyclerView 和cardView使用案例

    cardView: 添加依赖:在Studio搜索cardview即可 在V7包中 或者直接在gradle中添加 compile 'com.android.support:cardview-v7:24. ...

随机推荐

  1. Python Tkinter学习(1)——第一个Tkinter程序

    注:本文可转载,转载请注明出处:http://www.cnblogs.com/collectionne/p/6885066.html.格式修改未完成. Tkinter资料 Python Wiki, T ...

  2. 关于vue2用vue-cli搭建环境后域名代理的http-proxy-middleware

    在vue中用http-proxy-middleware来进行接口代理,比如:本地运行环境为http://localhost:8080但真实访问的api为 http://www.baidu.com这时我 ...

  3. 【JAVAWEB学习笔记】网上商城实战2:异步加载分类、Redis缓存分类和显示商品

    网上商城实战2 今日任务 完成分类模块的功能 完成商品模块的功能 1.1      分类模块的功能: 1.1.1    查询分类的功能: 1.1.2    查询分类的代码实现: 1.1.2.1  创建 ...

  4. xcode8.3 shell 自动打包脚本 记录

    题记 xcode升级8.3后发现之前所用的xcode自动打包基本无法使用,因此在网上零碎找到些资料,将之前的脚本简化.此次脚本是基于xcode证书配置进行打包(之前是指定描述文件.相对繁琐).因此代码 ...

  5. 如何创建一个一流的SDK?

    怎么样的SDK算是一个好的SDK? 在做SDK的过程中我们走过非常多的弯路,是一个难以想象的学习过程,我们总结一个好的SDK应该具备的特质: 易用性,稳定性,轻量,灵活,优秀的支持. 一.易用性 因为 ...

  6. js创建对象的多种方式及优缺点

    在js中,如果你想输入一个的信息,例如姓名,性别,年龄等,如果你用值类型来存储的话,那么你就必须要声明很多个变量才行,变量声明的多了的话,就会造成变量污染.所以最好的方式就是存储到对象中.下面能我就给 ...

  7. HTML面试题

    1.你做的页面在哪些流览器测试过?这些浏览器的内核分别是什么? 所谓的“浏览器内核”无非指的是一个浏览器最核心的部分-“Rendering Engine”,直译叫做“渲染引擎”,我们也常称为“排版引擎 ...

  8. 11.并发包阻塞队列之LinkedBlockingQueue

    在上文<10.并发包阻塞队列之ArrayBlockingQueue>中简要解析了ArrayBlockingQueue部分源码,在本文中同样要介绍的是Java并发包中的阻塞队列LinkedB ...

  9. jQuery 插件 的this 指向问题(实战)

    daterangepicker 是一个JavaScript组件,用来选择日期. 资源直接搜索 daterangepicker 即可,当然好看的样式可以基于Bootstrap. 官网:http://ww ...

  10. angularjs应用prerender.io 搜索引擎优化实践

    上一篇博文(http://www.cnblogs.com/ideal-lx/p/5625428.html)介绍了单页面搜索引擎优化的原理,以及介绍了两个开源框架的优劣.prerender框架的工作原理 ...