#1002·epoxy

在 Carousel 中使用 GridLayoutManager(..LinearLayoutManager.HORIZONTAL) 时, item 之间的间距不正确

作者: wsmni520创建于 2020年6月19日更新于 2024年9月11日

Hi, brother, the spacing between items seems incorrect when I use GridLayoutManager(..LinearLayoutManager.HORIZONTAL) in Carousel. The screen size is 1080*2560. I build the Carousel model in epoxyController like this: `hotCityViewCarouselModelBuilder { id("hotCity") for (index in 0 until 9) { itemHotCityHolder { id("hotCityItem $index") listener { Log.d("zf", "click") } cityName("item") } } }` The code for the HotCityViewCarousel class is as follows: `@ModelView(autoLayout = ModelView.Size.MATCH_WIDTH_WRAP_HEIGHT) class HotCityViewCarousel constructor(context: Context) : Carousel(context) { private var SPAN_COUNT = 3 override fun getSnapHelperFactory(): Nothing? = null override fun createLayoutManager(): LayoutManager { return GridLayoutManager(context, SPAN_COUNT, LinearLayoutManager.HORIZONTAL, false) } override fun getDefaultSpacingBetweenItemsDp(): Int { return 50 } }` The layout for the Carousel item is as follows: `<?xml version="1.0" encoding="utf-8"?>` <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="60dp" android:layout_height="60dp"> <TextView android:id="@+id/hotCityNameTv" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp" android:background="?attr/selectableItemBackground" /> </FrameLayout> The position of the items in the layout is as follows: 0 3 6 1 4 7 2 5 8 Here is the coordinate of the Carousel and all the items in the screen, 1dp = 3px, getDefaultSpacingBetweenItemsDp() = 50dp The coordinate is as follows: (1) androidx.recyclerview.widget.RecyclerView [0,300][1080,1590] item0 > FrameLayout [150,450][330,630] item3 > FrameLayout [480,450][660,630] item6 > FrameLayout [810,450][990,630] item1 > FrameLayout [150,855][330,1035] item4 > FrameLayout [480,855][660,1035] item7 > FrameLayout [810,855][990,1035] item2 > FrameLayout [150,1185][330,1365] item5 > FrameLayout [480,1185][660,1365] item8 > FrameLayout [810,1185][990,1365] We see that the topPadding for the first item-item0 is (450 - 300) = 150px, which is correct. The leftPadding is (150 - 0) = 150px, which is also correct. The space between item0 and item1 is (855-630) = 225px, which is incorrect. The space between item1 and item2 is (1185-1035) = 150px, which is correct. The bottomPadding for the last item-item8 is (1590-1365) = 225px, which is incorrect. The rightPadding for the end of the scroll is (1080 -930) = 150px, which is correct. Although I don't often use this scene, I hope the issue will help epoxy.