DividerItemDecoration水平不出现在中心

时间:2017-12-05 11:05:42

标签: java android android-recyclerview android-support-library item-decoration

我正在使用支持库中的DividerItemDecoration。

以下是添加装饰的代码

 GridLayoutManager gridLayoutManager = new GridLayoutManager(context, 3);
 recyclerView.setLayoutManager(gridLayoutManager);
 recyclerView.addItemDecoration(new DividerItemDecoration(context, DividerItemDecoration.HORIZONTAL));

但是分隔线并没有横向居中。

以下是它的屏幕截图。

enter image description here

以下是项目布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:clickable="true"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="8dp"
android:paddingTop="8dp">


<ImageView
    android:id="@+id/imgFacility"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_gravity="center"
    android:foreground="@drawable/page_main_category_home_grid_selector"
    android:gravity="center"
    android:src="@drawable/plane_img"
    android:tint="?colorAccent" />


<TextView
    android:id="@+id/txtFacilityName"
    mediumFontPath="fonts/montserrat-medium.ttf"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="2dp"
    android:ellipsize="end"
    android:gravity="center"
    android:maxLines="2"
    android:minLines="2"
    android:text="Facility\nEWlall"
    android:textColor="#353535"
    android:textSize="12sp"
    tools:ignore="MissingPrefix" />

 </LinearLayout>

我也尝试使用GridDividerDecoration

以下是输出 enter image description here

给出每个布局的背景颜色,这就是它的外观

enter image description here

1 个答案:

答案 0 :(得分:0)

首先使用以下代码创建类

public class DividerItemDecoration extends RecyclerView.ItemDecoration {

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

private Drawable mDivider;
private int mInsets;

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

    mInsets = 1;
}

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

/**
 * Draw dividers at each expected grid interval
 */
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);
    }
}

/**
 * Draw dividers to the right of each child view
 */
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) {
    //We can supply forced insets for each item view here in the Rect
    outRect.set(mInsets, mInsets, mInsets, mInsets);
}

}

在此之后使用此recyclarview中的装饰类

recyclerView.addItemDecoration(new DividerItemDecoration(getActivity()));

有助于显示项目之间的垂直和水平线。