RecyclerView
嵌套在CardView
中。我尝试致电addItemDecoration()
将分频器添加到RecycylerView
,但它没有用。
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="@color/white_color"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
card_view:cardElevation="3dp">
<android.support.v4.widget.SwipeRefreshLayout
android:id="@+id/swipe_saleproduct_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_saleproduct_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:visibility="visible" />
</android.support.v4.widget.SwipeRefreshLayout>
</android.support.v7.widget.CardView>
java代码:
mSwipe = (SwipeRefreshLayout)view.findViewById(R.id.swipe_saleproduct_list);
mRecyclerView= (RecyclerView) view.findViewById(R.id.recycler_saleproduct_list);
mRecyclerView.setLayoutManager(new LinearLayoutManager(UIUtils.getContext()));
mRecyclerView.addItemDecoration(new DividerItemDecoration(UIUtils.getContext(), DividerItemDecoration.VERTICAL_LIST));
SaleProductListAdapter saleProductListAdapter = new SaleProductListAdapter(UIUtils.getContext(), mRecyclerView, list, mSwipe);
mRecyclerView.setAdapter(saleProductListAdapter);
答案 0 :(得分:6)
以下是一个很好的例子,请看一下:
<head>
这是我的public class DividerItemDecoration extends RecyclerView.ItemDecoration {
private static final int[] ATTRS = new int[]{
android.R.attr.listDivider
};
public static final int HORIZONTAL_LIST = LinearLayoutManager.HORIZONTAL;
public static final int VERTICAL_LIST = LinearLayoutManager.VERTICAL;
private Drawable mDivider;
private int mOrientation;
public DividerItemDecoration(Context context, int orientation) {
final TypedArray a = context.obtainStyledAttributes(ATTRS);
mDivider = a.getDrawable(0);
a.recycle();
setOrientation(orientation);
}
public void setOrientation(int orientation) {
if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
throw new IllegalArgumentException("invalid orientation");
}
mOrientation = orientation;
}
@Override
public void onDraw(Canvas c, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
drawVertical(c, parent);
} else {
drawHorizontal(c, parent);
}
}
public void drawVertical(Canvas c, RecyclerView parent) {
final int left = parent.getPaddingLeft();
final int right = parent.getWidth() - parent.getPaddingRight();
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 top = child.getBottom() + params.bottomMargin;
final int bottom = top + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
public void drawHorizontal(Canvas c, RecyclerView parent) {
final int top = parent.getPaddingTop();
final int bottom = parent.getHeight() - parent.getPaddingBottom();
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;
final int right = left + mDivider.getIntrinsicHeight();
mDivider.setBounds(left, top, right, bottom);
mDivider.draw(c);
}
}
@Override
public void getItemOffsets(Rect outRect, int itemPosition, RecyclerView parent) {
if (mOrientation == VERTICAL_LIST) {
outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
} else {
outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
}
}
}
,它运作良好,Fragment
清晰而薄:
divider
我希望你会受到启发。
答案 1 :(得分:1)
平台KitKat及以下版本的mDivider
系统可能NullPointerException
,因此我建议您自己使用mDivider
:
public MyDecoration(Context context, int orientation) {
mDivider = context.getResources().getDrawable(R.drawable.line_divider);
setOrientation(orientation);
}
以下是line_divider:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="1dp"
android:height="1dp" />
<solid android:color="@color/color_in_common_green" />
</shape>
它可能运作良好