2017-01-09 15 views
8

tôi đang xây dựng một ứng dụng Android đang sử dụng RecyclerView. Tôi muốn thêm ngăn để RecyclerView, mà tôi đã sử dụng mã này:Thêm lề để chia trong RecyclerView

DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(), linearLayoutManager.getOrientation()); 
recyclerView.addItemDecoration(dividerItemDecoration); 

Cho đến nay tất cả mọi thứ hoạt động tốt. Tuy nhiên, dải phân cách đang lấy kích thước toàn màn hình và tôi muốn thêm lề cho nó. Có cách nào để tôi có thể thêm lề vào bộ chia bằng cách sử dụng phương thức sẽ thêm một số không gian vào hình chữ nhật được vẽ không và bằng cách tạo một hình dạng có thể vẽ tùy chỉnh với lề và thêm nó vào RecyclerView?

+0

sử dụng tùy chỉnh dividerItemDecoration –

Trả lời

7

Sử dụng tùy chọn này và tùy chỉnh theo yêu cầu của bạn.

public class DividerItemDecoration extends RecyclerView.ItemDecoration { 

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

private Drawable divider; 

/** 
* Default divider will be used 
*/ 
public DividerItemDecoration(Context context) { 
    final TypedArray styledAttributes = context.obtainStyledAttributes(ATTRS); 
    divider = styledAttributes.getDrawable(0); 
    styledAttributes.recycle(); 
} 

/** 
* Custom divider will be used 
*/ 
public DividerItemDecoration(Context context, int resId) { 
    divider = ContextCompat.getDrawable(context, resId); 
} 

@Override 
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) { 
    int left = parent.getPaddingLeft(); 
    int right = parent.getWidth() - parent.getPaddingRight(); 

    int childCount = parent.getChildCount(); 
    for (int i = 0; i < childCount; i++) { 
     View child = parent.getChildAt(i); 

     RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams(); 

     int top = child.getBottom() + params.bottomMargin; 
     int bottom = top + divider.getIntrinsicHeight(); 

     divider.setBounds(left, top, right, bottom); 
     divider.draw(c); 
    } 
} 

}

+0

Trong trường hợp của tôi nó chỉ hiển thị một lần sau khi mục cuối cùng. – Makalele

2

Bạn có thể tạo trang trí mục riêng của bạn để xem tái chế. Đây là mã cho cùng.

public class SimpleItemDecorator extends RecyclerView.ItemDecoration { 

    int space; 
    boolean isHorizontalLayout; 
    public SimpleItemDecorator(int space) { 
     this.space = space; 
    } 

    public SimpleItemDecorator(int space, boolean isHorizontalLayout) { 
     this.space = space; 
     this.isHorizontalLayout = isHorizontalLayout; 
    } 

    @Override 
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) { 
     super.getItemOffsets(outRect, view, parent, state); 
     if(isHorizontalLayout) 
     { 
      outRect.bottom=space; 
      outRect.right=space; 
      outRect.left=space; 
      outRect.top=space; 

     } else { 
      outRect.bottom = space; 
      if (parent.getChildAdapterPosition(view) == 0) 
       outRect.top = space; 
      else 
       outRect.top = 0; 

     } 


    } 
} 

Và để sử dụng nó với recyclerview của bạn, bạn có thể làm như thế này:

recyclerView.addItemDecoration(new SimpleItemDecorator(5)); 
3

Tôi nghĩ rằng giải pháp đơn giản nhất là sử dụng phương pháp setDrawable trên đối tượng trang trí và vượt qua nó một drawable inset với các giá trị inset bạn muốn cho các lề. Cũng giống như vậy:

int[] ATTRS = new int[]{android.R.attr.listDivider}; 

TypedArray a = context.obtainStyledAttributes(ATTRS); 
Drawable divider = a.getDrawable(0); 
int inset = getResources().getDimensionPixelSize(R.dimen.your_margin_value); 
InsetDrawable insetDivider = new InsetDrawable(divider, inset, 0, inset, 0); 
a.recycle(); 

DividerItemDecoration itemDecoration = new DividerItemDecoration(context, DividerItemDecoration.VERTICAL); 
itemDecoration.setDrawable(insetDivider); 
recyclerView.addItemDecoration(itemDecoration); 
Các vấn đề liên quan