luizgrp / SectionedRecyclerViewAdapter

An Adapter that allows a RecyclerView to be split into Sections with headers and/or footers. Each Section can have its state controlled individually.
MIT License
1.68k stars 372 forks source link

How to decorate only child items #97

Closed htooaunghlaing closed 6 years ago

htooaunghlaing commented 6 years ago

QUESTION TEMPLATE

Is this library allow to use ItemDecoration class with RecyclerView?
Expected behaviour

I want to decorate child items with EqualSpacingItemDecoration(https://gist.github.com/alexfu/f7b8278009f3119f523a)

Demo Project

https://github.com/htooaunghlaing/Grid-Section-Recycler

Actual behaviour

In really, header row was assume as one cell, so, the 3rd item was always align to right. Pelase see the attachment image (https://imgur.com/a/vAtiD)

MCVE
 @Override
 public RecyclerView.ViewHolder getItemViewHolder(View view) {
            ItemViewHolder holder =  new ItemViewHolder(view);
            view.getLayoutParams().height = (int) (Utality.getDeviceWidth(getContext()) / 3 * 1.9f); //3 is Span Count & 1.9f is image ratio
            return holder;
        }

Checked in decoration class,

case GRID:
                if (layoutManager instanceof GridLayoutManager) {
                    GridLayoutManager gridLayoutManager = (GridLayoutManager) layoutManager;
                    int cols = gridLayoutManager.getSpanCount();
                    int rows = itemCount / cols;
                    int viewType = parent.getAdapter().getItemViewType(position);
                    if(viewType != SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER){
                        outRect.left = spacing;
                        outRect.right = position % cols == cols - 1 ? spacing : 0;
                        outRect.top = spacing;
                        outRect.bottom = position / cols == rows - 1 ? spacing : 0;
                    }
                }

Span Size LookUp

GridLayoutManager glm = new GridLayoutManager(getContext(), 3);
        glm.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
            @Override
            public int getSpanSize(int position) {
                switch(sectionAdapter.getSectionItemViewType(position)) {
                    case SectionedRecyclerViewAdapter.VIEW_TYPE_HEADER:
                        return 3;
                    default:
                        return 1;
                }
            }
        });

Please don't hesitate and feel free to talk me if I make something wrong.

luizgrp commented 6 years ago

Hi @htooaunghlaing,

Do you think there is an issue with this library that is not allowing you to use ItemDecoration properly? Or you have a question how to use that EqualSpacingItemDecoration code with this library? If it's a question please edit your template with the question template. If it's an issue please edit your template and clarify:

Expected behaviour

What's the expected behaviour? You want to see equal spacing between the items of the section?

Actual behaviour

Thanks for the image, but please clarify what is the actual behaviour, the item on the very right has no space on the right?

MCVE

Please provide a simpler ItemDecoration, setting a fixed amount of spacing and simple layouts.

I will reopen the issue when it's clarified and enough information is provided.

htooaunghlaing commented 6 years ago

@luizgrp , I have created a demo project for it.please check it out and let me know if I missed somethings else. Thanks you .

https://github.com/htooaunghlaing/Grid-Section-Recycler

htooaunghlaing commented 6 years ago

@luizgrp , I have updated my issue and added codes and example and Iet me know any response from you.

luizgrp commented 6 years ago

Could you please clarify the expected behaviour and the current behaviour? You want to have equal spacing between the content items and the items from the 3rd column don't have spacing on the right? Is that what you mean?

Your example still has too much code, is not minimal, I can't answer for that code from that gist. Try to create your own RecyclerView.ItemDecoration, with minimal code. Try to find out the line of code where the issue is so I can try to help you.

If you need quicker help please use stackoverflow.com