1

StaggeredLayoutManager 在尝试调整单元格时,有时会在滚动期间使单元格位置混乱。我的 RecyclerView 中有一个 HeaderView 和 NormalViews。以下是设置代码:

int spacing = (int) getResources().getDimension(R.dimen.post_item_spacing);
        mBindings.rvPosts.addItemDecoration(new PostsEqualGapItemDecoration(AppConstants.POSTS_SPAN_COUNT, spacing));
        mPostsLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS);
        mBindings.rvPosts.setLayoutManager(mPostsLayoutManager);
        mProfileAdapter.setRecyclerView(mBindings.rvPosts);
        mBindings.rvPosts.setAdapter(mAdapter);
        mBindings.rvPosts.setNestedScrollingEnabled(false);

物品装饰类:

public class PostsEqualGapItemDecoration extends RecyclerView.ItemDecoration {

    private int spanCount;
    private int spacing;

    public PostsEqualGapItemDecoration(int spanCount, int spacing) {
        this.spanCount = spanCount;
        this.spacing = spacing;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

        StaggeredGridLayoutManager.LayoutParams layoutParams = (StaggeredGridLayoutManager.LayoutParams) view.getLayoutParams();

        if (layoutParams.isFullSpan()) {
            outRect.set(0, 0, 0, 0);
          } else {
            int spanIndex = layoutParams.getSpanIndex();
            int layoutPosition = layoutParams.getViewLayoutPosition();
            int itemCount = parent.getAdapter().getItemCount();

            boolean leftEdge = spanIndex == 0;
            boolean rightEdge = spanIndex == (spanCount - 1);

            boolean topEdge = spanIndex < spanCount;
            boolean bottomEdge = layoutPosition >= (itemCount - spanCount);

            int halfSpacing = spacing / 2;

            /**
             * Updated values to keep cells width same and spacing from left and right
             * most item to be double than gap between items themselves.
             */
            outRect.set(
                    leftEdge ? spacing * 2 : rightEdge ? 0 : spacing,
                    topEdge ? spacing : halfSpacing,
                    rightEdge ? spacing * 2 : leftEdge ? 0 : spacing,
                    bottomEdge ? spacing : 0
            );


        }
    }
}

截屏:

和

4

1 回答 1

0
You have to write custom item decoration for that case to your child item views
public class SpacesItemDecoration extends RecyclerView.ItemDecoration {
  private int space;

  public SpacesItemDecoration(int space) {
    this.space = space;
  }

  @Override
  public void getItemOffsets(Rect outRect, View view, 
      RecyclerView parent, RecyclerView.State state) {
    outRect.left = space;
    outRect.right = space;
    outRect.bottom = space;

    // Add top margin only for the first item to avoid double space between items
    if (parent.getChildLayoutPosition(view) == 0) {
        outRect.top = space;
    } else {
        outRect.top = 0;
    }
  }
}
于 2019-04-02T07:45:41.960 回答