在RecyclerView中设置contentInset

时间:2015-03-20 09:36:11

标签: android scrollview android-recyclerview

我正在尝试在iOS中实现类似RecyclerView的{​​{1}}。 我想在回收站视图的底部设置一个UICollectionView,但我找不到这样做的选项。

Android有类似内容吗?

6 个答案:

答案 0 :(得分:22)

解决方案很简单:

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

答案 1 :(得分:2)

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

这是我的水平滚动回收视图的代码。

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

答案 2 :(得分:1)

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

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

虽然这个问题已经超过一年了,但我认为我会分享我的所作所为,有人可能会喜欢它,或者只是不想打字并且可以特此复制/粘贴:D。< / p>

我的解决方案是在最后一项添加一些底部边距。

首先我确定它是最后一项:

@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(position == mItems.size()-1) {
        // If so, add some margin to the element
        holder.setBottomMargin(R.dimen.last_item_offset); // some dimension (or set a fixed amount of pixels
    }
}

然后我在视图中有一个方法来设置底部边距。

class ItemViewHolder extends RecyclerView.ViewHolder {
    private View mRootView;
    private Context mContext;

    public ItemViewHolder(View itemView) {
        super(itemView);
        mRootView = itemView;
        mContext = itemView.getContext();
        // do more stuff like binding views, etc
    }

    public void setBottomMargin(@DimenRes int margin){
        RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) mRootView.getLayoutParams();
        layoutParams.bottomMargin = (int)getContext().getResources().getDimension(margin);
        mRootView.setLayoutParams(layoutParams);
    }
}

为了完整本例,维度

<resources>    
    <dimen name="spot_list_footer_position_offset">-56dp</dimen>
</resources>

我希望这就是你要找的东西。

答案 5 :(得分:0)

这个答案将是@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>