litho icon indicating copy to clipboard operation
litho copied to clipboard

ItemDecoration for recycler not correct

Open ironmanTony opened this issue 5 years ago • 2 comments

Version

litho-* 0.29.0 tools.build.gradle: 3.4.1

Issues and Steps to Reproduce

the result WeWork Helper20190726030201

Expected Behavior

horizontal grid have the same gap;

Link to Code

use recycler:

    public static ViewGroup createRecycler(Context context) {
        ComponentContext c = new ComponentContext(context);
        final RecyclerBinder binder = new RecyclerBinder.Builder()
                .layoutInfo(new GridLayoutInfo(context, 4))
                .build(c);
        Component component = MyRecycler.create(c)
                .binder(binder).build();
        for (int i = 0; i < 100; i++) {
            binder.appendItem(Text.create(c).text("hello:" + i)
                    .textSizeDip(32)
                    .paddingDip(YogaEdge.VERTICAL, 4)
                    .build());
        }
        return LithoView.create(c, component);
    }

MyRecyclerSpec:

@LayoutSpec
public class MyRecyclerSpec {

    @OnCreateLayout
    static Component onCreateLayout(ComponentContext c, @Prop RecyclerBinder binder) {
        return Recycler.create(c)
                .binder(binder)
                .itemDecoration(new FduiHomeTabDecoration(false))
                .build();
    }
}

decoration:

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
//        super.getItemOffsets(outRect, view, parent, state);
        GridLayoutManager.LayoutParams layoutParams = (GridLayoutManager.LayoutParams) view.getLayoutParams();
        int spanIndex = layoutParams.getSpanIndex();
        switch (spanIndex) {
            case 0:
                outRect.set(dip8, dip8, dip4, 0);
                break;
            case 1:
            case 2:
                outRect.set(dip4, dip8, dip4, 0);
            case 3:
                outRect.set(dip4, dip8, dip8, 0);
        }

ironmanTony avatar Jul 26 '19 07:07 ironmanTony

Unfortunately I believe our support for item decorations isn't very well tested, especially when it comes to offsets and paddings. If you see a simple fix then we would welcome a PR :) cc @pasqualeanatriello

astreet avatar Sep 03 '19 01:09 astreet

If I set a width for the grid items, the itemDecoration works well.

ironmanTony avatar Apr 08 '20 13:04 ironmanTony