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();
 
        

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM