RecycleView GridLayoutManager 分割线

Posted callMeVita

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了RecycleView GridLayoutManager 分割线相关的知识,希望对你有一定的参考价值。

先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 分割线的主要内容,如果未能解决你的问题,请参考以下文章

RecycleView 点击事件

Android RecycleView自定义布局的使用

关于 RecycleView [关闭]

Android RecycleView使用详解

自定义加载更多的Recycleview

RecycleView的使用(转)