StaggeredLayoutManager нарушает положение ячеек во время настройки в RecyclerView - PullRequest
0 голосов
/ 02 апреля 2019

StaggeredLayoutManager иногда искажает положение ячеек во время прокрутки при попытке их отрегулировать. У меня есть HeaderView и NormalViews в моем RecyclerView. Ниже приведен код установки:

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
            );


        }
    }
}

Скриншот:

and

1 Ответ

0 голосов
/ 02 апреля 2019
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;
    }
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...