android – 如何跳过RecyclerView中的第一行GridLayout的项目装饰?

前端之家收集整理的这篇文章主要介绍了android – 如何跳过RecyclerView中的第一行GridLayout的项目装饰?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在GridLayout中使用这个ItemDecoration类 – > https://github.com/devunwired/recyclerview-playground/blob/master/app/src/main/java/com/example/android/recyclerplayground/GridDividerDecoration.java

但是问题是,GridLayout中的第一行是一个图像,我把span设置为2.

您可以按照以下屏幕截图查看我的屏幕:

如何跳过第一行,使ItemDecoration不会在图像上绘制?

以下是我用来添加ItemDecoration的代码: –

mRecyclerView.setHasFixedSize(true);
mLayoutManager = new GridLayoutManager(getActivity(),2);
mLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
    @Override
    public int getSpanSize(int position) {
        return position == 0 ? 2 : 1;
    }
});
mRecyclerView.setLayoutManager(mLayoutManager);
mAdapter.setHighlightsCallbacks(this);
mRecyclerView.setAdapter(mAdapter);
mRecyclerView.setItemAnimator(new DefaultItemAnimator());
mRecyclerView.addItemDecoration(new GridDividerDecoration(getActivity()));

我还修改了GridDividerDecoration类中的drawHorizo​​ntal方法,只有在imageview为null但仍然不工作时才绘制:

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);
        if (child.findViewById(R.id.home_imgHeader) == null) {
            final RecyclerView.LayoutParams params =
                    (RecyclerView.LayoutParams) child.getLayoutParams();
            final int left = child.getRight() + params.rightMargin + mInsets;
            final int right = left + mDivider.getIntrinsicWidth();
            mDivider.setBounds(left,top,right,bottom);
            mDivider.draw(c);
        }
    }
}

任何帮助?

解决方法

假设您的项目没有单独具有实体背景(即白色来自父母),最简单的修复将是将代码从onDrawOver()移动到onDraw().这将在您的子视图内容下绘制网格线,并将其隐藏在标题图像下.

否则,您使用的代码假定网格线总是绘制在drawHorizo​​ntal()中的视图顶部(注意top是一个永不改变的值).您将需要修改功能解决第一个项目的底部,并开始在该处绘制.就像是:

public void drawHorizontal(Canvas c,RecyclerView parent) {
    final View topView = parent.findViewHolderForAdapterPosition(0);
    int viewBottom = -1;
    if (topView != null) {
        final RecyclerView.LayoutParams params =
            (RecyclerView.LayoutParams) child.getLayoutParams();
        viewBottom = child.getBottom() + params.bottomMargin + mInsets;
    }

    final int top = viewBottom > parent.getPaddingTop() ? viewBottom : parent.getPaddingTop();
    final int bottom = parent.getHeight() - parent.getPaddingBottom();

    … /* Remaining Code */
}

猜你在找的Android相关文章