在 RecyclerView 中设置 contentInset

Set contentInset in RecyclerView

我正在尝试在 iOS 中实现 RecyclerView,就像 UICollectionView。 我想在回收站视图的底部设置一个 contentInset,但找不到执行此操作的选项。

Android有类似的东西吗?

我已经通过添加自定义 RecyclerView Cell 解决了这个问题,它在视图的末尾添加了一个具有清晰背景颜色的偏移量。

我创建了一个包装器适配器来实现这一点,它很好地隔离了代码。但是,我想要一个更好的解决方案..

public class HeaderSpaceWrapperAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private static final int TYPE_HEADER = 0x4711;
    private int mSpace;
    RecyclerView.Adapter mAdapter;


    public HeaderSpaceWrapperAdapter(RecyclerView.Adapter adapter, int space) {
        mAdapter = adapter;
        mSpace = space;

        mAdapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
            @Override
            public void onChanged() {
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER) {
            return new ViewHolder(new Spacer(parent.getContext(), mSpace));
        }

        return mAdapter.onCreateViewHolder(parent, viewType);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position > 0) {
            mAdapter.onBindViewHolder(holder, position - 1);
        }
    }

    @Override
    public int getItemCount() {
        return 1 + mAdapter.getItemCount();
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? TYPE_HEADER : mAdapter.getItemViewType(position - 1);
    }

    private class ViewHolder extends RecyclerView.ViewHolder {

        public ViewHolder(View view) {
            super(view);
        }
    }
}

然后使用它:

mWrapperAdapter = new HeaderSpaceWrapperAdapter(mAdapter, 100);
mRecyclerView.setAdapter(mWrapperAdapter);

这个答案将是对@Phin 答案的增强

@Override
public void onBindViewHolder(final ItemViewHolder holder, int position) {

    .... 
    Your other code
    ....
    // Check if it is the last item and the list is not empty.
            if(i == arraylist.size()-1) {
                // If so, add some margin to the element
                itemHolder.setBottomMargin(R.dimen.quick_controls_height); // some dimension (or set a fixed amount of pixels
            } else {
                itemHolder.setBottomMargin(R.dimen.zero_margin); //Set zero margin as the views will be reused.
            }

}

dimens.xml :

<dimen name="quick_controls_height">34.1dp</dimen>
<dimen name="zero_margin">0dp</dimen>

您可以创建自己的 ItemDecoration 并在其中设置第一个项目的偏移量以及项目之间的边距。

这是我的水平滚动 recyclerview 代码。

public class HorizontalContentItemDecoration extends RecyclerView.ItemDecoration {
  private int startOffset = 10;
  private int itemOffset = 10;

  public HorizontalContentItemDecoration(int startOffset, int itemOffset) {
    this.startOffset = startOffset;
    this.itemOffset = itemOffset;
  }

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

    int position = parent.getChildAdapterPosition(view);

    if (position == 0) {
      outRect.set(startOffset, 0, itemOffset, 0);
    } else {
      outRect.set(0, 0, itemOffset, 0);
    }
  }
}

解决方法很简单:

 <android.support.v7.widget.RecyclerView
   android:paddingBottom="30dp"
   android:clipToPadding="false"
   android:layout_width="match_parent"
   android:layout_height="match_parent" />