5

I have RecyclerView with 2 columns. What I want is to show last item in center if number of items in list is odd. Something like this

enter image description here

I have tried implementing all the so threads from here https://www.google.com/search?client=firefox-b-d&q=show+last+item+in+recyclerview+center+horizontal+android

I have used below code to set layout manager

    gridLayoutManager.setSpanSizeLookup(new GridLayoutManager.SpanSizeLookup() {
        @Override
        public int getSpanSize(int position) {
            if (mLangInfos.size() % 2 == 0)
                return 1;
            else {
                if (position == mLangInfos.size()-1)
                    return 2;
                else
                    return 1;
            }
        }
    });
    recyclerView.setLayoutManager(gridLayoutManager);

But what this does is it shows last item in full width, see below image.

enter image description here

karan
  • 8,637
  • 3
  • 41
  • 78
  • 1
    You can you `FlexboxLayoutManager` https://stackoverflow.com/q/50340698/7666442 – AskNilesh Jun 22 '20 at 06:46
  • can you give me the pointers on how to implement, I read through it but not helping much – karan Jun 22 '20 at 07:09
  • I have added an example in this answer https://stackoverflow.com/a/50411183/7666442 with full code what extra information you need to use? – AskNilesh Jun 22 '20 at 07:11
  • I want my items to be of equal size. The solution you provided gives me all items in single row. also the items are of unequal size. – karan Jun 22 '20 at 07:15
  • Just threw `FlexboxLayoutManager` out in the garbage - there is a bug where all items are not showing. 5+ years and still not fixed. No thanks. https://github.com/google/flexbox-layout/issues/420 – Starwave Apr 11 '23 at 14:21

0 Answers0