如何使RecyclerView项目分割器响应不同的屏幕?

时间:2017-08-06 14:41:39

标签: java android responsive-design responsive

响应问题

我正在使用一个名为DividerItemDecoration的类,它用于在recyclerview项目之间进行划分,我使用胶水代码实现左右边距,我认为这不会像其他设备(例如平板电脑)或其他设备那样正常工作更高的屏幕 这是我的代码:

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);
    mDivider.setColorFilter( 0xC1D0D7, PorterDuff.Mode.SRC_OVER);
}

public void setOrientation(int orientation) {
    if (orientation != HORIZONTAL_LIST && orientation != VERTICAL_LIST) {
        throw new IllegalArgumentException("invalid orientation");
    }
    mOrientation = orientation;
}

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

public void drawVertical(Canvas c, RecyclerView parent) {
    final int leftMargin=100;
    final int rightMargin=250;
    final int left = parent.getPaddingLeft();
    final int right = parent.getWidth() - parent.getPaddingRight();

    final int childCount = parent.getChildCount();
    for (int i = 0; i < childCount-1; 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+leftMargin, top, right-rightMargin, 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() +params.leftMargin;
        mDivider.setBounds(left, top, right, bottom);
        mDivider.draw(c);
    }
}

@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    if (mOrientation == VERTICAL_LIST) {
        outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
    } else {
        outRect.set(0, 0, mDivider.getIntrinsicWidth(), 0);
    }
}
}

1 个答案:

答案 0 :(得分:0)

您可以在与布局类似的不同布局文件夹中使用不同的尺寸值进行填充。

例如,

res/values/文件夹下:

<resources>
    <dimen name="divider_padding">16dp</dimen>
</resources>

res/values-sw600dp/文件夹下:

<resources>
    <dimen name="divider_padding">20dp</dimen>
</resources>

然后在运行时使用

引用它
int padding = context.getResources().getDimensionPixelSize(R.dimen.divider_padding);

你将获得16dp的手机和20dp的平板电脑,最小宽度为600dp。