项目需要使用瀑布流,看了下感觉还挺容易,结果上下滑动的时候遇到item换位问题
<androidx.recyclerview.widget.RecyclerView android:id="@+id/mRecyc" android:layout_width="match_parent" android:layout_height="match_parent" />
看了别人的解决方法,结果还是不行,最后才发现,是我把recycleview高度设成了wrap_content
附带解决方法
//声明瀑布流的布局方式: 2列,垂直方向 StaggeredGridLayoutManager staggeredGridLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL); //为recyclerView设置布局管理器 mRecyc.setLayoutManager(staggeredGridLayoutManager); staggeredGridLayoutManager.setGapStrategy(StaggeredGridLayoutManager.GAP_HANDLING_NONE);
在adapter内,
Map<Integer, Integer> heightArray = new HashMap<>();使用map保存已加载的view的高度
if (heightArray.get(i) == null) { int height = 0; ConstraintLayout.LayoutParams imageParams = (ConstraintLayout.LayoutParams) secKillViewHolder.mImage_video.getLayoutParams(); imageParams.height = height; secKillViewHo