• 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();
    
    
    
  • 相关阅读:
    C#线程同步(1)- 临界区&Lock
    详细解析Java中抽象类和接口的区别
    防止重复提交的几种办法
    网页中实现JSON的编辑与显示
    xcode5 ios7升级后的一系列问题解决
    hadoop-2.0.0-mr1-cdh4.2.0源码编译总结
    hadoop-2.0.0-cdh4.2.1源码编译总结
    cocos2d-iphone加入芒果广告
    hadoop2.0 eclipse 源码编译
    HBase学习笔记
  • 原文地址:https://www.cnblogs.com/vitabebeauty/p/7424067.html
Copyright © 2020-2023  润新知