How to prepare setSpanSizeLookup in GridLayoutManager

心已入冬 提交于 2021-01-29 19:01:01

问题


i need every 18,36,54,...18*Nth... grid with layout with width:match parent and height:60dp in grid layout manager for pagination loadingbar purpose.

Here is my code,

 GridLayoutManager mng_layout = new GridLayoutManager(this, 4);//WHAT IS 4 ?//
            mng_layout.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
                @Override
                public int getSpanSize(int position) {
                    int mod = position % 6;   //WHAT IS 6 ?//
    
                    if (position == 0 || position == 1)
                        return 2;    //WHAT IS 2 ?//
                    else if (position < 6)
                        return 1;   //WHAT IS 1 ?//
                    else if (mod == 0 || mod == 1)
                        return 2;   //WHAT IS 2 ?//
                    else
                        return 1;   //WHAT IS 1 ?//
                }
            }); 

if anybody have a time please explain commented line in code


回答1:


Span size means that this items take how many cells from the grid

public int getSpanSize(int position) {
                int mod = position % 6;   //here 18%6=0 ,19%6=1 ,36%6=0 ,37%6=1 and so one    
                if (position == 0 || position == 1)
                    return 2;    // if this is the first or sesond items make it take span size of 2 so 4/2 =2 cells will be shows
                else if (position < 6)
                    return 1;   // here 2,3,4,5 < 6 so every item should take span size 1 out of 4 , so we have 4 cells 
                else if (mod == 0 || mod == 1)
                    return 2;   // here we check the mode which is defined above 18,19 & 36,37 ,54,55 and so on every pair will span 2 cells for every number 
                else
                    return 1;   // any thing other like  will take one span out of 4 grid columns 
            }
        }

you can take a look at this tutorial



来源:https://stackoverflow.com/questions/63509661/how-to-prepare-setspansizelookup-in-gridlayoutmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!