2017-06-28 97 views
0

目前,我有這個Recyclerview用實線ItemDecoration分離的元素:Android的水平和垂直recyclerview項目裝修

current

,但希望有一個RecyclerView ItemDecoration像這樣一個樣式:

want

這是我的裝飾只有堅實的垂直和水平線,沒有像我的例子那樣的線條空間:

public class GridDividerDecoration extends RecyclerView.ItemDecoration { 

    private static final int[] ATTRS = {android.R.attr.listDivider}; 

    private Drawable mDivider; 
    private int mInsets; 

    public GridDividerDecoration(Context context) { 
     TypedArray a = context.obtainStyledAttributes(ATTRS); 
     mDivider = a.getDrawable(0); 
     a.recycle(); 

     mInsets = context.getResources().getDimensionPixelSize(R.dimen.card_insets); 
    } 

    @Override 
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { 
     drawVertical(c, parent); 
     drawHorizontal(c, parent); 
    } 

    public void drawVertical(Canvas c, RecyclerView parent) { 
     if (parent.getChildCount() == 0) return; 

     final int childCount = parent.getChildCount(); 

     for (int i = 0; i < childCount; i++) { 
      final View child = parent.getChildAt(i); 
      final RecyclerView.LayoutParams params = 
        (RecyclerView.LayoutParams) child.getLayoutParams(); 

      final int left = child.getLeft() - params.leftMargin - mInsets; 
      final int right = child.getRight() + params.rightMargin + mInsets; 
      final int top = child.getBottom() + params.bottomMargin + mInsets; 
      final int bottom = top + mDivider.getIntrinsicHeight(); 
      mDivider.setBounds(left, top, right, bottom); 
      mDivider.draw(c); 
     } 
    } 

    public void drawHorizontal(Canvas c, RecyclerView parent) { 
     final int childCount = parent.getChildCount(); 

     for (int i = 0; i < childCount; i++) { 
      final View child = parent.getChildAt(i); 
      final RecyclerView.LayoutParams params = 
        (RecyclerView.LayoutParams) child.getLayoutParams(); 

      final int left = child.getRight() + params.rightMargin + mInsets; 
      final int right = left + mDivider.getIntrinsicWidth(); 
      final int top = child.getTop() - params.topMargin - mInsets; 
      final int bottom = child.getBottom() + params.bottomMargin + mInsets; 
      mDivider.setBounds(left, top, right, bottom); 
      mDivider.draw(c); 
     } 
    } 

    @Override 
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 
     outRect.set(mInsets, mInsets, mInsets, mInsets); 
    } 
} 

任何人都可以幫助如何設置行之間的一些空格?

+0

如果您發佈了您的代碼現在和您想要實現的功能的圖像,它會更有幫助。 「設置線條之間的空間」是相當開放的解釋。 – Kuffs

+0

@Kuffs 我的代碼顯示它: http://uupload.ir/files/c10s_untitled.png 我想行 –

+0

是要把它作爲一個項目裝飾它需要之間設置空間,它可以是一個簡單的部分項目視圖? – Arpit

回答

0

你只是想稍微縮短一行,所以只需修改你的界限。

如:

對於垂直線,你必須:

mDivider.setBounds(left, top, right, bottom); 

所以只是改變了頂部和底部偏移是這樣的:

mDivider.setBounds(left, top + 10, right, bottom - 10); 

做水平類似的事情線,但改變左右偏移。

您可以嘗試實際值,直到獲得所需的效果。