Android RecyclerView finding out first and last view on ItemDecoration-ThrowExceptions

Exception or error:

Here is my scenario

  • I have to add space on first and last item of Recycler View
  • I am doing so using ItemDecoration

     recyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
    
    
        @Override
        public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            int totalHeight = parent.getHeight();
            int viewHeight = getResources().getDimensionPixelOffset(R.dimen.breadcrumb_btn_height);
            int padding = (totalHeight - viewHeight) / 2;
            //to position view on middle
            if (view.getTag() instanceof Integer) {
                int position = (int) view.getTag();
                if (position == 0) {
                    //first position
                    outRect.set(padding, padding, 0, padding);
                } else if (position == linearLayoutManager.getItemCount() - 1)   {
                    //last position
                    outRect.set(0, padding, padding, padding);
                } else {
                    outRect.set(0, padding, 0, padding);
                }
            }
    
    
        }
    });
    

    Here I have

  • LinearLayoutManager with Horizontal Orientation

    I am adding position tag on RecyclerView Adapter in the following manner

    @Override
    public void onBindViewHolder(BreadCrumbViewHolder holder, int position) {
    holder.setPosition(position);
    }
    

    Is there any other way to access RecyclerView first and last child on ItemDecoration so that later on removing and adding items won’t have any problem.

How to solve:

Is there any other way to access RecyclerView first and last child on
ItemDecoration so that later on removing and adding items won’t have
any problem.

Yes. You can achieve what you want only by implementing your own ItemDecoration.

To get the current item/child position:

int position = parent.getChildAdapterPosition(view);

To get the number of items/childs:

int itemCount = state.getItemCount();

With this you can add a space on the first and last child of your RecyclerView.


Now changing your code for this approach you get:

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

    //your padding...

    final int itemPosition = parent.getChildAdapterPosition(view);
    if (itemPosition == RecyclerView.NO_POSITION) {
        return;
    }

    final int itemCount = state.getItemCount();

    /** first position */
    if (itemPosition == 0) {
        outRect.set(padding, padding, 0, padding);
    }
    /** last position */
    else if (itemCount > 0 && itemPosition == itemCount - 1) {
        outRect.set(0, padding, padding, padding);
    }
    /** positions between first and last */
    else {
        outRect.set(0, padding, 0, padding);
    }
}

Bonus: If you are looking to give padding to all items of the RecyclerView take a look at the Gist I’ve created: https://gist.github.com/ryanamaral/93b5bd95412baf85a81a

Leave a Reply

Your email address will not be published. Required fields are marked *