0

I'm creating a calendar application, and I need a way to display the dates by month. With RecylcerView, is there a way to make some rows have less items, and have those items shifted over accordingly? For example if the month starts on a Wednesday, the first row would only have 4 items, and the first item in that row would be directly above the 4th item in the rows below it. The items should all have the same size. Is there a way to do this, or do I have to do something like have a complete grid (with no shifts) but somehow get the cells that I don't want to show to be blank?

java
  • 11
  • 3

1 Answers1

0

I think that the FlexboxLayoutManager from https://github.com/google/flexbox-layout might suit your need

When the item is the "First Day of the Week" you would set wrapBefore on that item and you would also set justifyContent on the layout to be flex_end

With this you are manually in control of how many items there are in a row.

Update: while this solution works for the start of the month it just shifts the problem to the end of the month

enter image description here

I did it in onBindViewHolder in the recycler's adapter with the code below, the frame view holder was just the root layout element the recycler's item layout.

public void onBindViewHolder(@NonNull DaysViewHolder holder, int position) {
        final Pair<Integer,String> day = dayList.get(position);
        holder.dayNumber.setText(String.format(Locale.ROOT,"%d", day.first));
        holder.dayName.setText(String.format(Locale.ROOT,"%s", day.second));
        if (day.second.equals("Sunday")) {
            ViewGroup.LayoutParams lp = holder.frame.getLayoutParams();
            FlexboxLayoutManager.LayoutParams flexboxLp = (FlexboxLayoutManager.LayoutParams) lp;
            flexboxLp.setWrapBefore(true);
        }
    }

If you look at a lot of calendar UI's they pad the start and end of the months with the actual days of previous and next month.

e.g.

enter image description here

Andrew
  • 8,198
  • 2
  • 15
  • 35
  • how do I set wrapBefore on an item? does I need the item need to be inside a flexbox layout, or does any layout work? When i try to set it in a simlar way as https://stackoverflow.com/questions/45868488/set-flexboxlayouts-attribute-layout-wrapbefore-programmatically, except using FlexBoxLayoutMananger.LayoutParams with a linear layout, it crashes – java Apr 03 '20 at 16:47