java 自动为RecyclerView项添加相等的间距。可以处理水平,垂直和网格显示模式

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 自动为RecyclerView项添加相等的间距。可以处理水平,垂直和网格显示模式相关的知识,希望对你有一定的参考价值。

## BEFORE
<img src="http://i.imgur.com/gYoIvID.png" width="400" alt="before">

## AFTER
<img src="http://i.imgur.com/xTiItWa.png" width="400" alt="after">
// Automatically infer display orientation of items
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.addItemDecoration(new EqualSpacingItemDecoration(16)); // 16px. In practice, you'll want to use getDimensionPixelSize

// Explicitly specify display orientation of items.
// Available display modes:
//   - HORIZONTAL
//   - VERTICAL
//   - GRID
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.addItemDecoration(new EqualSpacingItemDecoration(16, EqualSpacingItemDecoration.HORIZONTAL)); // 16px. In practice, you'll want to use getDimensionPixelSize
import android.graphics.Rect;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

public class EqualSpacingItemDecoration extends RecyclerView.ItemDecoration {
  private final int spacing;
  private int displayMode;

  public static final int HORIZONTAL = 0;
  public static final int VERTICAL = 1;
  public static final int GRID = 2;

  public EqualSpacingItemDecoration(int spacing) {
    this(spacing, -1);
  }

  public EqualSpacingItemDecoration(int spacing, int displayMode) {
    this.spacing = spacing;
    this.displayMode = displayMode;
  }

  @Override
  public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
    int position = parent.getChildViewHolder(view).getAdapterPosition();
    int itemCount = state.getItemCount();
    RecyclerView.LayoutManager layoutManager = parent.getLayoutManager();
    setSpacingForDirection(outRect, layoutManager, position, itemCount);
  }

  private void setSpacingForDirection(Rect outRect,
                                      RecyclerView.LayoutManager layoutManager,
                                      int position,
                                      int itemCount) {
    
    // Resolve display mode automatically
    if (displayMode == -1) {
      displayMode = resolveDisplayMode(layoutManager);
    }

    switch (displayMode) {
      case HORIZONTAL:
        outRect.left = spacing;
        outRect.right = position == itemCount - 1 ? spacing : 0;
        outRect.top = spacing;
        outRect.bottom = spacing;
        break;
      case VERTICAL:
        outRect.left = spacing;
        outRect.right = spacing;
        outRect.top = spacing;
        outRect.bottom = position == itemCount - 1 ? spacing : 0;
        break;
      case GRID:
        if (layoutManager instanceof GridLayoutManager) {
          GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
          int cols = gridLayoutManager.getSpanCount();
          int rows = itemCount / cols;

          outRect.left = spacing;
          outRect.right = position % cols == cols - 1 ? spacing : 0;
          outRect.top = spacing;
          outRect.bottom = position / cols == rows - 1 ? spacing : 0;
        }
        break;
    }
  }

  private int resolveDisplayMode(RecyclerView.LayoutManager layoutManager) {
    if (layoutManager instanceof GridLayoutManager) return GRID;
    if (layoutManager.canScrollHorizontally()) return HORIZONTAL;
    return VERTICAL;
  }
}

以上是关于java 自动为RecyclerView项添加相等的间距。可以处理水平,垂直和网格显示模式的主要内容,如果未能解决你的问题,请参考以下文章

在键盘打开时将项目添加到 RecyclerView 时向下滚动

为RecyclerView的不同item项实现不同的布局(添加分类Header)

RecyclerView 自动滚动到 WebView/Fresco SimpleDraweeView 项

从其他片段添加新的 RecyclerView 项

Android RecyclerView 添加和删除项

markdown 使用自定义ItemDecoration为Android RecyclerView GridLayoutManager提供相等的列间距