交错网格布局行为问题

时间:2018-04-06 08:57:12

标签: android android-recyclerview recyclerview-layout staggeredgridlayout staggeredgridlayoutmanager

我是Android的新手。 有人可以帮助"奇怪" StaggeredGridLayout行为。 GIF附件如下:

first_example_StaggeredGridLayout_behavior.gif

second_example_StaggeredGridLayout_behavior.gif

我的回收商初始代码:

 val layoutManager = StaggeredGridLayoutManager(resources.getInteger(R.integer.span_value), StaggeredGridLayoutManager.VERTICAL)
 layoutManager.gapStrategy = StaggeredGridLayoutManager.GAP_HANDLING_MOVE_ITEMS_BETWEEN_SPANS
 photoRecycler.adapter = adapterUnsplash
 photoRecycler.layoutManager = layoutManager
 photoRecycler.addItemDecoration(StaggeredItemDecoration(view.resources.getInteger(R.integer.column_spacing)))

ItemDecoration类

class StaggeredItemDecoration constructor(private var space: Int)
: RecyclerView.ItemDecoration() {

    override fun getItemOffsets(outRect: Rect, view: View, parent: RecyclerView, state: RecyclerView.State?) {
       val halfSpacing = space / 2
       outRect.set(halfSpacing, halfSpacing, halfSpacing, halfSpacing)

 }
}

我研究过很多来源,但都没有对我的问题有所帮助。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

您是否尝试过使用Animator项目?

photoRecycler.setItemAnimator(null);

上面的代码将删除应用于回收站视图的默认动画。