RecycleView GridLayoutManager 分割线

摘要:
首先绘制效果图:效果如上图所示,以达到效果。为什么不使用网格视图。这可能是一个小的大脑泵,但它可能会在未来增加功能。如果满了怎么办?此任务主要通过重写dividerItemDecoration类中的ondrawover方法来完成。

先po出效果图:

RecycleView GridLayoutManager 分割线第1张

效果如上所示  就是为了达到效果(每行间有分割线 最后一排没有分割线) 

至于为什么不用 gridview。可能有点脑抽吧 ,以后可能会添加功能 如果填满了呢?(哎就是这样自我安慰)

完成这任务主要是在 dividerItemDecoration类里面 重写 ondrawover方法。

1:在fragment或者activity里 给recycleview 设置manager和decoration(自定义)

 if (mGridLayoutManager == null) {
            mGridLayoutManager = new GridLayoutManager(getContext(), 4, LinearLayoutManager.VERTICAL, false);
        }
 if (mDividerItemDecoration == null) {
          mDividerItemDecoration = new GridItemDividerDecoration(getResources().getDrawable(R.drawable.divider_underline),GridItemDividerDecoration.VERTICAL,1);}
  mRv.addItemDecoration(mDividerItemDecoration);
mRv.setLayoutManager(mGridLayoutManager);

2自定义GridItemDividerDecoration

/*给gridRecycleView 实现分割线  tip:建议recycleview的长宽模式设为wrapcontent divider的作用主要提供颜色和默认边框值  均可自己设置
* */
public class GridItemDividerDecoration extends RecyclerView.ItemDecoration {
    private  int mRedundant;
    public static  final  int VERTICAL =1;
    public static final int Horizon =0;
    private  Drawable mDivider;
    private int mThickness;
    private int mOratation;


    public GridItemDividerDecoration(Drawable divider, int orantation) {
        this.mDivider =divider;
        this.mOratation =orantation;
        setThickness();
    }

    public GridItemDividerDecoration(Drawable divider, int orantation,int thick) {//亲测横向纵向都可以的
        this.mDivider =divider;
        this.mOratation =orantation;
        setThickness(thick);
    }

    private void setThickness(){
        if (mOratation==VERTICAL){
           mThickness= mDivider.getIntrinsicHeight();
        }else if (mOratation==Horizon){
          mThickness=mDivider.getIntrinsicWidth();
        }
    }

    private void setThickness(int thickness){//用于自己设置分割线宽度
       mThickness = thickness;
    }


    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
       int spancount= ((GridLayoutManager)parent.getLayoutManager()).getSpanCount();
        mRedundant = parent.getChildCount()%spancount;
        mRedundant = mRedundant==0?spancount:mRedundant;//最后一排的item个数
        final int underlineNum = parent.getChildCount()-mRedundant;//下划线的child的个数
        final Drawable divider = mDivider;
        final int thickness = mThickness;
        for (int i = 0;i<underlineNum;i++) {//给非最后一排的item画边边
            View child = parent.getChildAt(i);
            if (mOratation == VERTICAL) {
                divider.setBounds(child.getLeft(), child.getBottom(), child.getRight(), child.getBottom() + thickness);
            }
            if (mOratation == Horizon) {
                divider.setBounds(child.getRight(),child.getTop(), child.getRight() + thickness, child.getBottom());
            }
            divider.draw(c);
        }

    }
}

记一下: setBounds(left,top,right,bottom)参数是要画的东西的绝对位置.

另外:学会了一个获取child的position的方法

int iPos = ((RecyclerView.LayoutParams) recycleview.getLayoutParams()).getViewLayoutPosition();

免责声明:文章转载自《RecycleView GridLayoutManager 分割线》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇nssm使用,安装服务、删除服务[轉]redis;mongodb;memcache三者的性能比較下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

RecyclerView的万能适配器+定义可以到底部自动刷新的RecyclerView

RecyclerView的重要性就不做重复说明了,为了方便以后直接使用写了这个,主要有:   万能适配器在使用的时候分为定义布局和绑定数据,方便直接套用。加入了底部刷新,需要配合自己写的RecyclerView一起使用,对于布局中各个子布局和控件可以做到响应各种点击事件:     首先抽取ViewHolder:这里的要点是用到了SparseArray(源码...

Android RecyclerView 快速平滑返回顶部

先看下实现的效果,没效果什么都白扯 下面直接上方法: //目标项是否在最后一个可见项之后 private booleanmShouldScroll; //记录目标项位置 private intmToPosition; //目标项是否在最后一个可见项之后 private boolean mShouldScroll; //记录目标项位...

安卓Design包之CoordinatorLayout配合AppBarLayout,ToolBar,TabLaout的使用

转载:  CoordinatorLayout配合AppBarLayout,Toolbar和TabLayout的使用控件的简单介绍: AppBarLayout:它是继承LinerLayout实现的一个ViewGroup容器组件,是为了Material Design而设计的App Bar,支持手势滑动操作。 默认的AppBarLayout是垂直方向的,它的作用...

RecyclerView导入依赖包

1. eclipse 上的导入: 如下进入Android SDK的如下路径, android-sdkextrasandroidm2repositorycomandroidsupport ecyclerview-v7 找到和自己当前android api版本相同的目录,这里以21.0.0为例: 进入21.0.0命名的目录,找到recyclerview-v7...

一个漂亮而强大的RecyclerView

代码地址如下:http://www.demodashi.com/demo/13470.html 简介 主要提供了简单易用强大的RecyclerView库,包括自定义刷新加载效果、极简通用的万能适配器Adapter、万能分割线、多种分组效果、常见状态页面、item动画效果、添加多个header和footer、侧滑、拖拽、Sticky(黏性)效果、多item布...

android RecyclerView

引用 在项目的build.gradle添加依赖 compile 'com.android.support:recyclerview-v7:23.4.0' RecyclierView使用的基本方法 recyclerView.setAdapter(); 添加适配器(必须) recyclerView.setLayoutManager(); 选择一种布局(必须)...