Setting the contents of an INSET in a RecyclerView

I am trying to implement RecyclerView as a UICollectionView in iOS. I want to set the contentInset at the bottom of the recycler window, but I cannot find a variant of it.

Is there something similar for Android?

+7
android scrollview android-recyclerview
source share
6 answers

The solution is simple:

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

I created a batch adapter for this, which perfectly isolates the code. But I would like to get a better solution.

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

Then use it:

 mWrapperAdapter = new HeaderSpaceWrapperAdapter(mAdapter, 100); mRecyclerView.setAdapter(mWrapperAdapter); 
+1
source share

You can create your own ItemDecoration and set the offset there for the first item, as well as the fields between the items.

Here is my code for my re-viewing the horizontal scroll.

 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); } } } 
+1
source share

I decided to add a custom RecyclerView cell that adds an offset at the end of the view with a clear background color.

0
source share

Although this question is more than a year old, I thought that I would share what I did, someone might like it, or simply do not want to print and can copy / paste: D.

My solution was to add a bottom edge to the last element.

First, I determine that this is the last element:

 @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 } } 

Then I have a method in the view node to set the bottom field.

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

And for the completeness of this example, dimension

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

Hope this is what you were looking for.

0
source share

This answer will be an improvement on @Phin's answer.

 @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. } } 

dens.xml:

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

All Articles