android StaggeredGridLayoutManager 瀑布流 数据错乱 间距消失解决方案,亲测可用
·
在使用StaggeredGridLayoutManager实现recyclerview瀑布流的时候,发现数据会错乱,当数据错乱之后就会有间距问题,左右间距变成两边了,比较难受,查了好多资料,最后发现这种方式对我有帮助
核心就是在设置itemdecoration的时候,判断位置不根据parent.getChildAdapterPosition(view),而是根据
val params = view.layoutParams as StaggeredGridLayoutManager.LayoutParams
val spanIndex = params.spanIndex
这个来,这个的位置是item实际的位置
val params = view.layoutParams as StaggeredGridLayoutManager.LayoutParams
val spanIndex = params.spanIndex
if (spanIndex % 2 == 0) { //左
outRect.left = 0;
outRect.right=spacing/2;
} else { //右
// item为奇数位,设置其左间隔为5dp
outRect.left=spacing/2;
outRect.right = 0;
}
// 下方间隔
outRect.bottom = spacing;
DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。
更多推荐



所有评论(0)