码迷,mamicode.com
首页 > 其他好文 > 详细

RecycleView GridLayoutManager 分割线

时间:2017-08-24 17:54:42      阅读:340      评论:0      收藏:0      [点我收藏+]

标签:cycle   xtend   ondraw   item   recycle   drawable   分割   on()   style   

先po出效果图:

技术分享

 

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

至于为什么不用 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 分割线

标签:cycle   xtend   ondraw   item   recycle   drawable   分割   on()   style   

原文地址:http://www.cnblogs.com/vitabebeauty/p/7424067.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!